chris-marsh / pureline

A Pure Bash Powerline PS1 Command Prompt
MIT License
507 stars 95 forks source link

Add tmux, hg segments #89

Open aghast opened 9 months ago

aghast commented 9 months ago

See patch - just two files in segments/. Hg support is pretty dumb for now, tmux support uses tmux display -p so it can expand to pretty much anything you want.

diff --git a/segments/hg_segment b/segments/hg_segment
new file mode 100644
index 0000000..6377b59
--- /dev/null
+++ b/segments/hg_segment
@@ -0,0 +1,47 @@
+#!/usr/bin/env bash
+
+# hg segment
+
+# Set default symbols if not already defined in config
+# Defaults should be standard symbols.
+[[ -z ${PL_SYMBOLS[hg_branch]} ]] && PL_SYMBOLS[hg_branch]="╬"
+
+# -----------------------------------------------------------------------------
+# append to prompt: hgbranch with indicators for;
+#     number of; modified files, staged files and conflicts
+# arg: $1 background color
+# arg: $2 foreground color
+# option variables;
+#   PL_HG_UNTRACKED: true/false
+function hg_segment {
+    which hg >/dev/null 2>&1 || return;  ## return if no hg
+
+    local hg_branch="$(hg branch 2>/dev/null)"
+
+    [[ -z $hg_branch ]] && return; ## return early if not a branch/no info
+
+   local bg_color="$1"
+   local fg_color="$2"
+   local content="${PL_SYMBOLS[hg_branch]} $hg_branch"
+
+   if [[ $PL_HG_UNTRACKED == true ]]; then
+       local number_untracked="$(hg status -u -T ".\n" | wc -l | tr -d '[:space:]')"
+       if (( number_untracked != 0 )); then
+           content+=" ${PL_SYMBOLS[soft_separator]} ${PL_SYMBOLS[hg_untracked]}$number_untracked"
+       fi
+   fi
+
+   if [[ -n "$(hg status 2>/dev/null)" ]]; then
+       if [[ -v PL_HG_DIRTY_FG ]]; then
+           fg_color="$PL_HG_DIRTY_FG"
+       fi
+       if [[ -v PL_HG_DIRTY_BG ]]; then
+           bg_color="$PL_HG_DIRTY_BG"
+       fi
+   fi
+
+   PS1+="$(segment_end "$fg_color" "$bg_color")"
+   PS1+="$(segment_content "$fg_color" "$bg_color" " $content ")"
+   __last_color="$bg_color"
+}
+
diff --git a/segments/tmux_segment b/segments/tmux_segment
new file mode 100644
index 0000000..c8518df
--- /dev/null
+++ b/segments/tmux_segment
@@ -0,0 +1,37 @@
+#!/usr/bin/env bash
+#
+# tmux_segment
+#
+
+# Set default symbols if not already defined in config.
+# Defaults should be standard symbols.
+
+[[ -z ${PL_SYMBOLS[tmux_session]} ]] && PL_SYMBOLS[tmux_session]='♆'
+[[ -z ${PL_SYMBOLS[tmux_window]} ]] && PL_SYMBOLS[tmux_window]='▢'
+# -----------------------------------------------------------------------------
+# append to prompt: tmux session name
+# arg: $1 background color
+# arg: $2 foreground color
+# option variables;
+#   PL_TMUX_WINDOW: true/false, append window name
+
+function tmux_session_segment {
+    [[ ! -v TMUX ]] && return;  ## return if $TMUX not set
+   which tmux >/dev/null 2>&1 || return; ## return if no 'tmux'
+
+    local bg_color="$1"
+    local fg_color="$2"
+    local content="${PL_SYMBOLS[tmux_session]} #{session_name}"
+
+    if [[ $PL_TMUX_WINDOW ]]; then
+        content+=" ${PL_SYMBOLS[tmux_window]} #{window_name}"
+    fi
+
+    content="$(tmux display -p "$content")"
+
+    PS1+="$(segment_end "$fg_color" "$bg_color")"
+    PS1+="$(segment_content "$fg_color" "$bg_color" " $content ")"
+    __last_color="$bg_color"
+}
+
+