...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package main
18
19 import (
20 "fmt"
21 "os"
22 "strings"
23 "syscall"
24
25 "github.com/chzyer/readline"
26 "github.com/google/pprof/driver"
27 )
28
29 func main() {
30 if err := driver.PProf(&driver.Options{UI: newUI()}); err != nil {
31 fmt.Fprintf(os.Stderr, "pprof: %v\n", err)
32 os.Exit(2)
33 }
34 }
35
36
37
38
39
40
41 type readlineUI struct {
42 rl *readline.Instance
43 }
44
45 func newUI() driver.UI {
46 rl, err := readline.New("")
47 if err != nil {
48 fmt.Fprintf(os.Stderr, "readline: %v", err)
49 return nil
50 }
51 return &readlineUI{
52 rl: rl,
53 }
54 }
55
56
57
58 func (r *readlineUI) ReadLine(prompt string) (string, error) {
59 r.rl.SetPrompt(prompt)
60 return r.rl.Readline()
61 }
62
63
64
65 func (r *readlineUI) Print(args ...interface{}) {
66 text := fmt.Sprint(args...)
67 if !strings.HasSuffix(text, "\n") {
68 text += "\n"
69 }
70 fmt.Fprint(r.rl.Stderr(), text)
71 }
72
73
74
75 func (r *readlineUI) PrintErr(args ...interface{}) {
76 text := fmt.Sprint(args...)
77 if !strings.HasSuffix(text, "\n") {
78 text += "\n"
79 }
80 if readline.IsTerminal(int(syscall.Stderr)) {
81 text = colorize(text)
82 }
83 fmt.Fprint(r.rl.Stderr(), text)
84 }
85
86
87 func colorize(msg string) string {
88 var red = 31
89 var colorEscape = fmt.Sprintf("\033[0;%dm", red)
90 var colorResetEscape = "\033[0m"
91 return colorEscape + msg + colorResetEscape
92 }
93
94
95
96 func (r *readlineUI) IsTerminal() bool {
97 return readline.IsTerminal(int(syscall.Stdout))
98 }
99
100
101 func (r *readlineUI) WantBrowser() bool {
102 return r.IsTerminal()
103 }
104
105
106
107 func (r *readlineUI) SetAutoComplete(complete func(string) string) {
108
109 }
110
View as plain text