Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
25 changes: 25 additions & 0 deletions shellterm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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()
Expand Down
40 changes: 40 additions & 0 deletions shellterm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 $" })
}
Loading