Skip to content

Commit

Permalink
Set up prompt in Docker
Browse files Browse the repository at this point in the history
Add output of current time and, more importantly, git status.
  • Loading branch information
antichris committed Jul 21, 2020
1 parent 2b26c66 commit 54c2357
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
6 changes: 6 additions & 0 deletions docker/Dockerfile
@@ -1,3 +1,8 @@
FROM ubuntu:20.04 AS prompt-setup

COPY prompt-setup /
RUN /prompt-setup /etc/skel/.bashrc

FROM ubuntu:20.04

RUN set -x \
Expand Down Expand Up @@ -33,6 +38,7 @@ RUN set -x \
WORKDIR /src

COPY make pull /
COPY --from=prompt-setup /etc/skel/.bashrc /etc/skel/.bashrc
COPY entrypoint /usr/bin/entrypoint

ENTRYPOINT ["entrypoint"]
Expand Down
69 changes: 69 additions & 0 deletions docker/prompt-setup
@@ -0,0 +1,69 @@
#!/bin/sh

dieWithUsage() {
local status=${1:-0} message="$2"
[ "$message" ] && printf %s\\n "$message" >&2
cat >&2 <<-EOD
Usage: $0 BASHRCFILE
Alter a .bashrc file to enhance the shell prompt display.
--help display this help and exit
Locates the PS1 prompt strings in a .bashrc file, enhancing them with the
output of last exit/error status and current time, and adds PROMPT_COMMAND and
GIT_PS1_* variables, to add Git status to the prompt and configure it.
EOD
exit "$status"
}
for arg; do
[ "$arg" = '--help' ] && dieWithUsage
done

if [ ! -f "$1" ]; then
dieWithUsage 1 'No BASHRCFILE specified'
fi
bashrc=$1

promptSetupComment='## The following lines have been added by the prompt-setup script.'

if grep --quiet "$promptSetupComment" "$bashrc"; then
printf 'The file "%s" appears to have already been altered\n' "$bashrc"
exit
fi

# shellcheck disable=SC2016
sed -Ei '
## For each line that starts with PS1=<singlequote>
/^\s+PS1='\''/{
## Move the dollar sign suffix to a separate variable
s/(^\s+)(\S.+)(\\\$ )('\'')$/\1\2\4\n\1PS1_suffix=\4\\n\3\4/;
## Prefix with last exit status (if non-zero) and clock
s/(^[^$]+)(\$)/\1$(e=$?;[ "$e" -eq 0 ]||echo "$e|")$(date +%T) \2/;
## For each line that contains "\[" (shell prefix for ANSI escape sequences)
/\\\[/{
## Dim the prompt dollar sign
s/(_.+)(\\\$)/\1\\[\\033[2m\\]\2\\[\\033[0m\\]/
## Set color to red for error number (bright) and its separator (dim)
s/(^.+o ")(\$e)(\|)(")/\1\\[\\033[1;31m\\]\2\\[\\033[2m\\]\3\\[\\033[0m\\]\4/;
## Dim the clock
s/(\$[^)]+\))( )/\\[\\033[2m\\]\1\\[\\033[0m\\]\2/;
## Add variable to enable colors in Git prompt
s/(^\s+)(\S.+$)/&\n\1GIT_PS1_SHOWCOLORHINTS=1/;
}
}' "$bashrc"

cat >>"$bashrc" <<-EOD
$promptSetupComment
GIT_PS1_SHOWDIRTYSTATE=1
GIT_PS1_SHOWSTASHSTATE=1
GIT_PS1_SHOWUNTRACKEDFILES=1
GIT_PS1_SHOWUPSTREAM='verbose name'
GIT_PS1_DESCRIBE_STYLE='branch'
GIT_PS1_HIDE_IF_PWD_IGNORED=1
PROMPT_COMMAND="__git_ps1 '\$PS1' '\$PS1_suffix'"
unset PS1_suffix
## End of content added by prompt-setup.
EOD

0 comments on commit 54c2357

Please sign in to comment.