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

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 $DOMTERM_RESOURCE_DIR/tools/shell-integration.bash
    source $DOMTERM_RESOURCE_DIR/tools/bash-preexec.sh
  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. In your ~/.zshrc put something like:

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

Setting the Fish Shell (fish) prompt

For fish (the Fish Shell), place the following in the main fish configuration file $__fish_config_dir/config.fish:

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

Command-completion (tab completion) for Bash

Command completion enables you to type an initial part of a command, and have the computer “fill in” the next part. Completion is also called tab completion, because it its commonly requested by typing Tab.

To set up tab-completion in bash for the domterm command execute the following command (perhaps in your ~/.bashrc):

complete -o nospace -C  'domterm "#complete-for-bash" "$COMP_LINE" "$COMP_POINT"' domterm

This instructs bash when completing the domterm command it should invoke the domterm executable with certain options that cause a list of candidate completions to be printed. (Delegating command-completion to the domterm command itself makes it easier to keep completion consistent with the command.)

There is currently no completion support for other shells such as fish or zsh.

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.