Description
Modification of Prompt string to display useful details in PASE terminal.
Simple Method:
By modifying the PS1
environment variable, we can we can display useful information like
username, server name, current working directory etc., like below
This would help the users to know exactly where they're currently located at without the need to issue a pwd
command.
We only need to create a .profile
file in the user's home directory and place the code below
echo 'PS1="\[\e[32m\]\u\[\e[0m\]@\[\e[32m\]\h\[\e[33m\]:\w\[\e[0m\]\$ "' >> ~/.profile
Advanced Method:
By modifiying the PROMPT_COMMAND
, we can display the git status on the prompt string.
When I was learning Git, I modified the powershell terminal to reflect the git status of my current working git directory. There was an application called posh git which would show us the git status on the prompt itself like below. I was trying to replicate the same in PASE. Then I came to know about the __posh_git_ps1
shell function which can output the Git Status in an easily understandable string format.
This means, I am currently on the folder called gitrepo
which is a git repository.
The cyan master = Branch name
? = No remote repository configured
The green +1 = One file is tracked and it is newly added
The red +2 = Two files are added and untracked
Step-1:
First, get the git-prompt.sh
shell script that can evaluate the git status. You can read more about this script here.
/QOpenSys/pkgs/bin/wget --show-progress https://raw.githubusercontent.com/lyze/posh-git-sh/refs/heads/master/git-prompt.sh -O .git-prompt.sh
Step-2:
Setup the open source path variable in .profile
file. This is required or the git-prompt.sh
to work correctly.
echo "export PATH=/QOpenSys/pkgs/bin:$PATH" >> .profile
Step-3:
Setup the PROMPT_COMMAND
to reflect the git status. Run the three commands one by one. Know that the PROMPT_COMMAND
is a special shell variable that gets executed each time before the shell's primary prompt (PS1) is displayed.
echo "PROMPT_COMMAND='__posh_git_ps1 \"\${VIRTUAL_ENV:+(\`basename \$VIRTUAL_ENV\`)}\\[\\e[32m\\]\\u\\[\\e[0m\\]@\\h:\\[\\e[33m\\]\\w\\[\\e[0m\\] \" \"\\\\\\\$ \";'\$PROMPT_COMMAND" >> .profile
echo "source ~/.git-prompt.sh" >> .profile
source ~/.profile
How it works?
We are instructing the PROMPT_COMMAND
to call the git-prompt.sh
script everytime the prompt is displayed.
The function __posh_git_ps1
takes two parameters (__posh_git_ps1 <prefix> <suffix>
), and sets PS1
to <prefix><status><suffix>
.
Click here to read the full legend of the Git Prompt
[{HEAD-name} x +A ~B -C !D | +E ~F -G !H W]
{HEAD-name}
is the current branch, or the SHA of a detached HEAD. The color
of{HEAD-name}
represents the divergence from upstream.{HEAD-name}
also
changes to indicate progress if you are in the middle of a cherry-pick, a
merge, a rebase, etc.cyan
the branch matches its remotegreen
the branch is ahead of its remote (green light to push)red
the branch is behind its remoteyellow
the branch is both ahead of and behind its remote
x
is a symbol that represents the divergence from upstream.≡
the branch matches its remote↑
the branch is ahead of its remote↓
the branch is behind its remote↕
the branch is both ahead of and behind its remote
- Status changes are indicated by prefixes to
A
throughH
, whereA
through
D
represent counts for the index andE
throughH
represent counts for
the working directory. As ingit status
, index status is dark green and
working directory status is dark red.+
added~
modified-
removed!
conflicting
W
represents the overall status of the working directory.!
there are unstaged changes in the working tree~
there are uncommitted changes, i.e. staged changes, in the working tree
waiting to be committed- None: there are no unstaged or uncommitted changes to the working tree
For example, a status of [master ≡ +0 ~2 -1 | +1 ~1 -0 !]
corresponds to the
following git status
:
# On branch master
#
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage)
#
# modified: this-changed.txt
# modified: this-too.txt
# deleted: gone.txt
#
# Changed but not updated:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: not-staged.txt
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# new.txt
Final Thoughts
Instead of this line can we execute the above steps to display useful information on the PASE terminal prompt?
I have a working demo of both the methods which I can explain on the next code-for-i Friday meeting.
Thanks,
Ravi.