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 focuses on the bash shell.

Detecting domterm terminal

The DOMTERM environment variable is normally set when running under DomTerm. However, using DOMTERM to determine if the current terminal is DomTerm is not reliable: If you ssh, DOMTERM will not be automatically set. On the other hand, if you start some other terminal emulator like xterm from within DomTerm, the xterm will inherit the DOMTERM variable will be misleadingly set.

So it is better to probe the terminal itself. You can use the domterm is-domterm command:

if domterm is-domterm
then
  execute domterm-specific actions
fi

If you’re not sure whether the domterm program will be in the PATH, you can use the following shell code:

probe_domterm() {
  # probe if TERM unset, or contains "xterm", or DOMTERM is set
  case "$TERM/$DOMTERM" in
    /* | *xterm*/* | */?*)
      echo -en "\e[>0c" >/dev/tty
      read -s -t 1 -d c </dev/tty
      case "${REPLY}" in
        ?"[>990;"*";"*) DOMTERM_REPLY="${REPLY}" ;;
        "") DOMTERM_REPLY="-timeout)" ;;
        *) DOMTERM_REPLY="-non-match"
      esac
      ;;
    *) DOMTERM_REPLY="-not-tried"
  esac
}

is_domterm() {
  test -z "$DOMTERM_REPLY" && probe_domterm
  case "$DOMTERM_REPLY" in
    -*) return -1;;
    *) return 0;;
  esac
}

if is_domterm
then
  execute domterm-specific actions
fi

The is_domterm function only tests if the current terminal is domterm; it does not check if standard input/output have been re-directed. To check that, you can use the test builtin with the -t option:

if test -t 0 -a -t 1 -a -t 2 && is_domterm
then
  execute domterm-specific actions
fi

Setting the Bash shell prompt

You can place the following in your ~/.bashrc to take advantage of DomTerm features:

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

  # Check if the DOMTERM variable includes the string "tty=ttyname"
  # where ttyname is the output from the tty command
  case ";$DOMTERM;" in
    *";tty=`tty`;"*) ;;
    *";tty="*) unset DOMTERM;;
  esac

  if [ -n "$DOMTERM" ]
  then
    # Add some DomTerm-specific escape sequences
    PS1='\[\e]119;'$BASHPID'\a\e[14u\e[16u\]▼\[\e[17u\]'$PS1'\[\e[15u\]'
    PS2='\[\e[14u\]'$PS2'\[\e[18u\]'
  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.

See the Wire byte protocol section for the details.

(The "\[" and "\]" are bash syntax, and are equivalent to readline’s "\001" and "\002". They are used to indicate escape sequences that don’t move the cursor, which is needed for readline to calculate the column position.)

Setting the Zsh shell prompt

For zsh, the syntax is slightly different from bash, but it’s the same basic idea. Putting the following into you ~/.zshrc works:

if [ "$PS1" != "" ]
then
  if [ -n "$DOMTERM" ]
  then
     ZSHPID=$$
     PS1=$'%{\e]119;zsh'$ZSHPID$'\a\e[14u\e[16u%}▼%{\e[17u%}'$PS1$'%{\e[15u\e]75;> \a%}'
  fi
fi

Setting the Fish Shell (fish) prompt

For fish (the Fish Shell) put the following in your ~/.config/fish/functions/fish_prompt.fish:

set FISHPID %self
function fish_prompt --description 'Write out the prompt'
  # generic prompt used for non-DomTerm terminals and as base for DomTerm
  set -l suffix '$ '
  set -l prompt (prompt_pwd)"$suffix"

  if test -n "$DOMTERM"
    set prompt '\e]119;fish'$FISHPID'\a\e[14u\e[16u▼\e[17u'$prompt'\e[15u\e]75;> \a'
  end
  echo -nes "$prompt"
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.