Documentation
¶
Overview ¶
Package hook makes a shell that a terminal program starts report the exit status of each command. With the hook, bash, zsh, sh, dash, ash and fish 3 send an OSC 133 D mark with the status before each prompt, and the program reads it from the shell's output, to show whether the last command failed, for example. fish 4 sends the marks itself.
The program builds the exec.Cmd that starts the user's shell and, before it starts the command, calls Prepare with it and a directory the program owns. Prepare finds the shell from the command's name, writes the shell's startup script into a directory under root named after the version of the scripts, and adds what makes the shell read it: bash reads kc.bash given with --rcfile, zsh reads kc.zsh as .zshenv from ZDOTDIR, sh reads kc.sh from ENV, and fish runs kc.fish given with -C. Each script reads the user's own startup file first and leaves no trace in the environment. A shell the package does not know starts unchanged. Set cmd.Env before the call, to os.Environ() for the program's own environment: for zsh and sh, Prepare adds a variable to it, and a nil Env would then hold only that variable.
The directories are private to the user (0700) and the files 0600. A file with other content is replaced atomically, so several programs and versions can share root, and nothing removes the files. The scripts and the variables they use are named after kc, the program they were written for.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Shell ¶
type Shell string
Shell is a shell the hook can be added to.
const ( // Unknown is a shell the package does not know; it starts unchanged. Unknown Shell = "" Bash Shell = "bash" Zsh Shell = "zsh" // POSIX is sh, dash or ash: an interactive one reads the file ENV // names. POSIX Shell = "sh" Fish Shell = "fish" )
The shells, named as Of finds them.
func Of ¶
Of returns the shell a program runs, from the name it is started with. bash started as sh behaves as sh, so the name decides.
Example ¶
This example finds the shell from the name a program starts it with.
package main
import (
"fmt"
"github.com/stokaro/kc/tui/shell/hook"
)
func main() {
for _, name := range []string{"/bin/bash", "dash", "/usr/bin/fish", "nu"} {
fmt.Printf("%s: %q\n", name, hook.Of(name))
}
}
Output: /bin/bash: "bash" dash: "sh" /usr/bin/fish: "fish" nu: ""
func Prepare ¶
Prepare changes cmd so that the shell it starts loads the hook, and returns which shell that is. cmd starts the shell named by cmd.Args[0] with cmd.Env. The file the shell reads is written into a directory under root that belongs to this version of the scripts. A shell the package does not know is left alone, and on an error cmd is not changed.
Example ¶
This example prepares the commands that start bash and zsh, writing the startup scripts into a temporary root. A program then starts the command in its terminal; the example only prints what Prepare changed.
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"github.com/stokaro/kc/tui/shell/hook"
)
// version matches the directory of one version of the scripts.
var version = regexp.MustCompile(`\$ROOT/[0-9a-f]{12}/`)
// show prints s with the temporary root as $ROOT and the version directory
// as <version>, so that the output does not depend on either.
func show(root, s string) string {
s = strings.ReplaceAll(s, root, "$ROOT")
return version.ReplaceAllString(filepath.ToSlash(s), "$$ROOT/<version>/")
}
func main() {
root, err := os.MkdirTemp("", "hook-example-")
if err != nil {
panic(err)
}
defer func() { _ = os.RemoveAll(root) }()
bash := exec.Command("bash", "-i")
bash.Env = []string{"HOME=/home/user"}
shell, err := hook.Prepare(bash, root)
if err != nil {
panic(err)
}
fmt.Println(shell, show(root, strings.Join(bash.Args, " ")))
zsh := exec.Command("/bin/zsh")
zsh.Env = []string{"HOME=/home/user", "ZDOTDIR=/home/user/.config/zsh"}
shell, err = hook.Prepare(zsh, root)
if err != nil {
panic(err)
}
fmt.Println(shell, show(root, strings.Join(zsh.Env, " ")))
unknown := exec.Command("tclsh")
shell, _ = hook.Prepare(unknown, root)
fmt.Printf("%q %v\n", shell, unknown.Args)
}
Output: bash bash -i --rcfile $ROOT/<version>/kc.bash zsh HOME=/home/user KC_ORIG_ZDOTDIR=/home/user/.config/zsh ZDOTDIR=$ROOT/<version>/zsh "" [tclsh]