Shell prompts and tricks

Many interactive REPL-style programs (such as a “shell") allow you to customize the string used to prompt for input. Putting certain DomTerm-specific escape sequences in the prompt enables some nice features.

The following discussion tests $DOMTERM to decide whether to do domterm-specific actions. See how to more reliably test for DomTerm.

Setting the Bash shell prompt

Download shell-integration.bash and bash-preexec.sh. Then in your ~/.bashrc put something like:

if [ "$PS1" != "" ]
then
  # Optionally override the system default - for all terminals
  PS1='$ ' # or whatever

  if [ -n "$DOMTERM" ]
  then
    source /path/to/bash-preexec.sh
    source /path/to/shell-integration.bash
  fi
fi

This causes the prompt to have the prompt style (specifically to be in a <span std="prompt"> element), while the remainder of the current line gets the input style (specifically, in a <span std="input"> element). The appearance of these styles can be customized with CSS stylesheets. Furthermore, this prompt enables text folding: a hide/show button (click on the character), which hides/shows the output from the command.

Setting the Zsh shell prompt

Zsh is similar to bash. Download shell-integration.zsh. Then in your ~/.zshrc put something like:

if [ "$PS1" != "" ]
  # Maybe change PS1 from the default
then
  if [ -n "$DOMTERM" ]
  then
     source /path/to/shell-integration.zsh
  fi
fi

Setting the Fish Shell (fish) prompt

For fish (the Fish Shell), copy shell-integration.fish into the Fish configuration directory $__fish_config_dir (which defaults to .config/fish). Then place the following in the main fish configuration file $__fish_config_dir/config.fish:

if test -n "$DOMTERM"
  source $__fish_config_dir/shell-integration.fish
end

Tracking current working directory

It is useful for DomTerm to track the working directory of the process. One reason is creating links from compiler error messages.

If you’re using the Bash shell, you can set the PROMPT_COMMAND to send a special escape sequence, like the following.

# Based on Orwellophile's answer to
# https://stackoverflow.com/questions/296536/how-to-urlencode-data-for-curl-command
# adding the LC_ALL=C trick from /etc/profile.d/vte.sh (on Fedora27)
print_path_url() {
  local LC_ALL=C
  local string="$PWD"
  local strlen=${#string}
  local encoded=""
  local pos c o

  for (( pos=0 ; pos<strlen ; pos++ )); do
     c=${string:$pos:1}
     case "$c" in
        [-_.~a-zA-Z0-9/] ) o="${c}" ;;
        * )               printf -v o '%%%02x' "'$c"
     esac
     encoded+="${o}"
  done
  printf "\033]7;file://%s%s\007" "${HOSTNAME:-}" "${encoded}"
}
test "$PROMPT_COMMAND" = __vte_prompt_command || \
    PROMPT_COMMAND="$PROMPT_COMMAND;print_path_url"

(On some platforms Gnome Terminal loads vte.sh which sets PROMPT_COMMAND to __vte_prompt_command, which sends the same escape sequence as print_path_url.)

Directory tracking of make

Sometimes make will recurse into sub-directories. Error message in those sub-directories may be relative. The following make wrapper causes make to report to DomTerm the current directory, so it can resolve relative files names to absolsute file: links.

BASE_MAKE=/usr/bin/make
if test -t 1 && is_domterm
then
  print_path_url
  export MAKE=`command -v $0`
  $BASE_MAKE "$@"
  ex=$?
  print_path_url
  exit $ex
else
  $BASE_MAKE "$@"
fi

This uses the is_domterm and print_path_url functions defined above.