#9 Add more information about the Git files in prompt

Closed
opened 4 years ago by tukusejssirs · 7 comments

We could add more refined info in the prompt when CWD is in a Git repository, see below.

gss="$(git status --porcelain=1 2>/dev/null)"

# All changes (might be useful in the script, but should not be displayed in the prompt)
# Note that when I used `wc -l <<< "$gss"`, it has output +1 (at least when there where no changes)
file_changes=$(echo -n $gss | wc -l)

file_deleted=$(grep -c '^D' <<< "$gss")
file_untracked=$(grep -c '^??' <<< "$gss")
file_added=$(grep -c '^A ' <<< "$gss")
file_added_remodified=$(grep -c '^AM' <<< "$gss")
file_added_all=$(grep -c '^A\|^AM' <<< "$gss")
file_modified_nonstaged=$(grep -c '^ M' <<< "$gss")
file_modified_staged=$(grep -c '^M ' <<< "$gss")
file_modified_staged_remodified=$(grep -c '^MM' <<< "$gss")
file_modified_all=$(grep -c '^MM\|^M \|^ M' <<< "$gss")
file_renamed=$(grep -c '^R ' <<< "$gss")
file_renamed_remodified=$(grep -c '^RM' <<< "$gss")

Suggested output order

[M]↓1↑1MM1M-1M+1A1AM1D1?1

Example output (screenshot; without $GBehind/$GAhead obviously).

Also note that we should redefine local GChanges="$(echo -n "$gss" | wc -m)", as currently when there are (for example) some untracked files, the branch name is not in red.

We could add more refined info in the prompt when `CWD` is in a Git repository, see below. ```bash gss="$(git status --porcelain=1 2>/dev/null)" # All changes (might be useful in the script, but should not be displayed in the prompt) # Note that when I used `wc -l <<< "$gss"`, it has output +1 (at least when there where no changes) file_changes=$(echo -n $gss | wc -l) file_deleted=$(grep -c '^D' <<< "$gss") file_untracked=$(grep -c '^??' <<< "$gss") file_added=$(grep -c '^A ' <<< "$gss") file_added_remodified=$(grep -c '^AM' <<< "$gss") file_added_all=$(grep -c '^A\|^AM' <<< "$gss") file_modified_nonstaged=$(grep -c '^ M' <<< "$gss") file_modified_staged=$(grep -c '^M ' <<< "$gss") file_modified_staged_remodified=$(grep -c '^MM' <<< "$gss") file_modified_all=$(grep -c '^MM\|^M \|^ M' <<< "$gss") file_renamed=$(grep -c '^R ' <<< "$gss") file_renamed_remodified=$(grep -c '^RM' <<< "$gss") ``` Suggested output order ```bash [M]↓1↑1MM1M-1M+1A1AM1D1?1 ``` Example output (screenshot; without `$GBehind`/`$GAhead` obviously). <img src="https://i.imgur.com/Xci3QAI.png" width="60%" height="60%"> Also note that we should redefine `local GChanges="$(echo -n "$gss" | wc -m)"`, as currently when there are (for example) some untracked files, the branch name is not in red.
demure commented 4 years ago
Owner

Have you considered using grep -c to remove the need for the | wc -l? (trying to make sure it matches your expected results)

What is the $file_changes testing for? When I run the echo -n $gss | wc -l I always get 0 returned regardless of the git dir state (on void linux).

Personally this seems like more data than needed, but I do like that it is more succinct than the current iteration. Do you normally track the three different modified states? If I include the four modified trackers, would you object to me doing some fancy code so that it defaults to only file_modified_all if there are more than say... 5 or 10 modified? Just feels like it would be cluttered otherwise.

I'll probably need to consider some common-ish unicode symbols to pair with this as all the letters seems unaesthetical >__<

Have you considered using `grep -c` to remove the need for the ` | wc -l`? (trying to make sure it matches your expected results) What is the `$file_changes` testing for? When I run the `echo -n $gss | wc -l` I ***always*** get `0` returned regardless of the git dir state (on void linux). Personally this seems like more data than needed, but I do like that it is more succinct than the current iteration. Do you normally track the three different modified states? If I include the four modified trackers, would you object to me doing some fancy code so that it defaults to only file_modified_all if there are more than say... 5 or 10 modified? Just feels like it would be cluttered otherwise. I'll probably need to consider some common-ish unicode symbols to pair with this as all the letters seems unaesthetical >__<
tukusejssirs commented 4 years ago
Poster

You’re right about grep -c—I have totally forgotten it! I have changed it in the issue description.

$file_changes is unnecessary. I use it to output a space between the branch name and the changes list (a space is output only when there are some changes).

I always get 0 returned regardless of the git dir state (on void linux).

Well, I have no idea what happens on your system (I have never used void linux), but $gss contains list of changed files (one per a row each prepended with the change type), so I presume, there should be no problem. You might check man echo on your system if it describes -n option as do not output the trailing newline (without -n wc would count the trailing newline as an extra line and therefore an extra file). BTW, I use GNUecho. And finally, there is thewc -lwhich counts the lines of the text supplied fromstdin. Again, check theman wcif the-loption is described asprint the newline counts. And again, I use GNUwc. All in all,echo -n $gss | wc -l` is for counting how many changes are in the local repo currently, regardless of the change type.

Do you normally track the three different modified states?

I do track of these states. :smiley: But on the other hand, if you don’t like it, just don’t add it or just modify it to your liking. I have already made a fork or your prompt and modified the bits to my liking. I just wanted to contribute back something. :smiley:

[W]ould you object to me doing some fancy code … ?

No, I would not. Do as you wish. I like to know exactly how many and what kind of changes are in a specific repo. And I don’t care if it is a it lengthy, as I use a double-line PS1 prompt (\n right before \$).

I'll probably need to consider some common-ish unicode symbols to pair with this as all the letters seems unaesthetical >__<

Yes, that might be a good idea. I just wanted to be as close to Git style as possible. :smiley:

You’re right about `grep -c`—I have totally forgotten it! I have changed it in the issue description. `$file_changes` is unnecessary. I use it to output a space between the branch name and the changes list (a space is output only when there are some changes). > I always get `0` returned regardless of the git dir state (on void linux). Well, I have no idea what happens on your system (I have never used void linux), but `$gss` contains list of changed files (one per a row each prepended with the change type), so I presume, there should be no problem. You might check `man echo` on your system if it describes `-n` option as `do not output the trailing newline` (without `-n` `wc would count the trailing newline as an extra line and therefore an extra file). BTW, I use GNU `echo`. And finally, there is the `wc -l` which counts the lines of the text supplied from `stdin`. Again, check the `man wc` if the `-l` option is described as `print the newline counts`. And again, I use GNU `wc`. All in all, `echo -n $gss | wc -l` is for counting how many changes are in the local repo currently, regardless of the change type. > Do you normally track the three different modified states? I do track of these states. :smiley: But on the other hand, if you don’t like it, just don’t add it or just modify it to your liking. I have already made a fork or your `prompt` and modified the bits to my liking. I just wanted to contribute back something. :smiley: > [W]ould you object to me doing some fancy code … ? No, I would not. Do as you wish. I like to know _exactly_ how many and what kind of changes are in a specific repo. And I don’t care if it is a it lengthy, as I use a double-line `PS1` prompt (`\n` right before `\$ `). > I'll probably need to consider some common-ish unicode symbols to pair with this as all the letters seems unaesthetical >__< Yes, that might be a good idea. I just wanted to be as close to Git style as possible. :smiley:
demure commented 4 years ago
Owner

I've done my first round of refactoring: c1f7daaad1

I'll look at adding indicators for deletion/addition later. Zzz

I've done my first round of refactoring: https://notabug.org/demure/dotfiles/commit/c1f7daaad1e69483a0b5e91ceb5bd3d0c64b02f2 I'll look at adding indicators for deletion/addition later. Zzz
tukusejssirs commented 4 years ago
Poster

It looks nice. :)

It looks nice. :)
tukusejssirs commented 4 years ago
Poster

I have just added two new file statuses (I have forgot about them) to the issue description. I add them in this comment too, just to make it obvious.

file_renamed=$(grep -c '^R ' <<< "$gss")
file_renamed_remodified=$(grep -c '^RM' <<< "$gss")

Update: I have reversed the commands. :smiley:

I have just added two new file statuses (I have forgot about them) to the issue description. I add them in this comment too, just to make it obvious. ```bash file_renamed=$(grep -c '^R ' <<< "$gss") file_renamed_remodified=$(grep -c '^RM' <<< "$gss") ``` **Update:** I have reversed the commands. :smiley:
tukusejssirs commented 4 years ago
Poster

I have just noticed that when the remote branch (that is tracked by local branch currently checked out) has diverged from the local branch, we try to merge it by pulling the changes (git pull), and there are some conflicts, there are some additional ‘shortcuts’ in the git status -s output:

UU for both modified
AA for both added

I leave it to you if you want to include these in your output. :smiley:

I have just noticed that when the remote branch (that is tracked by local branch currently checked out) has diverged from the local branch, we try to merge it by pulling the changes (`git pull`), and there are some conflicts, there are some additional ‘shortcuts’ in the `git status -s` output: ``` UU for both modified AA for both added ``` I leave it to you if you want to include these in your output. :smiley:
demure commented 4 years ago
Owner

With 5cc51d1e09 I've added an indicator for added/deleted/moved. I'm not sure if I'll add the finer levels of nuance or not at this point.

With https://notabug.org/demure/dotfiles/commit/5cc51d1e099b1029f867e42e76b73e1749b138e5 I've added an indicator for added/deleted/moved. I'm not sure if I'll add the finer levels of nuance or not at this point.
Sign in to join this conversation.
No Milestone
No assignee
2 Participants
Loading...
Cancel
Save
There is no content yet.