1 Shell Integration
liquibyte edited this page 2026-11-19 16:23:30 -06:00

Shell Integration

A shell can tell the terminal where each prompt starts, where the user's command starts, where its output starts and how it ended. Hosho then knows each command on the screen and what the shell does. This page describes the marks, the file Hosho ships for each shell and how it is loaded, and what a host does with the marks.

1. The marks

The shell sends four operating system commands, OSC 133, at four moments of each command, and the directory it is in (OSC 7). A terminal that does not know these sequences ignores them, so the files are safe in any terminal.

The four marks of shell integration in their order: A when a prompt starts, B where the prompt ends, C when the command's output starts, D when the command ended, then A of the next prompt

The four marks of one command. The shell sends D and the next A together, before each prompt. The state in green is what hosho_terminal_shell_state() gives after the mark.

Mark Sent What the terminal does
OSC 133 ; A Before each prompt. Marks the cursor's line as a prompt's line (HOSHO_MARK_PROMPT). OSC 133 ; A ; hosho=infer says the shell cannot send C (section 3).
OSC 133 ; B At the end of the prompt. Notes the cursor's column on its line (input_column): the user's command starts there. A shell that cannot send B gets that place from the cursor when the user types the first character.
OSC 133 ; C After the command line is read, before the command runs. Marks the cursor's line as the start of the command's output (HOSHO_MARK_OUTPUT), unless it has a mark, and notes the time.
OSC 133 ; D ; status When the command ended, before the next prompt. Gives the nearest prompt above the cursor, also in the scrollback, its result, when it has none yet: HOSHO_STATUS_SUCCESS for a status of 0, HOSHO_STATUS_FAILURE for any other number. The status is the number up to the next ;, so options after it (D;0;aid=7) do not change it. A D with no status (OSC 133 ; D) says the command ended but not how: the prompt gets no result. After a C (or a start the terminal took from the typed keys, section 3), the host's command_end function gets the status (0 when there was none) and how many seconds the command ran.
OSC 7 ; file://host/path Before each prompt, or when the directory changes. Keeps the shell's directory (terminal->directory), with %XX decoded, so a host can open a file a compiler names.

The marks are not kept on the alternate screen: the terminal ignores OSC 133 while it is in use. An erase in the display (ED) removes the marks of the lines it erases whole; an erase in one line (EL), and a selective erase, keep them, as a shell erases its prompt's line to draw it again.

2. The shell's state

hosho_terminal_shell_state() says what the shell does, from its marks:

State When
HOSHO_SHELL_PROMPT A prompt shows and no text waits after it.
HOSHO_SHELL_INPUT A prompt shows with text after it that was not sent yet: the cursor is somewhere else than the place where the command starts, or it is there and a character is at that place, or that place has left the top of the screen. Until the terminal knows where the command starts (from B, or from the first typed key), the state is HOSHO_SHELL_PROMPT.
HOSHO_SHELL_RUNNING A command runs: after C, until D.
HOSHO_SHELL_UNKNOWN The shell sends no marks, or it is between a command's end and the next prompt.

The terminal tells the host each change through its shell_state function, after the bytes that changed it. A host can show the state while its terminal is out of view, for example on a tab.

The host gives the terminal what the user types or pastes, with hosho_terminal_typed(), when it sends it to the program; the widget does this by itself. The terminal does not show these bytes, but learns from them where the user's command starts (for a shell that did not send B) and that a command starts (for a shell that cannot send C). When the shell ends, or another program starts, the host calls hosho_terminal_shell_forget(), so the marks of the old shell say nothing about the new program.

3. A shell that cannot say that a command starts

Some shells have no way to run something before a command. Such a shell says so in each prompt: OSC 133 ; A ; hosho=infer. The terminal then takes Return typed after text at the prompt as the start of a command: text that waits on the screen, text typed a moment ago that the shell did not show yet, or text in the same bytes (a pasted line). Such a shell sends D with each prompt, also when no command ran (Return on an empty line); a D with no command started is then no result.

4. The files for each shell

The folder shell/ has a file for each shell; make install puts it in /usr/share/hosho/shell/. hosho_pseudoterminal_shell_start(shell, folder, &start) gives the arguments and variables that make a shell read its file, so the user changes no file of their own, and hosho_widget_start_shell() uses it. It returns 1 when the start reads the integration, and 0 when it is the shell alone (a shell with no integration, a folder that is NULL, "" or too long for the room in HoshoShellStart, or a file that is missing). A shell that is not given as a full path is the user's shell. Each file reads the user's own files first, so the shell is as the user knows it, and loads only once, in an interactive shell.

Shell How it reads the file What it sends
bash bash --rcfile shell/hosho_rcfile.bash. That file reads ~/.bashrc, then hosho.bash. All. D, OSC 7 and A come from the first of the prompt commands (PROMPT_COMMAND), so $? is still the command's status; B is added to the end of PS1 by the last prompt command, so a prompt that is made new each time keeps it; C comes from PS0.
zsh ZDOTDIR=shell/zsh, with the user's own ZDOTDIR in HOSHO_USER_ZDOTDIR. The .zshenv there gives ZDOTDIR back, reads the user's .zshenv, then hosho.zsh; zsh reads the user's other files as always. All. D, OSC 7 and A from the first precmd hook, C from the first preexec hook, and B when the line editor starts, so a prompt theme cannot lose it.
fish fish --init-command "source '.../hosho.fish'". fish runs it after its own files (config.fish), before the first prompt. All. fish 4 and later sends A, C, D and OSC 7 by itself, and the file adds only B, at the end of the user's prompt. For an older fish the file sends them all.
ksh93 ENV=shell/hosho.sh, with the user's own ENV in HOSHO_USER_ENV; hosho.sh reads hosho.ksh93. When the user had no ENV, ksh93 reads ~/.kshrc first, as it does by itself. All. The marks are in PS1; the DEBUG trap of hosho.ksh93 sends C before the first command of the line.
mksh, OpenBSD's ksh, dash, the sh of FreeBSD and NetBSD ENV=shell/hosho.sh, as for ksh93. When the user had no ENV, mksh reads ~/.mkshrc first. A (with hosho=infer), B, D and OSC 7, in PS1; not C (section 3). NetBSD's sh does not send B either.
tcsh Not by itself: tcsh reads only the files in the home folder. Add source /usr/share/hosho/shell/hosho.tcsh at the end of ~/.tcshrc. hosho_pseudoterminal_shell_start() starts tcsh alone. A, D and OSC 7 from the precmd alias, C from postcmd when the history has a new line. Not B. Aliases of these names that exist already are kept and run after.

The shells are known by the name of their program: bash, zsh, fish, and for ENV ksh, ksh93, mksh, lksh, oksh, dash and sh. When sh is bash, hosho.sh reads hosho.bash. bash started this way is not a login shell: it reads ~/.bashrc, not ~/.bash_profile.

To use a file in another terminal, or with a shell started another way, read it from the shell's own file, for example source /usr/share/hosho/shell/hosho.bash at the end of ~/.bashrc, source .../hosho.zsh in ~/.zshrc, source .../hosho.fish in ~/.config/fish/config.fish, or . .../hosho.sh at the end of the file ENV names. To try it with the demo and change no file: demo/hosho_demo bash --rcfile shell/hosho_rcfile.bash.

5. What a host does with the marks

The marks are on the lines (HoshoLine.mark, status, input_column), and hosho_text_block_at() gives the lines of one command: its prompt, where its output starts, its last line, and whether it ended. The widget uses them for these:

  • a mark beside each prompt, in the theme's color for a command that ended well, failed, or still runs;
  • a background for each prompt's line (prompt-line) and for the output of a failed command (output-failure), when the theme gives one;
  • Control+Shift+Up and Control+Shift+Down to go from prompt to prompt;
  • a click on a mark selects the command's output, two clicks hide or show it, and the menu copies it;
  • a notification when a command that ran long ends while the terminal does not have the keyboard;
  • the shell's directory for file names in a compiler's output, and for a new terminal in the same place;
  • the state, for a host that shows it on a tab.

6. Known limitations

  • The files were checked with bash 5.2, zsh 5.9, fish 4.0, ksh93u+m 1.0, mksh R59, tcsh 6.24 and dash 0.5.12. The parts for OpenBSD's ksh and the sh of FreeBSD and NetBSD were written from the source of those shells and were not run there.
  • tcsh and NetBSD's sh do not say where the prompt ends; the place is the cursor's at the first typed character.