2019-08-15 04:44:46 +00:00
|
|
|
function fish_prompt --description 'Write out the prompt'
|
|
|
|
set -l last_status $status
|
|
|
|
set -l color_cwd
|
|
|
|
set -l suffix
|
|
|
|
set -l user_prefix
|
|
|
|
set -l git_branch
|
|
|
|
set -l jobs_num
|
|
|
|
set -l exit_code
|
|
|
|
|
|
|
|
# Set exit code, based on last command status
|
|
|
|
if test $last_status -ne 0
|
|
|
|
set exit_code '<' (set_color $fish_color_status) $last_status (set_color normal) '> '
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set jobs num, based on exit code of jobs command
|
2020-10-31 22:13:25 +00:00
|
|
|
if jobs >/dev/null
|
2019-08-15 04:44:46 +00:00
|
|
|
set -l jobs_out (jobs -c)
|
2019-09-08 19:25:04 +00:00
|
|
|
set jobs_num '[' (set_color grey) (count $jobs_out) (set_color normal) '] '
|
2019-08-15 04:44:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set cwd color and prompt suffix, based on current user and fish_bind_mode.
|
|
|
|
switch "$USER"
|
|
|
|
case root toor
|
|
|
|
if set -q fish_color_cwd_root
|
|
|
|
set color_cwd $fish_color_cwd_root
|
|
|
|
else
|
|
|
|
set color_cwd $fish_color_cwd
|
|
|
|
end
|
|
|
|
|
2020-11-06 00:51:51 +00:00
|
|
|
set suffix '#'
|
2019-08-15 04:44:46 +00:00
|
|
|
|
|
|
|
if test $fish_key_bindings = 'fish_vi_key_bindings'
|
|
|
|
switch "$fish_bind_mode"
|
2020-10-31 22:13:25 +00:00
|
|
|
case 'insert'
|
|
|
|
set suffix (set_color brblue) $suffix (set_color normal)
|
|
|
|
case 'visual'
|
2020-11-06 00:51:51 +00:00
|
|
|
set suffix (set_color bryellow) $suffix (set_color normal)
|
2019-08-15 04:44:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
case '*'
|
|
|
|
set color_cwd $fish_color_cwd
|
2020-10-31 22:13:25 +00:00
|
|
|
|
2020-11-06 00:51:51 +00:00
|
|
|
set suffix '>'
|
2019-08-15 04:44:46 +00:00
|
|
|
|
|
|
|
if test $fish_key_bindings = 'fish_vi_key_bindings'
|
|
|
|
switch "$fish_bind_mode"
|
2020-10-31 22:13:25 +00:00
|
|
|
case 'insert'
|
|
|
|
set suffix (set_color brblue) $suffix (set_color normal)
|
|
|
|
case 'default'
|
2020-11-06 00:51:51 +00:00
|
|
|
set suffix '^'
|
2020-10-31 22:13:25 +00:00
|
|
|
case 'visual'
|
2020-11-06 00:51:51 +00:00
|
|
|
set suffix (set_color bryellow) 'v' (set_color normal)
|
2019-08-15 04:44:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
# Set user_prefix, based on whether or not inside of ssh session.
|
2020-10-31 22:13:25 +00:00
|
|
|
if test -n "$SSH_CLIENT"
|
|
|
|
set user_prefix $USER @ (prompt_hostname) ' '
|
|
|
|
end
|
2019-08-15 04:44:46 +00:00
|
|
|
|
|
|
|
# Show current git branch, based on git commands only.
|
2020-10-31 22:13:25 +00:00
|
|
|
if git status >/dev/null 2>&1
|
2019-08-15 04:44:46 +00:00
|
|
|
set -l branch_color green
|
|
|
|
set -l git_status (git status)
|
|
|
|
|
2020-10-31 22:13:25 +00:00
|
|
|
if string match 'Changes not staged for commit:' $git_status >/dev/null
|
|
|
|
or string match 'Untracked files:' $git_status >/dev/null
|
2019-08-15 04:44:46 +00:00
|
|
|
set branch_color red
|
2020-10-31 22:13:25 +00:00
|
|
|
else if string match 'Changes to be committed:' $git_status >/dev/null
|
2019-08-15 04:44:46 +00:00
|
|
|
set branch_color yellow
|
|
|
|
end
|
|
|
|
|
2020-02-08 22:45:16 +00:00
|
|
|
set -l cur_branch (string sub -s 3 (string match -r '^\* .*$' (git branch)))
|
2020-10-31 22:13:25 +00:00
|
|
|
if string match -q '(HEAD detached*' $cur_branch
|
|
|
|
set cur_branch 'detached'
|
|
|
|
end
|
2019-08-15 04:44:46 +00:00
|
|
|
|
2020-04-17 21:36:44 +00:00
|
|
|
if test -n "$cur_branch"
|
|
|
|
set -l cur_branch_len (string length $cur_branch)
|
|
|
|
if test $cur_branch_len -gt 21
|
|
|
|
set -l sub_str (string sub -l 18 $cur_branch)
|
|
|
|
set cur_branch "$sub_str..."
|
|
|
|
end
|
2019-08-15 04:44:46 +00:00
|
|
|
end
|
|
|
|
|
2020-10-31 22:13:25 +00:00
|
|
|
if string match 'Your branch is ahead of*' $git_status >/dev/null
|
|
|
|
set cur_branch $cur_branch \u21A5
|
|
|
|
else if string match 'Your branch is behind*' $git_status >/dev/null
|
|
|
|
set cur_branch $cur_branch \u21A7
|
2019-08-15 04:44:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
set git_branch ' on ' (set_color $branch_color) $cur_branch (set_color normal)
|
|
|
|
end
|
|
|
|
|
2020-11-06 00:51:51 +00:00
|
|
|
# Set user prefix, based on docker machine name
|
|
|
|
if test -n "$DOCKER_MACHINE_NAME"
|
|
|
|
set user_prefix (set_color blue) $DOCKER_MACHINE_NAME (set_color normal) ' '
|
|
|
|
end
|
|
|
|
|
2019-08-15 04:44:46 +00:00
|
|
|
# Set go version, by existence of go mod or dep files
|
2020-02-08 22:10:26 +00:00
|
|
|
if test -e ./go.mod; or test -e ./Gopkg.toml; and command -sq go
|
|
|
|
if test -z "$go_version"
|
|
|
|
set -l version_str (string match -r 'go\d+\.\d+\.?\d*' (go version))
|
2020-02-23 23:01:31 +00:00
|
|
|
set -g go_version ' with ' (set_color 8eadaf) $version_str (set_color normal)
|
2020-02-08 22:10:26 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
set -g go_version
|
2019-08-15 04:44:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set docker version, by existence of Dockerfile
|
2020-02-08 22:10:26 +00:00
|
|
|
if test -e ./Dockerfile; and command -sq docker
|
|
|
|
if test -z "$docker_version"
|
|
|
|
set -l version_str (string match -r '\d+\.\d+\.?\d*' (docker --version))
|
2020-02-23 23:01:31 +00:00
|
|
|
set -g docker_version ' on ' (set_color blue) 'docker' $version_str (set_color normal)
|
2020-02-08 22:10:26 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
set -g docker_version
|
2019-08-15 04:44:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set node (and ts) version, based on existance of package.json (and tsconfig.json)
|
2020-02-08 22:10:26 +00:00
|
|
|
if test -e ./package.json; and command -sq node
|
|
|
|
if test -z "$node_version"
|
|
|
|
set -l version_str (string sub -s 2 (node -v))
|
2020-02-23 23:01:31 +00:00
|
|
|
set -g node_version ' with ' (set_color brgreen) 'node' $version_str (set_color normal)
|
2020-02-08 22:10:26 +00:00
|
|
|
|
|
|
|
if test -e ./tsconfig.json; and command -sq tsc
|
|
|
|
set -l version_str (string match -r '\d+\.\d+\.?\d*' (tsc -v))
|
2020-02-23 23:01:31 +00:00
|
|
|
set -g node_version $node_version ' and ' (set_color cyan) 'ts' $version_str (set_color normal)
|
2020-02-08 22:10:26 +00:00
|
|
|
end
|
2019-08-15 04:44:46 +00:00
|
|
|
end
|
2020-02-08 22:10:26 +00:00
|
|
|
else
|
|
|
|
set -g node_version
|
2019-08-15 04:44:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set elixir version, based on existance of mix.exs
|
2020-02-08 22:10:26 +00:00
|
|
|
if test -e ./mix.exs; and command -sq elixir
|
|
|
|
if test -z "$ex_version"
|
|
|
|
set -l version_str (string sub -s 8 (string match -r 'Elixir \d+\.\d+\.?\d*' (elixir -v)))
|
2020-02-23 23:01:31 +00:00
|
|
|
set -g ex_version ' with ' (set_color magenta) 'ex' $version_str (set_color normal)
|
2020-02-08 22:10:26 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
set -g ex_version
|
2019-08-15 04:44:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set dart version, based on existances of pubspec.yaml
|
2020-02-08 22:10:26 +00:00
|
|
|
if test -e ./pubspec.yaml; and command -sq dart
|
|
|
|
if test -z "$dart_version"
|
|
|
|
set -l version_str (string match -r '\d+\.\d+\.?\d*' (dart --version 2>| cat))
|
2020-02-23 23:01:31 +00:00
|
|
|
set -g dart_version ' with ' (set_color brblue) 'dart' $version_str (set_color normal)
|
2020-02-08 22:10:26 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
set -g dart_version
|
2019-08-15 04:44:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Combine all prompt variables
|
|
|
|
set -l cwd (set_color $color_cwd) (prompt_pwd) (set_color normal)
|
|
|
|
set -l top_prompt $cwd $git_branch $go_version $ex_version $node_version $dart_version $flutter_version $docker_version
|
|
|
|
set -l bottom_prompt $user_prefix $jobs_num $exit_code $suffix ' '
|
|
|
|
|
|
|
|
# Decide whether to insert newline, based on whether the top prompt is only equal to cwd
|
|
|
|
if test "$top_prompt" = "$cwd"
|
|
|
|
set bottom_prompt ' ' $bottom_prompt
|
|
|
|
else
|
|
|
|
set bottom_prompt \n $bottom_prompt
|
|
|
|
end
|
|
|
|
|
|
|
|
echo -n -s $top_prompt
|
|
|
|
echo -n -s $bottom_prompt
|
|
|
|
end
|