diff --git a/README.md b/README.md index 7b991d8..55993e1 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,7 @@ s := shellterm.New(shellterm.Options{ Post: post, // runs a func on the UI goroutine (required) Greeting: "my shell\r\n", // written once, before the first prompt OnDone: refresh, // optional: after each command, on the UI goroutine + Prompt: func() string { return cwd + " $ " }, // optional; default "$ " }) s.SetShell(myShell, myCompleter) // a lineedit.Completer, or nil term := toolkit.NewTerminal(80, 24, 5000) @@ -33,6 +34,13 @@ on every change. - **The prompt:** a submitted line goes to `Shell.Run` on a goroutine of its own. Its output becomes terminal output (`\n` → `\r\n`). A non-zero status shows `[exit status N]`, then the prompt returns. +- **A prompt that follows the shell:** `Options.Prompt` (optional; the default + is `$ `) is asked when the session starts and after each command, once `Run` + has returned, so it can show a working directory or the last status + (`[1] /tmp $ `). It runs on the session's goroutine with the session locked: + read the shell's state, do not call back into the `Session`. Control + characters are dropped from what it returns; an empty answer keeps the previous + prompt. It is drawn in lineedit's prompt style (bold cyan). - **While a command runs:** only Ctrl-C reaches it (it cancels `ctx`). `Running` (an `mvvm.Observable[bool]`) says so. - **Keys:** they are handled on the session's goroutine, never the UI's, so a diff --git a/shellterm.go b/shellterm.go index bd4f78a..a85cbf6 100644 --- a/shellterm.go +++ b/shellterm.go @@ -48,6 +48,16 @@ type Options struct { OnDone func() // HistorySize bounds the history (0: 500). HistorySize int + // Prompt, if set, returns the prompt to show before the line. It is + // called on the session's own goroutine, with the session locked, when the + // session starts (Attach, SetShell) and after each command, once Run has + // returned: so it may read state that the shell changed (a working + // directory, the last status) without a lock of its own, but must not call + // back into the Session. Control characters are dropped from its result (a + // prompt is one line of text, its width is counted as such); the prompt is + // drawn in the renderer's prompt style. "" (or nil Prompt) keeps the + // previous prompt, "$ " at first. + Prompt func() string } // Limits on what waits for the view or for the key goroutine. @@ -109,6 +119,20 @@ func (t *Session) SetShell(s Shell, complete lineedit.Completer) { } t.ed = lineedit.New(lineedit.Options{Complete: complete, Cols: t.r.Cols, HistorySize: t.opts.HistorySize}) t.ed.SetHistory(hist) + t.refreshPrompt() +} + +// refreshPrompt asks Options.Prompt for the prompt; t.mu is held and t.ed set. +func (t *Session) refreshPrompt() { + if t.opts.Prompt == nil { + return + } + t.ed.SetPrompt(strings.Map(func(r rune) rune { + if r < 0x20 || r == 0x7f || r >= 0x80 && r <= 0x9f { + return -1 + } + return r + }, t.opts.Prompt())) } // History returns the lines entered, oldest first (for persistence). @@ -273,6 +297,7 @@ func (t *Session) start(line string) { } else if code != 0 { fmt.Fprintf(&b, "\x1b[2m[exit status %d]\x1b[0m\r\n", code) } + t.refreshPrompt() b.WriteString(t.r.Render(t.ed.State())) t.mu.Unlock() out := b.String() diff --git a/shellterm_test.go b/shellterm_test.go index b87212c..7091147 100644 --- a/shellterm_test.go +++ b/shellterm_test.go @@ -270,3 +270,43 @@ func TestSessionWithoutOnDone(t *testing.T) { r.typeWait("go\r", "ran go") r.until("prompt", func() bool { return !r.running() && r.line() == "$" }) } + +// A prompt hook is asked at Attach and after every command, so the prompt +// follows state the shell changed (a working directory, the last status). +func TestSessionPromptHook(t *testing.T) { + sh := &recShell{} + var mu sync.Mutex + prompt := "/ $ " + r := newRig(t, Options{Prompt: func() string { mu.Lock(); defer mu.Unlock(); return prompt }}) + r.ses.SetShell(sh, nil) // asks once, before any view + r.attach() + r.until("first prompt", func() bool { return r.line() == "/ $" }) + + // The shell changes state; the prompt changes when the command is done. + mu.Lock() + prompt = "[3] /tmp $ " + mu.Unlock() + sh.set(3, false, nil) + r.typeWait("false\r", "[3] /tmp $") + r.until("new prompt", func() bool { return !r.running() && r.line() == "[3] /tmp $" }) + + // Control characters are dropped, an empty answer keeps the prompt. + mu.Lock() + prompt = "a\x1b[31m\nb\x7f\u0085 $ " + mu.Unlock() + sh.set(0, false, nil) + r.ses.Input("x\r") + r.until("sanitised", func() bool { return !r.running() && r.line() == "a[31mb $" }) + mu.Lock() + prompt = "" + mu.Unlock() + r.ses.Input("y\r") + r.until("kept", func() bool { return !r.running() && r.line() == "a[31mb $" }) + // A shell swap re-asks too, and the default stays "$ " without a hook. + mu.Lock() + prompt = "swapped $ " + mu.Unlock() + r.ses.SetShell(sh, nil) + r.ses.Input("z\r") + r.until("after SetShell", func() bool { return !r.running() && r.line() == "swapped $" }) +}