From 42c7f5311188faaefb3c9de0a0372855dc58ddfc Mon Sep 17 00:00:00 2001 From: ache Date: Thu, 22 Oct 2020 11:38:26 +0200 Subject: New function --- functions/fish_right_prompt.fish | 216 +++++++++++++++++++++++++++++++++++++++ functions/fr.fish | 1 + functions/gdr.fish | 1 + functions/humanize_duration.fish | 19 ++++ functions/lw.fish | 1 + functions/screenoff.fish | 1 + functions/wch.fish | 1 + 7 files changed, 240 insertions(+) create mode 100644 functions/fish_right_prompt.fish create mode 100644 functions/fr.fish create mode 100644 functions/gdr.fish create mode 100644 functions/humanize_duration.fish create mode 100644 functions/lw.fish create mode 100644 functions/screenoff.fish create mode 100644 functions/wch.fish (limited to 'functions') diff --git a/functions/fish_right_prompt.fish b/functions/fish_right_prompt.fish new file mode 100644 index 0000000..ea5681d --- /dev/null +++ b/functions/fish_right_prompt.fish @@ -0,0 +1,216 @@ +# +# Segments functions +# +set -g current_bg NONE +set -g segment_separator_right \uE0B2 + + + +function prompt_segment_right -d "Function to draw a segment" + + set -l bg + set -l fg + if [ -n "$argv[1]" ] + set bg $argv[1] + else + set bg normal + end + + if [ -n "$argv[2]" ] + set fg $argv[2] + else + set fg normal + end + + if [ -z "$argv[3]" ] + return + end + + set_color $bg + echo -n " $segment_separator_right" + + set_color -b $bg + set_color $fg + + + echo -n " $argv[3]" + set -g current_bg $bg +end + +function git_status_summary_right -d "Deal with git status --porcelain" + awk ' + BEGIN { + untracked=0; + unstaged=0; + staged=0; + ahead=0; + behind=0; + } + { + if (!after_first && $0 ~ /^##.+/) { + if ( match($0, /ahead ([1-9]+)/) ) { + split(substr($0, RSTART, RLENGTH), A); + ahead=A[2]; + } + if ( match($0, /behind ([1-9]+)/) ) { + split(substr($0, RSTART, RLENGTH), A); + behind=A[2]; + } + } else if ($0 ~ /^\?\? .+/) { + untracked += 1 + } else { + if ($0 ~ /^.[^ ] .+/) { + unstaged += 1 + } + if ($0 ~ /^[^ ]. .+/) { + staged += 1 + } + } + after_first = 1 + } + END { + if (!seen_header) { + + } + print ahead ";" behind ";" untracked ";" unstaged ";" staged + }' +end + + +function prompt_git_right -d "Display the current git state" + set -l ref + if [ -e .git ] + set dirty '' + set gstatus ( git status --porcelain -b | git_status_summary_right ) + + # Check for commit ahead + set ahead (echo $gstatus | cut -d ';' -f 1) + # Check for commit behind + set behind (echo $gstatus | cut -d ';' -f 2) + # Check for modified files + set unstdg (echo $gstatus | cut -d ';' -f 3) + # Check for untracked files + set untrkd (echo $gstatus | cut -d ';' -f 4) + # Check for staged files + set staged (echo $gstatus | cut -d ';' -f 5) + + if [ $ahead -gt 0 ] + set dirty $dirty' ↑:'$ahead + end + if [ $behind -gt 0 ] + set dirty $dirty' ↓:'$behind + end + if [ $untrkd -gt 0 ] + set dirty $dirty' U:'$untrkd + end + if [ $unstdg -gt 0 ] + set dirty $dirty' ?:'$unstdg + end + if [ $staged -gt 0 ] + set dirty $dirty' S:'$staged + end + + + if [ $staged -eq 0 -a $untrkd -eq 0 ] + set BG green + set PROMPT "$dirty" + else + set BG yellow + + # Check if there's any commit in the repo + set -l empty 0 + git rev-parse --quiet --verify HEAD > /dev/null 2>&1; or set empty 1 + + set -l target + if [ $empty = 1 ] + # The repo is empty + set dirty '' + set target '4b825dc642cb6eb9a060e54bf8d69288fbee4904' + + else + # The repo is not emtpy + set target 'HEAD' + end + + # Check for dirty + if [ "$dirty" != "" ] + set PROMPT "$dirty" + end + end + prompt_segment_right $BG black $PROMPT + end +end + + + +function prompt_shlv_right -d "Display level shell" + set pline '' + if [ "$VIMRUNTIME" != "" ] + set pline 'vim' + if [ "$SHLVL" -gt 3 ] + set pline 'vim:['$SHLVL']' + end + else + if [ "$SHLVL" -gt 2 ] + set pline '['$SHLVL']' + end + end + if [ $pline != '' ] + prompt_segment_right 8C8C8C black "$pline" + end +end + +function prompt_todo_right -d "Display todo" + if which todo 2> /dev/null > /dev/null + set all (todo | wc -l) + set chkd (todo checked | wc -l) + set td (math "$all-$chkd") + + if [ $all -gt 0 ] + prompt_segment_right 5C5C5C blue "$td/$all" + end + end +end + +function prompt_status_right -d "the symbols for a non zero exit status, root and background jobs" + + if [ $RETVAL -ne 0 ] + prompt_segment_right black red "✘" + end + + # if superuser (uid == 0) + set -l uid (id -u $USER) + if [ $uid -eq 0 ] + prompt_segment_right black yellow "⚡" + end + + # Jobs display + if [ (jobs -l | wc -l) -gt 0 ] + prompt_segment_right black cyan "⚙" + end +end +function prompt_finish_right -d "Close open segments" + if [ -n $current_bg ] + set_color -b normal + end + set -g current_bg NONE +end + + +function available -a name -d "Check if a function or program is available." + type "$name" 2>/dev/null >&2 +end + + +# +# Right prompt +# +function fish_right_prompt + set -g RETVAL $status +# prompt_status_right +# available git; and prompt_git_right +# prompt_todo_right +# prompt_shlv_right +# prompt_finish_right +end + diff --git a/functions/fr.fish b/functions/fr.fish new file mode 100644 index 0000000..be5b025 --- /dev/null +++ b/functions/fr.fish @@ -0,0 +1 @@ +alias fr='dicofr' diff --git a/functions/gdr.fish b/functions/gdr.fish new file mode 100644 index 0000000..0fb2cc8 --- /dev/null +++ b/functions/gdr.fish @@ -0,0 +1 @@ +alias gd='git diff -R' diff --git a/functions/humanize_duration.fish b/functions/humanize_duration.fish new file mode 100644 index 0000000..eeb999f --- /dev/null +++ b/functions/humanize_duration.fish @@ -0,0 +1,19 @@ +function humanize_duration -d "Make a time interval human readable" + command awk ' + function hmTime(time, stamp) { + split("h:m:s:ms", units, ":") + for (i = 2; i >= -1; i--) { + if (t = int( i < 0 ? time % 1000 : time / (60 ^ i * 1000) % 60 )) { + stamp = stamp t units[sqrt((i - 2) ^ 2) + 1] " " + } + } + if (stamp ~ /^ *$/) { + return "0ms" + } + return substr(stamp, 1, length(stamp) - 1) + } + { + print hmTime($0) + } + ' +end diff --git a/functions/lw.fish b/functions/lw.fish new file mode 100644 index 0000000..713aab7 --- /dev/null +++ b/functions/lw.fish @@ -0,0 +1 @@ +lw='lxc-wrapper' diff --git a/functions/screenoff.fish b/functions/screenoff.fish new file mode 100644 index 0000000..004fbf0 --- /dev/null +++ b/functions/screenoff.fish @@ -0,0 +1 @@ + alias screenoff='xset dpms force off' diff --git a/functions/wch.fish b/functions/wch.fish new file mode 100644 index 0000000..1204cdf --- /dev/null +++ b/functions/wch.fish @@ -0,0 +1 @@ + alias wch++='watch -n 1' -- cgit v1.2.3