Skip to content

Instantly share code, notes, and snippets.

@valyala
Created January 20, 2016 15:01
Show Gist options
  • Save valyala/37bd2c3f50a1235702d5 to your computer and use it in GitHub Desktop.
Save valyala/37bd2c3f50a1235702d5 to your computer and use it in GitHub Desktop.
Add readline support for interactive mode in go tool pprof
diff --git a/src/cmd/pprof/internal/plugin/plugin.go b/src/cmd/pprof/internal/plugin/plugin.go
index a22ec5f..4d545de 100644
--- a/src/cmd/pprof/internal/plugin/plugin.go
+++ b/src/cmd/pprof/internal/plugin/plugin.go
@@ -6,7 +6,6 @@
package plugin
import (
- "bufio"
"fmt"
"os"
"regexp"
@@ -14,6 +13,8 @@ import (
"time"
"cmd/pprof/internal/profile"
+
+ "github.com/chzyer/readline"
)
// A FlagSet creates and parses command-line flags.
@@ -166,16 +167,19 @@ type UI interface {
// prints messages to standard output,
// prints errors to standard error, and doesn't use auto-completion.
func StandardUI() UI {
- return &stdUI{r: bufio.NewReader(os.Stdin)}
+ r, err := readline.New("(pprof+r) ")
+ if err != nil {
+ panic(err)
+ }
+ return &stdUI{r: r}
}
type stdUI struct {
- r *bufio.Reader
+ r *readline.Instance
}
func (ui *stdUI) ReadLine() (string, error) {
- os.Stdout.WriteString("(pprof) ")
- return ui.r.ReadString('\n')
+ return ui.r.Readline()
}
func (ui *stdUI) Print(args ...interface{}) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment