sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.34k stars 460 forks source link

Pimp my Shell #15590

Open vbraun opened 10 years ago

vbraun commented 10 years ago

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.

CC: @novoselt @videlec @orlitzky

Component: scripts

Author: Volker Braun

Branch/Commit: u/jhpalmieri/pimp_my_shell @ 58fa20d

Issue created by migration from https://trac.sagemath.org/ticket/15590

vbraun commented 10 years ago

Branch: u/vbraun/pimp_my_shell

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Commit: 2678e8d

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

2678e8dalso display staged as red
vbraun commented 10 years ago

Author: Volker Braun

vbraun commented 10 years ago

Description changed:

--- 
+++ 
@@ -1 +1 @@
-
+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.
vbraun commented 10 years ago

Description changed:

--- 
+++ 
@@ -1 +1,13 @@
 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.
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

f2b46b4support for old git versions
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from 2678e8d to f2b46b4

jhpalmieri commented 10 years ago
comment:8

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

diff --git a/src/bin/sage b/src/bin/sage
index c01fea2..8770a57 100755
--- a/src/bin/sage
+++ b/src/bin/sage
@@ -278,6 +278,21 @@ if [ "$1" = '-sh' -o "$1" = '--sh' ]; then
             export ZDOTDIR="$SAGE_EXTCODE/shell"
             set -- "$SHELL" -d "$@"
             ;;
+        sh)
+            # see http://stackoverflow.com/a/3327022/1401572
+            if [ -n $BASH ]; then
+                set -- "$SHELL" --rcfile "$SAGE_EXTCODE/shell/bashrc" "$@"
+            elif [ -n $ZSH_NAME ]; then
+                export ZDOTDIR="$SAGE_EXTCODE/shell"
+                set -- "$SHELL" -d "$@"
+            elif [ -n $KSH_VERSION ]; then
+               export ENV="$SAGE_EXTCODE/shell/kshrc"
+                set -- "$SHELL" "$@"
+            else
+               source "$_SAGE_ENV_SCRIPT"
+                set -- "$SHELL" "$@"
+            fi
+            ;;
         *)
            # We don't know how to specify alternate startup file, you are on your own
            source "$_SAGE_ENV_SCRIPT"

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

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

1a8fefcworkaround for lack of "echo -e" on OSX
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from f2b46b4 to 1a8fefc

vbraun commented 10 years ago
comment:10

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.

jhpalmieri commented 10 years ago
comment:11

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

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

fb5b848more workarounds for weird $SHELL, OSX ksh
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from 1a8fefc to fb5b848

jhpalmieri commented 10 years ago
comment:13

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

jhpalmieri commented 10 years ago
comment:14

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?

vbraun commented 10 years ago
comment:15

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.

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

232ef94no guessing what SHELL is
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from fb5b848 to 232ef94

jhpalmieri commented 10 years ago
comment:17

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
vbraun commented 10 years ago
comment:18

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.

jhpalmieri commented 10 years ago
comment:19

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
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

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

d1f9154hook to supply a user DOT_SAGE/bashrc
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 10 years ago

Changed commit from 232ef94 to d1f9154

vbraun commented 10 years ago
comment:21

Sounds good to me...

ppurka commented 9 years ago
comment:24

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%}"
saraedum commented 8 years ago

Work Issues: merge conflicts

jhpalmieri commented 8 years ago

Changed branch from u/vbraun/pimp_my_shell to u/jhpalmieri/pimp_my_shell

jhpalmieri commented 8 years ago
comment:27

Rebased.


New commits:

58fa20dtrac 15590: custom shell prompts for 'sage --sh', rebased
jhpalmieri commented 8 years ago

Changed commit from d1f9154 to 58fa20d

saraedum commented 8 years ago

Changed work issues from merge conflicts to none

saraedum commented 8 years ago
comment:29

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

dimpase commented 5 years ago
comment:30

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

jhpalmieri commented 5 years ago
comment:31

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.

embray commented 5 years ago
comment:33

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

jhpalmieri commented 5 years ago
comment:34

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.

dimpase commented 5 years ago
comment:35

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

jhpalmieri commented 5 years ago
comment:36

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

orlitzky commented 2 years ago
comment:38

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.