From 55e3f4b02940171e93ade4dc08f9fdee4dd12c39 Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Sat, 13 Feb 2016 19:27:21 +0100 Subject: [PATCH] pushTip -> addTip; make it nicer to use. --- edit/bind.go | 3 +-- edit/builtins.go | 8 ++++---- edit/completers.go | 3 +-- edit/completion.go | 9 +++------ edit/editor.go | 18 +++++++++--------- 5 files changed, 18 insertions(+), 23 deletions(-) diff --git a/edit/bind.go b/edit/bind.go index 0b62991b..25092c34 100644 --- a/edit/bind.go +++ b/edit/bind.go @@ -164,8 +164,7 @@ func (c EvalCaller) Call(ed *Editor) { ec := eval.NewTopEvalCtx(ed.evaler, "[editor]", "", ports) ex := ec.PCall(c.Caller, []eval.Value{}) if ex != nil { - // XXX will disappear very quickly - ed.pushTip("function error: " + ex.Error()) + ed.notify("function error: %s", ex.Error()) } out.Close() diff --git a/edit/builtins.go b/edit/builtins.go index f931cd0b..eebd9087 100644 --- a/edit/builtins.go +++ b/edit/builtins.go @@ -102,7 +102,7 @@ func startInsert(ed *Editor) { func defaultCommand(ed *Editor) { k := ed.lastKey - ed.pushTip(fmt.Sprintf("Unbound: %s", k)) + ed.addTip("Unbound: %s", k) } func startCommand(ed *Editor) { @@ -232,7 +232,7 @@ func insertLastWord(ed *Editor) { if err == nil { ed.insertAtDot(lastWord(lastLine)) } else { - ed.pushTip(err.Error()) + ed.addTip("db error: %s", err.Error()) } } @@ -291,7 +291,7 @@ func defaultInsert(ed *Editor) { if k.Mod == 0 && k.Rune > 0 && unicode.IsGraphic(k.Rune) { insertKey(ed) } else { - ed.pushTip(fmt.Sprintf("Unbound: %s", k)) + ed.addTip("Unbound: %s", k) } } @@ -337,7 +337,7 @@ func startHistory(ed *Editor) { if ed.prevHistory() { ed.mode = modeHistory } else { - ed.pushTip("no matching history item") + ed.addTip("no matching history item") } } diff --git a/edit/completers.go b/edit/completers.go index e85db9b7..02135d7f 100644 --- a/edit/completers.go +++ b/edit/completers.go @@ -1,7 +1,6 @@ package edit import ( - "fmt" "io/ioutil" "os" "path" @@ -130,7 +129,7 @@ func complArgInner(head string, ed *Editor, formHead bool) []*candidate { cands := []*candidate{} if err != nil { - ed.pushTip(fmt.Sprintf("cannot list directory %s: %v", dir, err)) + ed.addTip("cannot list directory %s: %v", dir, err) return cands } diff --git a/edit/completion.go b/edit/completion.go index ec1fbfc2..1393ea87 100644 --- a/edit/completion.go +++ b/edit/completion.go @@ -1,9 +1,6 @@ package edit -import ( - "fmt" - "unicode/utf8" -) +import "unicode/utf8" type styled struct { text string @@ -68,9 +65,9 @@ func startCompletionInner(ed *Editor, completePrefix bool) { } if c.candidates == nil { - ed.pushTip("unsupported completion :(") + ed.addTip("unsupported completion :(") } else if len(c.candidates) == 0 { - ed.pushTip(fmt.Sprintf("no candidate for %s", c.completer)) + ed.addTip("no candidate for %s", c.completer) } else { if completePrefix { // If there is a non-empty longest common prefix, insert it and diff --git a/edit/editor.go b/edit/editor.go index 74bb5a10..9935da8d 100644 --- a/edit/editor.go +++ b/edit/editor.go @@ -108,12 +108,12 @@ func (ed *Editor) flash() { // TODO implement fish-like flash effect } -func (ed *Editor) pushTip(more string) { - ed.tips = append(ed.tips, more) +func (ed *Editor) addTip(format string, args ...interface{}) { + ed.tips = append(ed.tips, fmt.Sprintf(format, args)) } -func (ed *Editor) notify(msg string) { - ed.notifications = append(ed.notifications, msg) +func (ed *Editor) notify(format string, args ...interface{}) { + ed.notifications = append(ed.notifications, fmt.Sprintf(format, args...)) } func (ed *Editor) refresh(fullRefresh bool) error { @@ -129,7 +129,7 @@ func (ed *Editor) refresh(fullRefresh bool) error { _, err := ed.evaler.Compile(name, src, n) if err != nil { if err, ok := err.(*errutil.ContextualError); ok { - ed.pushTip("compiler error highlighted") + ed.addTip("compiler error highlighted") p := err.Pos() for i, token := range ed.tokens { if token.Node.Begin() <= p && p < token.Node.End() { @@ -301,19 +301,19 @@ MainLoop: case syscall.SIGCHLD: // ignore default: - ed.pushTip(fmt.Sprintf("ignored signal %s", sig)) + ed.addTip("ignored signal %s", sig) } case err := <-ed.reader.ErrorChan(): - ed.notify(err.Error()) + ed.notify("reader error: %s", err.Error()) case mouse := <-ed.reader.MouseChan(): - ed.pushTip(fmt.Sprint("mouse:", mouse)) + ed.addTip("mouse: %#v", mouse) case <-ed.reader.CPRChan(): // Ignore CPR case k := <-ed.reader.KeyChan(): lookupKey: keyBinding, ok := keyBindings[ed.mode] if !ok { - ed.pushTip("No binding for current mode") + ed.addTip("No binding for current mode") continue }