From 3d4ee814546af5347f3c7665b71293b1a208b0eb Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 12 Aug 2022 19:05:56 -0300 Subject: etc/{bash => sh}/vcs-ps1.sh: Add shell_status_jobs and shell_status_level --- etc/bash/vcs-ps1.sh | 171 -------------------------------------------- etc/sh/vcs-ps1.sh | 200 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 200 insertions(+), 171 deletions(-) delete mode 100644 etc/bash/vcs-ps1.sh create mode 100644 etc/sh/vcs-ps1.sh diff --git a/etc/bash/vcs-ps1.sh b/etc/bash/vcs-ps1.sh deleted file mode 100644 index 6e74e1d..0000000 --- a/etc/bash/vcs-ps1.sh +++ /dev/null @@ -1,171 +0,0 @@ -#!/bin/sh - -repo_status_git() { - BRANCH_NAME="$(git rev-parse --abbrev-ref HEAD)" - OUT="$(git status --short --branch --porcelain)" - BRANCH_LINE="$(echo "$OUT" | head -n 1)" - DIFF_LINES="$(echo "$OUT" | tail -n +2)" - - IS_AHEAD=false - IS_BEHIND=false - if echo "$BRANCH_LINE" | grep -q 'ahead'; then - IS_AHEAD=true - fi - if echo "$BRANCH_LINE" | grep -q 'behind'; then - IS_BEHIND=true - fi - - LINE='' - - if [ "$IS_AHEAD" = true ] && [ "$IS_BEHIND" = true ]; then - LINE="^^^ $BRANCH_NAME vvv" - elif [ "$IS_AHEAD" = true ]; then - LINE="^ $BRANCH_NAME ^" - elif [ "$IS_BEHIND" = true ]; then - LINE="v $BRANCH_NAME v" - else - LINE="$BRANCH_NAME" - fi - - HAS_DIFF=false - HAS_UNTRACKED=false - if echo "$DIFF_LINES" | grep -q '^[A|D|M| ][M|D| ]'; then - HAS_DIFF=true - fi - if echo "$DIFF_LINES" | grep -q '^[?][?]'; then - HAS_UNTRACKED=true - fi - - if [ "$HAS_DIFF" = true ]; then - COLOR_FN=redb - LINE="{$LINE}" - elif [ "$IS_AHEAD" = true ] || [ "$IS_BEHIND" = true ]; then - COLOR_FN=bluei - LINE="[$LINE]" - elif [ "$HAS_UNTRACKED" = true ]; then - COLOR_FN=lightblue - LINE="{$LINE}" - else - COLOR_FN=green - LINE="($LINE)" - fi - - color -c "$COLOR_FN" "$LINE" - - BRANCH_COUNT="$(git branch --list | wc -l)" - if [ "$BRANCH_COUNT" -gt 1 ]; then - color -c lightblue "<$BRANCH_COUNT>" - fi - - STASH_COUNT="$(git stash list | wc -l)" - if [ "$STASH_COUNT" != 0 ]; then - color -c red "*$STASH_COUNT" - fi - - color -c blacki " - git/$(git rev-parse HEAD)" -} - -repo_status_fossil() { - BRANCH_NAME="$(fossil branch current)" - - if [ -n "$(fossil extras)" ]; then - HAS_UNTRACKED=1 - fi - - BRANCH_MARKER="$BRANCH_NAME" - - if [ -n "${HAS_UNTRACKED:-}" ]; then - COLOR_FN=lightblue - LINE="($BRANCH_MARKER)" - else - COLOR_FN=green - LINE="($BRANCH_MARKER)" - fi - - color -c "$COLOR_FN" "$LINE" - - color -c blacki " - fossil/$(fossil info | awk '/^checkout:/ { print $2 }')" -} - -repo_status_mercurial() { - BRANCH_NAME="$(hg branch)" -} - -repo_status() { - dir="$(basename "$PWD")" - if [ -d .git ]; then - repo_status_git - elif [ -f "$dir.fossil" ]; then - repo_status_fossil - elif [ -d .hg ]; then - repo_status_mercurial - fi -} - - -error_marker() { - STATUS=$? - if [ "$STATUS" != 0 ]; then - color -c redb " (!! $STATUS !!) " - fi -} - -shell_level() { - if [ -n "${SHLVL:-}" ]; then - if [ -n "${TMUX:-}" ]; then - LVL=$((SHLVL - 1)) - else - LVL="$SHLVL" - fi - if [ "$LVL" != 1 ]; then - color -c white "[$LVL] " - fi - fi -} - -timestamp() { - color -c blacki '\T' -} - -path() { - color -c yellowb '\w/' -} - -guix_env() { - if [ -n "${GUIX_ENVIRONMENT:-}" ]; then - printf '\n' - color -c blacki '~> ' - color -c purple 'guix environment ' - printf '(' - color -c blueb "$GUIX_ENVIRONMENT" - printf ')' - fi -} - -in_nix_shell() { - if [ -n "${IN_NIX_SHELL:-}" ]; then - printf '\n' - color -c blacki '~> ' - color -c purpleb "$IN_NIX_SHELL " - color -c purple 'nix-shell ' - printf '(' - color -c blueb "${name:-}" - printf ')' - fi -} - -PS1='`error_marker`'$(timestamp)' '$(path)' `shell_level``repo_status``guix_env``in_nix_shell` -$ ' - - -vcs_status() { - git status "$@" -} - -vcs_diff() { - git diff "$@" -} - -vcs_diff_staged() { - git diff --staged "$@" -} diff --git a/etc/sh/vcs-ps1.sh b/etc/sh/vcs-ps1.sh new file mode 100644 index 0000000..d18a24a --- /dev/null +++ b/etc/sh/vcs-ps1.sh @@ -0,0 +1,200 @@ +#!/bin/sh + +repo_status_git() { + BRANCH_NAME="$(git rev-parse --abbrev-ref HEAD)" + OUT="$(git status --short --branch --porcelain)" + BRANCH_LINE="$(echo "$OUT" | head -n 1)" + DIFF_LINES="$(echo "$OUT" | tail -n +2)" + + IS_AHEAD=false + IS_BEHIND=false + if echo "$BRANCH_LINE" | grep -q 'ahead'; then + IS_AHEAD=true + fi + if echo "$BRANCH_LINE" | grep -q 'behind'; then + IS_BEHIND=true + fi + + LINE='' + + if [ "$IS_AHEAD" = true ] && [ "$IS_BEHIND" = true ]; then + LINE="^^^ $BRANCH_NAME vvv" + elif [ "$IS_AHEAD" = true ]; then + LINE="^ $BRANCH_NAME ^" + elif [ "$IS_BEHIND" = true ]; then + LINE="v $BRANCH_NAME v" + else + LINE="$BRANCH_NAME" + fi + + HAS_DIFF=false + HAS_UNTRACKED=false + if echo "$DIFF_LINES" | grep -q '^[A|D|M| ][M|D| ]'; then + HAS_DIFF=true + fi + if echo "$DIFF_LINES" | grep -q '^[?][?]'; then + HAS_UNTRACKED=true + fi + + if [ "$HAS_DIFF" = true ]; then + COLOR_FN=redb + LINE="{$LINE}" + elif [ "$IS_AHEAD" = true ] || [ "$IS_BEHIND" = true ]; then + COLOR_FN=bluei + LINE="[$LINE]" + elif [ "$HAS_UNTRACKED" = true ]; then + COLOR_FN=lightblue + LINE="{$LINE}" + else + COLOR_FN=green + LINE="($LINE)" + fi + + color -c "$COLOR_FN" "$LINE" + + BRANCH_COUNT="$(git branch --list | wc -l)" + if [ "$BRANCH_COUNT" -gt 1 ]; then + color -c lightblue "<$BRANCH_COUNT>" + fi + + STASH_COUNT="$(git stash list | wc -l)" + if [ "$STASH_COUNT" != 0 ]; then + color -c red "*$STASH_COUNT" + fi + + color -c blacki " - git/$(git rev-parse HEAD)" +} + +repo_status_fossil() { + BRANCH_NAME="$(fossil branch current)" + + if [ -n "$(fossil extras)" ]; then + HAS_UNTRACKED=1 + fi + + BRANCH_MARKER="$BRANCH_NAME" + + if [ -n "${HAS_UNTRACKED:-}" ]; then + COLOR_FN=lightblue + LINE="($BRANCH_MARKER)" + else + COLOR_FN=green + LINE="($BRANCH_MARKER)" + fi + + color -c "$COLOR_FN" "$LINE" + + color -c blacki " - fossil/$(fossil info | awk '/^checkout:/ { print $2 }')" +} + +repo_status_mercurial() { + BRANCH_NAME="$(hg branch)" +} + +repo_status() { + dir="$(basename "$PWD")" + if [ -d .git ]; then + repo_status_git + elif [ -f "$dir.fossil" ]; then + repo_status_fossil + elif [ -d .hg ]; then + repo_status_mercurial + fi +} + + +error_marker() { + STATUS=$? + if [ "$STATUS" != 0 ]; then + color -c redb " (!! $STATUS !!) " + fi +} + +shell_status_level() { + if [ -n "${SHLVL:-}" ]; then + if [ -n "${TMUX:-}" ]; then + LVL=$((SHLVL - 1)) + else + LVL="$SHLVL" + fi + if [ "$LVL" != 1 ]; then + color -c white "$LVL" + if [ -n "${RANGER_LEVEL:-}" ]; then + color -c white '|' + color -c bluei 'r' + fi + fi + fi +} + +shell_status_jobs() { + JOBS="$(jobs -p | wc -l)" + if [ "$JOBS" != 0 ]; then + color -c red "$JOBS" + fi +} + +shell_status() { + LEVEL="$(shell_status_level)" + JOBS="$(shell_status_jobs)" + + if [ -z "$LEVEL" ] && [ -z "$JOBS" ]; then + return + fi + + color -c white '[' + printf '%s' "$LEVEL" + if [ -n "$LEVEL" ] && [ -n "$JOBS" ]; then + color -c white '|' + fi + printf '%s' "$JOBS" + color -c white ']' + printf ' ' +} + +timestamp() { + color -c blacki '\T' +} + +path() { + color -c yellowb '\w/' +} + +guix_env() { + if [ -n "${GUIX_ENVIRONMENT:-}" ]; then + printf '\n' + color -c blacki '~> ' + color -c purple 'guix environment ' + printf '(' + color -c blueb "$GUIX_ENVIRONMENT" + printf ')' + fi +} + +in_nix_shell() { + if [ -n "${IN_NIX_SHELL:-}" ]; then + printf '\n' + color -c blacki '~> ' + color -c purpleb "$IN_NIX_SHELL " + color -c purple 'nix-shell ' + printf '(' + color -c blueb "${name:-}" + printf ')' + fi +} + +PS1='`error_marker`'$(timestamp)' '$(path)' `shell_status``repo_status``guix_env``in_nix_shell` +$ ' + + +vcs_status() { + git status "$@" +} + +vcs_diff() { + git diff "$@" +} + +vcs_diff_staged() { + git diff --staged "$@" +} -- cgit v1.2.3