Opened 8 years ago

Last modified 12 days ago

#15590 needs_work enhancement

Pimp my Shell

Reported by: vbraun Owned by:
Priority: major Milestone: sage-wishlist
Component: scripts Keywords:
Cc: novoselt, vdelecroix, mjo Merged in:
Authors: Volker Braun Reviewers:
Report Upstream: N/A Work issues:
Branch: u/jhpalmieri/pimp_my_shell (Commits, GitHub, GitLab) Commit: 58fa20d2a6f762002cb92d60fd6c564b7d2292a7
Dependencies: Stopgaps:

Status badges

Description (last modified by vbraun)

This patch reorganizes the "sage -sh" shell. If you are using bash, it also introduces a PS1 that includes git branch status. This can be very useful for beginners to keep an eye on the repository status.

Sage: desktop 02:53:17 {t/15590/pimp_my_shell|modified} ~/Sage/git
$ git add .
Sage: desktop 02:53:20 {t/15590/pimp_my_shell|staged} ~/Sage/git
$ git commit -m 'also display staged as red'
[t/15590/pimp_my_shell 2678e8d] also display staged as red
 1 file changed, 36 insertions(+), 30 deletions(-)
Sage: desktop 02:53:35 (t/15590/pimp_my_shell) ~/Sage/git
$ git st

but with colors.

Change History (38)

comment:1 Changed 8 years ago by vbraun

  • Branch set to u/vbraun/pimp_my_shell

comment:2 Changed 8 years ago by git

  • Commit set to 2678e8d56c40b882ce8cd08c31f484d2cffaeb6f

Branch pushed to git repo; I updated commit sha1. New commits:

2678e8dalso display staged as red

comment:3 Changed 8 years ago by vbraun

  • Authors set to Volker Braun
  • Component changed from PLEASE CHANGE to scripts
  • Description modified (diff)
  • Status changed from new to needs_review
  • Type changed from PLEASE CHANGE to enhancement

comment:4 Changed 8 years ago by vbraun

  • Description modified (diff)

comment:5 Changed 8 years ago by vbraun

  • Cc novoselt added

comment:6 Changed 8 years ago by git

  • Commit changed from 2678e8d56c40b882ce8cd08c31f484d2cffaeb6f to f2b46b47f05d899041c49c70b58d82d4b36eaf99

Branch pushed to git repo; I updated commit sha1. New commits:

f2b46b4support for old git versions

comment:7 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.1 to sage-6.2

comment:8 Changed 8 years ago by jhpalmieri

This is not working for me on OS X. On one machine, $SHELL is set to /bin/bash but I don't get an interesting prompt: I just get bash-3.2$. As far as I can tell, this is not loading my .bashrc file (I put an echo command in there to see, and nothing is printed). So I don't think it's loading src/ext/shell/bashrc, either. On another OS X machine, it's running bash but $SHELL is set to /bin/sh. I tried making the following changes, but it didn't help: the prompt remained sh-3.2$.

  • src/bin/sage

    diff --git a/src/bin/sage b/src/bin/sage
    index c01fea2..8770a57 100755
    a b if [ "$1" = '-sh' -o "$1" = '--sh' ]; then 
    278278            export ZDOTDIR="$SAGE_EXTCODE/shell"
    279279            set -- "$SHELL" -d "$@"
    280280            ;;
     281        sh)
     282            # see http://stackoverflow.com/a/3327022/1401572
     283            if [ -n $BASH ]; then
     284                set -- "$SHELL" --rcfile "$SAGE_EXTCODE/shell/bashrc" "$@"
     285            elif [ -n $ZSH_NAME ]; then
     286                export ZDOTDIR="$SAGE_EXTCODE/shell"
     287                set -- "$SHELL" -d "$@"
     288            elif [ -n $KSH_VERSION ]; then
     289               export ENV="$SAGE_EXTCODE/shell/kshrc"
     290                set -- "$SHELL" "$@"
     291            else
     292               source "$_SAGE_ENV_SCRIPT"
     293                set -- "$SHELL" "$@"
     294            fi
     295            ;;
    281296        *)
    282297           # We don't know how to specify alternate startup file, you are on your own
    283298           source "$_SAGE_ENV_SCRIPT"

(I added some echo commands here, too, and it is entering the appropriate case, the sh case with $BASH set.)

Last edited 8 years ago by jhpalmieri (previous) (diff)

comment:9 Changed 8 years ago by git

  • Commit changed from f2b46b47f05d899041c49c70b58d82d4b36eaf99 to 1a8fefce1cd993603926fdb486500b5f9f6172b1

Branch pushed to git repo; I updated commit sha1. New commits:

1a8fefcworkaround for lack of "echo -e" on OSX

comment:10 Changed 8 years ago by vbraun

You need to rebuild sage (run "make"), because the scripts are loaded from SAGE_ROOT/local/share/sage/ext/shell and not from SAGE_ROOT/src/ext/shell.

I think if $SHELL is not set to bash then you either explicitly don't want bash features used or your OS is old/weird and its doubtful that bash works as it should. Where did you see the SHELL=/bin/sh environment?

While trying this out I noticed that OSX doesn't support echo -e to expand \e, this is now also fixed.

comment:11 Changed 8 years ago by jhpalmieri

I saw SHELL=/bin/sh on one OS X 10.8 machine. It may have to do with how the sysadmins have set up that machine, but that's my shell there. I've also seen some references to this happening on other systems, for example in the stackoverflow answer I cited in my patch. (See also http://stackoverflow.com/q/5166657/1401572, and I'm sure I saw it other places.)

I'm trying again with the new version. Currently running make, which is taking a little while...

comment:12 Changed 8 years ago by git

  • Commit changed from 1a8fefce1cd993603926fdb486500b5f9f6172b1 to fb5b8482c2406802ab540abc2c2673f7d94ec836

Branch pushed to git repo; I updated commit sha1. New commits:

fb5b848more workarounds for weird $SHELL, OSX ksh

comment:13 Changed 8 years ago by jhpalmieri

On a machine with SHELL=/bin/bash, it works well. On the machine with SHELL=/bin/sh, it is entering the appropriate case ("bash)"), but the bashrc file in local/share/sage/ext/shell is not loaded. If I replace "$SHELL" with `command -v bash`, it is loaded — this is in the line

set -- "$SHELL" --rcfile "$SAGE_EXTCODE/shell/bashrc" "$@"

In this case, it works, but not perfectly: the prompt comes out as

\033]0;palmieri@jpalmieri: ~/Desktop/Sage_stuff/git/sage\007\c
Sage: jpalmieri 12:11:42 {new_shell|modified} ~/Desktop/Sage_stuff/git/sage
$

with the middle line appropriately colored. (On the first machine, the prompt looks better: it doesn't include the first line. It's running a slightly newer version of bash, 3.2.51 instead of 3.2.48; it's also running OS X 10.9 instead of 10.8, and as mentioned, it has SHELL=/bin/bash.)

comment:14 Changed 8 years ago by jhpalmieri

It may be that there is no good way to deal with bash if it is run as /bin/sh, I don't know. A different question, though: if I don't like the bash prompt defined like this, can I override it? Should we load $DOT_SAGE/bashrc or some similarly named file at the end of local/share/sage/ext/shell/bashrc?

comment:15 follow-up: Changed 8 years ago by vbraun

You don't have to use the sage shell at all. I never use it. But imho we should offer something nice for those that don't want to customize their own shell prompt.

But to be on the safe side I think we should just fall back to something safe if SHELL is not set to /bin/bash.

comment:16 Changed 8 years ago by git

  • Commit changed from fb5b8482c2406802ab540abc2c2673f7d94ec836 to 232ef94b02251c92f1cc509ded640089cabffbb2

Branch pushed to git repo; I updated commit sha1. New commits:

232ef94no guessing what SHELL is

comment:17 in reply to: ↑ 15 Changed 8 years ago by jhpalmieri

Replying to vbraun:

You don't have to use the sage shell at all. I never use it. But imho we should offer something nice for those that don't want to customize their own shell prompt.

My point is that in my .bashrc file, I've already customized my shell prompt for use with sage -sh, and these changes override my customizations. It seems bad to force a choice of prompt on users. (I'm talking about the machines where SHELL=/bin/bash, by the way.)


New commits:

232ef94no guessing what SHELL is

comment:18 Changed 8 years ago by vbraun

Thats what I'm talking about, if you like the way you have set up your shell then just don't use the Sage shell. This ticket doesn't change the fact that the Sage shell overrides the prompt, it just overrides it with a nicer prompt. I dont' see a reliable way to figure out if the prompt has already been changed away from the default, there are many different "defaults" that distros set up.

comment:19 Changed 8 years ago by jhpalmieri

Sometimes if I'm trying to debug on spkg, I want to go to local/var/tmp/sage/build/... and then run make or configure or whatever, and it's convenient to be running the Sage shell before doing this. That is, sage -sh is a useful tool. I don't want to be dissuaded from using it because I dislike the new prompt. So why not put something like this at the bottom of the proposed Sage bashrc file:

if [ -f "$DOT_SAGE/bashrc" ]; then
    . "$DOT_SAGE/bashrc"
fi

comment:20 Changed 8 years ago by git

  • Commit changed from 232ef94b02251c92f1cc509ded640089cabffbb2 to d1f915465475938ee8cb97987fe8db7a464b6a94

Branch pushed to git repo; I updated commit sha1. New commits:

d1f9154hook to supply a user DOT_SAGE/bashrc

comment:21 Changed 8 years ago by vbraun

Sounds good to me...

comment:22 Changed 8 years ago by vbraun_spam

  • Milestone changed from sage-6.2 to sage-6.3

comment:23 Changed 7 years ago by vbraun_spam

  • Milestone changed from sage-6.3 to sage-6.4

comment:24 Changed 7 years ago by ppurka

I use a much shorter function in my zshrc to set up the git status, the idea being that the shell should be fast in initialization and in returning the prompt. So, the only command that is run is git status and everything else is gleaned from that using shell operations. Also, I use RPROMPT so that the left prompt is short. The RPROMPT disappears automatically once the user starts typing too long commands.

function __check_git__() {
    local _s="$( git status 2>&1 )"
    [[ "${_s}" = "fatal:"* ]] && echo "" && return
    _s="$( echo "${_s}" | sed -e '/^#.*modified:.*(untracked content)$/d' )"
    local _g_s="$( echo "$_s" | sed -n -e '1s@^On branch @@p;q' )"

    if [[   "$_s" = *modified:*                 || \
            "$_s" = *"new file:"*               || \
            "$_s" = *"Your branch is behind"*   || \
            "$_s" = *deleted:*                  || \
            "$_s" = *renamed:*                     \
         ]]; then
        echo "[%20>...>%{$fg[red]%}$_g_s%{$fg[white]%}%>>]"
    elif [[ -n "$_g_s" ]]; then
        echo "[%20>...>%{$fg[green]%}$_g_s%{$fg[white]%}%>>]"
    else
        echo "%{$fg[yellow]%}[?]%{$fg[white]%}"
    fi
}

RPROMPT="%{$fg[white]%}•$(__check_git__)%{$fg[cyan]%}%n%{$fg[white]%}|%{$fg[green]%}%m%{$reset_color%}"

comment:25 Changed 6 years ago by saraedum

  • Status changed from needs_review to needs_work
  • Work issues set to merge conflicts

comment:26 Changed 6 years ago by jhpalmieri

  • Branch changed from u/vbraun/pimp_my_shell to u/jhpalmieri/pimp_my_shell

comment:27 Changed 6 years ago by jhpalmieri

  • Commit changed from d1f915465475938ee8cb97987fe8db7a464b6a94 to 58fa20d2a6f762002cb92d60fd6c564b7d2292a7
  • Status changed from needs_work to needs_review

Rebased.


New commits:

58fa20dtrac 15590: custom shell prompts for 'sage --sh', rebased

comment:28 Changed 6 years ago by saraedum

  • Work issues merge conflicts deleted

comment:29 Changed 5 years ago by saraedum

Somehow, I do not get colors here but only an inverted prompt. It seems that tput colors does not return 0 whereas tput rev does. That is strange since tput colors does return 0 if I invoke it manually. I have not had a chance to debug this further. The changes seem fine to me and I think they could be a positive review even if it does not work for me (maybe my setup is just somehow screwed up.)

comment:30 Changed 3 years ago by dimpase

This still is set as open for review. Is it so?

comment:31 Changed 3 years ago by jhpalmieri

I don't think I will work on it, but feel free to take a look if it interests you. If it looks worthwhile, maybe it will revive my interest.

comment:32 Changed 3 years ago by dimpase

  • Milestone changed from sage-6.4 to sage-wishlist

comment:33 Changed 2 years ago by embray

It definitely looks neat. Is there any serious problem remaining on this, or just bikeshedding about how to support every shell under the sun?

comment:34 Changed 2 years ago by jhpalmieri

No one has touched it in 3 years, but I think it was in okay shape then. It might be better to do this one shell at a time, letting people who actually use ksh, zsh, etc., implement and test. I don't care that much about it, in any case.

comment:35 follow-up: Changed 2 years ago by dimpase

The naming scheme looks inconsistent:

src/ext/shell/.zshrc	
src/ext/shell/bashrc
src/ext/shell/kshrc

Any reason for it?

As to zsh, it is coming in macos 10.15, so it's getting more users soon...

comment:36 in reply to: ↑ 35 Changed 2 years ago by jhpalmieri

Replying to dimpase:

The naming scheme looks inconsistent:

src/ext/shell/.zshrc	
src/ext/shell/bashrc
src/ext/shell/kshrc

Any reason for it?

It was like 5 years ago. I have no idea.

As to zsh, it is coming in macos 10.15, so it's getting more users soon...

comment:37 Changed 2 years ago by saraedum

  • Cc vdelecroix added

comment:38 Changed 12 days ago by mjo

  • Cc mjo added
  • Status changed from needs_review to needs_work

I rather like my PS1 the way it is; this should be optional at a minimum.

Ultimately I don't think we should be adding more internet bling bling into the sagemath codebase at this point. Anyone who wants this can enable it himself by following e.g.

https://git-scm.com/book/uz/v2/Appendix-A%3A-Git-in-Other-Environments-Git-in-Bash

which is "trivial" to do and works in zsh as well.

Note: See TracTickets for help on using tickets.