move stuff into functions/ as per fisher 4.2

This commit is contained in:
lowne
2021-02-26 13:59:48 +01:00
parent 558d5b3e9d
commit ed0fcfe31f
24 changed files with 0 additions and 0 deletions

View File

@@ -0,0 +1,2 @@
function fish_mode_prompt
end

138
functions/fish_prompt.fish Normal file
View File

@@ -0,0 +1,138 @@
function fish_prompt
set -l status_copy $status
set -l pwd_info (pwd_info "/")
set -l dir
set -l base
set -l dark_mode 1 #default to dark mode
set -l base_color
set -l text_color
set -l split_color
if functions -q is_dark_mode
is_dark_mode; or set -e dark_mode
end
if set -lq dark_mode
set base_color BBB 333
set text_color FFF
# set split_color 000
else
set base_color 555 DDD
set text_color 000
# set split_color FFF
end
set split_color $base_color[2]
if test "$PWD" = ~
set base "~"
else if pwd_is_home
set dir "~/"
else
if test "$PWD" != /
set dir "/"
end
set base (set_color red)"/"
end
if test ! -z "$pwd_info[1]"
set base "$pwd_info[1]"
end
if test ! -z "$pwd_info[2]"
set dir "$dir$pwd_info[2]/"
end
if test ! -z "$pwd_info[3]"
segment $base_color " $pwd_info[3] "
end
if set branch_name (git_branch_name)
set -l git_color black green
set -l git_glyph ""
if git_is_staged
set git_color black yellow
if git_is_dirty
set git_color $git_color white red
end
else if git_is_dirty
set git_color white red
else if git_is_touched
set git_color white red
end
if git_is_detached_head
set git_glyph "➤"
else if git_is_stashed
set git_glyph "╍╍"
end
set -l prompt
set -l git_ahead (git_ahead "+ " "- " "+- ")
if test "$branch_name" = master
set prompt " $git_glyph $git_ahead"
else
set prompt " $git_glyph $branch_name $git_ahead"
end
if set -q git_color[3]
segment "$git_color[3]" "$git_color[4]" "$prompt"
segment $split_color $split_color
segment "$git_color[1]" "$git_color[2]" " $git_glyph "
else
segment "$git_color[1]" "$git_color[2]" "$prompt"
end
end
segment $base_color " $dir"(set_color $text_color)"$base "
if test ! -z "$SSH_CLIENT"
set -l color bbb 222
if test 0 -eq (id -u "$USER")
set color red 222
end
segment $color (host_info " usr@host ")
else if test 0 -eq (id -u "$USER")
segment red 222 " \$ "
end
if test "$status_copy" -ne 0
segment red white (set_color -o)" ! "(set_color normal)
else if last_job_id > /dev/null
segment white 333 " %% "
end
if [ "$theme_display_virtualenv" != 'no' ]; and set -q VIRTUAL_ENV
segment yellow blue " "(basename "$VIRTUAL_ENV")" "
end
if [ "$theme_display_ruby" != 'no' ]; and set -q RUBY_VERSION
segment red fff " "(basename "$RUBY_VERSION")" "
end
if test "$fish_key_bindings" = "fish_vi_key_bindings"
switch $fish_bind_mode
case default
segment white red "[N]"
case insert
segment black green "[I]"
case replace-one
segment yellow blue "[R]"
case visual
segment white magenta "[V]"
end
end
segment_close
end

View File

@@ -0,0 +1,47 @@
function fish_right_prompt
set -l status_copy $status
set -l status_code $status_copy
set -l dark_mode 1 #default to dark mode
if functions -q is_dark_mode
is_dark_mode; or set -e dark_mode
end
set -l status_color 555; set -lq dark_mode; or set status_color DDD
set -l clock_color $status_color
set -l status_glyph " "
set -l duration_glyph
switch "$status_copy"
case 0 "$__metro_status_last"
set status_code
end
set -g __metro_status_last $status_copy
if test "$status_copy" -eq 0
set duration_glyph " "
else
set status_color red
set status_glyph
end
if test "$CMD_DURATION" -gt 250
if test ! -z "$status_code"
echo -sn (set_color $status_color) "($status_code)" (set_color normal)
set status_glyph
end
set -l duration (echo $CMD_DURATION | humanize_duration)
echo -sn (set_color $status_color) " ($duration) $duration_glyph" (set_color normal)
else
if test ! -z "$status_code"
echo -sn (set_color $status_color) "($status_code) " (set_color normal)
set status_glyph
end
end
set -q fish_right_prompt_show_clock; and echo -sn (set_color $clock_color) (date "+%H:%M:%S")
echo -sn (set_color $status_color) "$status_glyph" (set_color normal)
end

8
functions/git_ahead.fish Normal file
View File

@@ -0,0 +1,8 @@
function git_ahead -a ahead behind diverged none
command git rev-list --count --left-right "@{upstream}...HEAD" 2>/dev/null | command awk "
/^0\t0/ { print \"$none\" ? \"$none\" : \"\"; exit 0 }
/^[0-9]+\t0/ { print \"$behind\" ? \"$behind\" : \"-\"; exit 0 }
/^0\t[0-9]+/ { print \"$ahead\" ? \"$ahead\" : \"+\"; exit 0 }
// { print \"$diverged\" ? \"$diverged\" : \"±\"; exit 0 }
"
end

View File

@@ -0,0 +1,15 @@
function git_branch_name -d "Get the name of the current Git branch, tag or sha1"
set -l branch_name (command git symbolic-ref --short HEAD 2>/dev/null)
if test -z "$branch_name"
set -l tag_name (command git describe --tags --exact-match HEAD 2>/dev/null)
if test -z "$tag_name"
command git rev-parse --short HEAD 2>/dev/null
else
printf "%s\n" "$tag_name"
end
else
printf "%s\n" "$branch_name"
end
end

View File

@@ -0,0 +1,3 @@
function git_is_detached_head -d "Test if the repository is in a detached HEAD state"
git_is_repo; and not command git symbolic-ref HEAD 2>/dev/null > /dev/null
end

View File

@@ -0,0 +1,3 @@
function git_is_dirty -d "Test if there are changes not staged for commit"
git_is_repo; and not command git diff --no-ext-diff --quiet --exit-code 2>/dev/null
end

View File

@@ -0,0 +1,3 @@
function git_is_empty -d "Test if a repository is empty"
git_is_repo; and test -z (command git rev-list -n 1 --all 2>/dev/null)
end

View File

@@ -0,0 +1,5 @@
function git_is_repo -d "Test if the current directory is a Git repository"
if not command git rev-parse --git-dir > /dev/null 2>/dev/null
return 1
end
end

View File

@@ -0,0 +1,3 @@
function git_is_staged -d "Test if there are changes staged for commit"
git_is_repo; and not command git diff --cached --no-ext-diff --quiet --exit-code 2>/dev/null
end

View File

@@ -0,0 +1,3 @@
function git_is_stashed -d "Test if there are changes in the Git stash"
command git rev-parse --verify --quiet refs/stash > /dev/null 2>/dev/null
end

View File

@@ -0,0 +1,3 @@
function git_is_tag -d "Test if HEAD is on top of a tag (can be simple, annotated or signed)"
git_is_detached_head; and command git describe --tags --exact-match HEAD 2>/dev/null > /dev/null
end

View File

@@ -0,0 +1,3 @@
function git_is_touched -d "Test if there are any changes in the working tree"
git_is_staged; or git_is_dirty
end

View File

@@ -0,0 +1,3 @@
function git_repository_root -d "Get the top level directory of the current git repository"
git_is_repo; and command git rev-parse --show-toplevel
end

View File

@@ -0,0 +1,15 @@
function git_untracked_files -d "Get the number of untracked files in a repository"
git_is_repo; and command git ls-files --others --exclude-standard | command awk '
BEGIN {
n = 0
}
{ n++ }
END {
print n
exit !n
}
'
end

16
functions/host_info.fish Normal file
View File

@@ -0,0 +1,16 @@
function host_info -d "Get user and hostname information" -a format
set -l host (uname -n)
command id -un | command awk -v host="$host" -v format="$format" '
BEGIN { if (format == "") format = "user@host" }
{
user = $0
if (!sub("usr", substr(user, 1, 1), format)) {
sub("user", user, format)
}
len = split(host, host_info, ".")
sub("host", host_info[1], format)
sub("domain", len > 1 ? host_info[2] : "", format)
print(format)
}
'
end

View File

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

View File

@@ -0,0 +1,3 @@
function last_job_id
jobs $argv | command awk '/^[0-9]+\t/ { print status = $1 } END { exit !status }'
end

43
functions/pwd_info.fish Normal file
View File

@@ -0,0 +1,43 @@
function pwd_info -a separator -d "Print easy-to-parse information the current working directory"
set -l home ~
set -l git_root (command git rev-parse --show-toplevel ^ /dev/null)
command pwd -P | awk -v home="$home" -v git_root="$git_root" -v separator="$separator" -v dir_length="$fish_prompt_pwd_dir_length" '
function base(string) {
sub(/^\/?.*\//, "", string)
return string
}
function dirs(string, printLastName, prefix, path) {
len = split(string, parts, "/")
for (i = 1; i < len; i++) {
name = dir_length == 0 ? parts[i] : substr(parts[i], 1, dir_length ? dir_length : 1)
if (parts[i] == "" || name == ".") {
continue
}
path = path prefix name
prefix = separator
}
return (printLastName == 1) ? path prefix parts[len] : path
}
function remove(thisString, fromString) {
sub(thisString, "", fromString)
return fromString
}
{
if (git_root == home) {
git_root = ""
}
if (git_root == "") {
printf("%s\n%s\n%s\n",
$0 == home || $0 == "/" ? "" : base($0),
dirs(remove(home, $0)),
"")
} else {
printf("%s\n%s\n%s\n",
base(git_root),
dirs(remove(home, git_root)),
$0 == git_root ? "" : dirs(remove(git_root, $0), 1))
}
}
'
end

View File

@@ -0,0 +1,8 @@
function pwd_is_home
switch "$PWD"
case ~{,/\*}
return 0
case \*
return 1
end
end

8
functions/segment.fish Normal file
View File

@@ -0,0 +1,8 @@
function segment -a fg bg text -d "Add prompt segment"
if test -z "$segment_color"
set segment_color normal
end
set -g segment (set_color $fg -b $bg)"$text"(set_color $bg -b $segment_color)"$segment"
set -g segment_color $bg
end

View File

@@ -0,0 +1,8 @@
function segment_close
if test ! -z "$segment"
printf "$segment "
set segment
set segment_color
end
set_color normal
end

View File

@@ -0,0 +1,11 @@
function segment_right -a fg bg text -d "Add right prompt segment"
set -l right_color $segment_right_color
if test -z "$right_color"
set right_color $bg
end
set -g segment_right_color $fg
echo (set_color $bg)(set_color $segment_right_color -b $bg)"$text"(set_color $right_color)
end

View File

@@ -0,0 +1,32 @@
function set_color_custom
set -l brgrey 666
set -l brred red
if contains -- brgrey (set_color -c)
set brgrey "brgrey"
set brred "brred"
end
set -U fish_color_normal normal
set -U fish_color_command blue
set -U fish_color_param cyan
set -U fish_color_redirection normal
set -U fish_color_comment $brred
set -U fish_color_error red
set -U fish_color_escape cyan
set -U fish_color_operator cyan
set -U fish_color_end green
set -U fish_color_quote yellow
set -U fish_color_autosuggestion $brgrey
set -U fish_color_valid_path --underline
set -U fish_color_cwd green
set -U fish_color_cwd_root red
set -U fish_color_match cyan
set -U fish_color_search_match --background=$brgrey
set -U fish_color_selection --background=$brgrey
set -U fish_pager_color_prefix cyan
set -U fish_pager_color_completion white
set -U fish_pager_color_description $brgrey
set -U fish_pager_color_progress cyan
set -U fish_color_history_current cyan
end