Skip to content

Modification of Prompt string to display useful details in PASE terminal #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ravisankar-PIO opened this issue Jan 2, 2025 · 3 comments
Assignees
Labels
enhancement New feature or request

Comments

@ravisankar-PIO
Copy link

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
image

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.

image
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>.

By default, the status summary has the following format: 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 remote
    • green the branch is ahead of its remote (green light to push)
    • red the branch is behind its remote
    • yellow 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 through H, where A through
    D represent counts for the index and E through H represent counts for
    the working directory. As in git 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.

@sebjulliand sebjulliand transferred this issue from codefori/vscode-ibmi Jan 2, 2025
@sebjulliand sebjulliand added the enhancement New feature or request label Jan 2, 2025
@sebjulliand
Copy link
Contributor

Very interesting topic @ravisankar-PIO !
However, I transferred it here as I think it should be a guideline rather than something automatically enforced by Code for i. What's displayed in the terminal prompt is a mater of personal choice.

Feel free to create a PR in this repository to add a topic in the Tips & Tricks section that explains the prompt customization as you did in this issue. It's quite valuable!

@ravisankar-PIO
Copy link
Author

Merci @sebjulliand. Do you think this article is comprehensive enough to add into tips section? Or do you want me to elaborate any section in detail?

@sebjulliand
Copy link
Contributor

Looks good to me! Just rephrase it so it fits in a documentation, and maybe add a link to the list of available xterm color codes and to the available escape sequences - just so people know what they can use to customize their prompt 😉

And again, very good topic! 👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants