diff options
| author | Christoph Schlosser <christoph@linux.com> | 2025-09-07 17:36:35 +0200 |
|---|---|---|
| committer | Christoph Schlosser <christoph@linux.com> | 2025-09-07 17:36:35 +0200 |
| commit | ab332dec171261db462c954cf6d5b07ba1fab4af (patch) | |
| tree | b8367892ba6899460b75cc30ca286c3e65d5d7b3 | |
| parent | 8cd49ad1e6da644f24fa1c255f754d4e5925284c (diff) | |
| download | dots-ab332dec171261db462c954cf6d5b07ba1fab4af.tar.gz | |
fish: Remove fish_variables and add c-star
| -rw-r--r-- | fish/conf.d/cstar.fish | 161 | ||||
| -rw-r--r-- | fish/config.fish | 2 | ||||
| -rw-r--r-- | fish/fish_plugins | 1 | ||||
| -rw-r--r-- | fish/fish_variables | 38 | ||||
| -rw-r--r-- | fish/functions/fish_prompt.fish | 1358 | ||||
| -rw-r--r-- | fish/functions/fish_right_prompt.fish | 96 |
6 files changed, 202 insertions, 1454 deletions
diff --git a/fish/conf.d/cstar.fish b/fish/conf.d/cstar.fish new file mode 100644 index 0000000..4d53ccb --- /dev/null +++ b/fish/conf.d/cstar.fish @@ -0,0 +1,161 @@ +status is-interactive || exit + +set -q cstar_date_format || set -g cstar_date_format "%Y-%m-%dT%H:%M:%S%z" +set -q cstar_default_bg || set -g cstar_default_bg 333333 +set -q cstar_default_fg || set -g cstar_default_fg a9b7c6 +set -q cstar_user_bg || set -g cstar_user_bg $cstar_default_fg +set -q cstar_user_fg || set -g cstar_user_fg $cstar_default_bg +set -q cstar_pwd_bg || set -g cstar_pwd_bg $cstar_default_bg +set -q cstar_pwd_fg || set -g cstar_pwd_fg $cstar_default_fg +set -q cstar_git_bg || set -g cstar_git_bg $cstar_default_fg +set -q cstar_git_fg || set -g cstar_git_fg $cstar_default_bg +set -q cstar_user_marker_default_bg || set -g cstar_user_marker_default_bg $cstar_default_bg +set -q cstar_user_marker_fg || set -g cstar_user_marker_fg $cstar_default_fg +set -q cstar_exit_status_bg || set -g cstar_exit_status_bg red +set -q cstar_exit_status_fg || set -g cstar_exit_status_fg white +set -q cstar_datetime_bg || set -g cstar_datetime_bg $cstar_default_bg +set -q cstar_datetime_fg || set -g cstar_datetime_fg $cstar_default_fg +set -q cstar_command_time_bg || set -g cstar_command_time_bg $cstar_default_fg +set -q cstar_command_time_fg || set -g cstar_command_time_fg $cstar_default_bg + +set -g _cstar_git_async _cstar_git_async_$fish_pid +set -g _cstar_git_cached_pwd _cstar_git_cached_pwd_$fish_pid + +function cstar_colorize + set -l bg $argv[3] + set -l fg $argv[4] + set -g "$argv[1]" "$(set_color -b $bg $fg) $argv[2] $(set_color normal)" +end + +function cstar_command_time --on-event fish_postexec + set -l dur $CMD_DURATION + set -l time_str "" + + if test $dur -lt 1000 + set time_str (printf "%dms" $dur) + else + set -l s (math "floor($dur / 1000)") + set -l ms_rem (math "$dur % 1000") + + if test $s -lt 60 + set time_str (printf "%d.%03ds" $s $ms_rem) + else + set -l m (math "floor($s / 60)") + set -l s_rem (math "$s % 60") + + if test $m -lt 60 + set time_str (printf "%d:%02d.%03d" $m $s_rem $ms_rem) + else + # If the program was running for more than an hour it's probably ok to + # calculate the totals eagerly to improve readability a bit. + set -l h (math "floor($m / 60)") + set -l m_rem (math "$m % 60") + set -l d (math "floor($h / 24)") + set -l w (math "floor($d / 7)") + set -l mo (math "floor($d / 30)") + set -l y (math "floor($d / 365)") + + if test $h -lt 24 + set time_str (printf "%d:%02d:%02d" $h $m_rem $s_rem) + else if test $d -lt 7 + set -l h_rem (math "$h % 24") + set time_str (printf "%dd %dh" $d $h_rem) + else if test $w -lt 4 + set -l d_rem (math "$d % 7") + set time_str (printf "%dw %dd" $w $d_rem) + else if test $mo -lt 12 + set -l w_rem (math "floor(($d % 30) / 7)") + set time_str (printf "%dmo %dw" $mo $w_rem) + else + set -l mo_rem (math "floor(($d % 365) / 30)") + set time_str (printf "%dy %dmo" $y $mo_rem) + end + end + end + end + + if test -z "$time_str" + set time_str "0ms" + end + + cstar_colorize cstar_command_time $time_str $cstar_command_time_bg $cstar_command_time_fg +end + +function cstar_datetime --on-event fish_prompt + cstar_colorize cstar_datetime (date "+$cstar_date_format") $cstar_datetime_bg $cstar_datetime_fg +end + +function cstar_exit_status --on-event fish_postexec + set cmd_status $status + if test $cmd_status -eq 0 + set -ge cstar_exit_status + else + cstar_colorize cstar_exit_status (set_color -o)\uf00d $cstar_exit_status_bg $cstar_exit_status_fg + end +end + +function cstar_git_query_async --on-event fish_prompt + if ! command -q git + return + end + set -l cwd_is_repo "$(git rev-parse --is-inside-work-tree 2> /dev/null)" + if test "$cwd_is_repo" != "true" + set -Ue $_cstar_git_async + set -Ue $_cstar_git_cached_pwd + end + + set -l cached_pwd $$_cstar_git_cached_pwd + + if test "$PWD" != "$cached_pwd" + if test -n "$cwd_is_repo" + set -U $_cstar_git_async \ue727 + else + set -Ue $_cstar_git_async + end + end + + fish --private --command "set -x __fish_git_prompt_show_informative_status 1 && \ + set -U $_cstar_git_async (fish_git_prompt '%s') && \ + set -q $_cstar_git_async || set -Ue $_cstar_git_async $git_status && \ + set -U $_cstar_git_cached_pwd \"$PWD\" + "& +end + +function cstar_git --on-variable $_cstar_git_async + set git_status $$_cstar_git_async + if test -n "$git_status" + cstar_colorize cstar_git $git_status $cstar_git_bg $cstar_git_fg + else + set -ge cstar_git + end + commandline --function repaint +end + +function cstar_user --on-variable __fish_initialized + set -l user_string (whoami) + if test -n "$SSH_CONNECTION" + set -f user_string "$user_string@$(prompt_hostname)" + end + cstar_colorize cstar_user "$user_string" $cstar_user_bg $cstar_user_fg +end + +function cstar_user_marker --on-variable __fish_initialized + switch (whoami) + case "root" + set -g cstar_user_marker_bg red + set -f char "#" + case '*' + set -g cstar_user_marker_bg $cstar_user_marker_default_bg + set -f char "\$" + end + cstar_colorize cstar_user_marker $char $cstar_user_marker_bg $cstar_user_marker_fg +end + +function cstar_pwd --on-variable PWD --on-variable __fish_initialized + cstar_colorize cstar_pwd (prompt_pwd) $cstar_pwd_bg $cstar_pwd_fg +end + +function cstar_cleanup --on-event fish_exit + set -Ue $_cstar_git_async + set -Ue $_cstar_git_cached_pwd +end diff --git a/fish/config.fish b/fish/config.fish index 1000fb6..4c79026 100644 --- a/fish/config.fish +++ b/fish/config.fish @@ -7,4 +7,6 @@ if status is-interactive set -g theme_newline_cursor yes set -g theme_date_timezone Europe/Berlin set -g theme_date_format "+%a %F %T" + + fish_add_path -gP ~/.local/bin end diff --git a/fish/fish_plugins b/fish/fish_plugins index fa25bf1..50bc957 100644 --- a/fish/fish_plugins +++ b/fish/fish_plugins @@ -1,3 +1,4 @@ jorgebucaran/fisher oh-my-fish/theme-bobthefish jethrokuan/z +cschlosser/c-star diff --git a/fish/fish_variables b/fish/fish_variables deleted file mode 100644 index 4c5987f..0000000 --- a/fish/fish_variables +++ /dev/null @@ -1,38 +0,0 @@ -# This file contains fish universal variable definitions. -# VERSION: 3.0 -SETUVAR ZO_CMD:zo -SETUVAR Z_CMD:z -SETUVAR __fish_initialized:3800 -SETUVAR _fisher_jethrokuan_2F_z_files:\x7e/\x2econfig/fish/functions/__z\x2efish\x1e\x7e/\x2econfig/fish/functions/__z_add\x2efish\x1e\x7e/\x2econfig/fish/functions/__z_clean\x2efish\x1e\x7e/\x2econfig/fish/functions/__z_complete\x2efish\x1e\x7e/\x2econfig/fish/conf\x2ed/z\x2efish -SETUVAR _fisher_jorgebucaran_2F_fisher_files:\x7e/\x2econfig/fish/functions/fisher\x2efish\x1e\x7e/\x2econfig/fish/completions/fisher\x2efish -SETUVAR _fisher_oh_2D_my_2D_fish_2F_theme_2D_bobthefish_files:\x7e/\x2econfig/fish/functions/__bobthefish_colors\x2efish\x1e\x7e/\x2econfig/fish/functions/__bobthefish_display_colors\x2efish\x1e\x7e/\x2econfig/fish/functions/__bobthefish_glyphs\x2efish\x1e\x7e/\x2econfig/fish/functions/bobthefish_display_colors\x2efish\x1e\x7e/\x2econfig/fish/functions/fish_greeting\x2efish\x1e\x7e/\x2econfig/fish/functions/fish_mode_prompt\x2efish\x1e\x7e/\x2econfig/fish/functions/fish_prompt\x2efish\x1e\x7e/\x2econfig/fish/functions/fish_right_prompt\x2efish\x1e\x7e/\x2econfig/fish/functions/fish_title\x2efish -SETUVAR _fisher_plugins:jorgebucaran/fisher\x1eoh\x2dmy\x2dfish/theme\x2dbobthefish\x1ejethrokuan/z -SETUVAR _fisher_upgraded_to_4_4:\x1d -SETUVAR fish_color_autosuggestion:brblack -SETUVAR fish_color_cancel:\x2dr -SETUVAR fish_color_command:normal -SETUVAR fish_color_comment:red -SETUVAR fish_color_cwd:green -SETUVAR fish_color_cwd_root:red -SETUVAR fish_color_end:green -SETUVAR fish_color_error:brred -SETUVAR fish_color_escape:brcyan -SETUVAR fish_color_history_current:\x2d\x2dbold -SETUVAR fish_color_host:normal -SETUVAR fish_color_host_remote:yellow -SETUVAR fish_color_normal:normal -SETUVAR fish_color_operator:brcyan -SETUVAR fish_color_param:cyan -SETUVAR fish_color_quote:yellow -SETUVAR fish_color_redirection:cyan\x1e\x2d\x2dbold -SETUVAR fish_color_search_match:white\x1e\x2d\x2dbackground\x3dbrblack -SETUVAR fish_color_selection:white\x1e\x2d\x2dbold\x1e\x2d\x2dbackground\x3dbrblack -SETUVAR fish_color_status:red -SETUVAR fish_color_user:brgreen -SETUVAR fish_color_valid_path:\x2d\x2dunderline -SETUVAR fish_key_bindings:fish_default_key_bindings -SETUVAR fish_pager_color_completion:normal -SETUVAR fish_pager_color_description:yellow\x1e\x2di -SETUVAR fish_pager_color_prefix:normal\x1e\x2d\x2dbold\x1e\x2d\x2dunderline -SETUVAR fish_pager_color_progress:brwhite\x1e\x2d\x2dbackground\x3dcyan -SETUVAR fish_pager_color_selected_background:\x2dr diff --git a/fish/functions/fish_prompt.fish b/fish/functions/fish_prompt.fish index 8e67d21..cde92fd 100644 --- a/fish/functions/fish_prompt.fish +++ b/fish/functions/fish_prompt.fish @@ -1,1338 +1,20 @@ -# name: bobthefish -# -# bobthefish is a Powerline-style, Git-aware fish theme optimized for awesome. -# -# You will need a Powerline-patched font for this to work: -# -# https://powerline.readthedocs.org/en/master/installation.html#patched-fonts -# -# I recommend picking one of these: -# -# https://github.com/Lokaltog/powerline-fonts -# -# For more advanced awesome, install a nerd fonts patched font (and be sure to -# enable nerd fonts support with `set -g theme_nerd_fonts yes`): -# -# https://github.com/ryanoasis/nerd-fonts -# -# See README.md for setup and configuration options. -# -# ============================== -# Helper methods -# ============================== - -function __bobthefish_basename -d 'basically basename, but faster' - string replace -r '^.*/' '' -- $argv -end - -function __bobthefish_dirname -d 'basically dirname, but faster' - string replace -r '/[^/]+/?$' '' -- $argv -end - -function __bobthefish_pwd -d 'Get a normalized $PWD' - # The pwd builtin accepts `-P` on at least Fish 3.x, but fall back to $PWD if that doesn't work - builtin pwd -P 2>/dev/null - or echo $PWD -end - -# Note that for fish < 3.0 this falls back to unescaped, rather than trying to do something clever /shrug -# After we drop support for older fishies, we can inline this without the fallback. -function __bobthefish_escape_regex -a str -d 'A backwards-compatible `string escape --style=regex` implementation' - string escape --style=regex "$str" 2>/dev/null - or echo "$str" -end - -function __bobthefish_git_branch -S -d 'Get the current git branch (or commitish)' - set -l tag (command git describe --tags --exact-match 2>/dev/null) - and echo "$tag_glyph $tag " - - set -l branch (command git symbolic-ref HEAD 2>/dev/null | string replace -r '^refs/heads/' '') - and begin - [ -n "$theme_git_default_branches" ] - or set -l theme_git_default_branches master main (git config init.defaultBranch) - - [ "$theme_display_git_master_branch" != 'yes' -a "$theme_display_git_default_branch" != 'yes' ] - and contains $branch $theme_git_default_branches - and echo $branch_glyph - and return - - # truncate the middle of the branch name, but only if it's 25+ characters - set -l truncname $branch - [ "$theme_use_abbreviated_branch_name" = 'yes' ] - and set truncname (string replace -r '^(.{17}).{3,}(.{5})$' "\$1…\$2" $branch) - - echo $branch_glyph $truncname - and return - end - - # If we've already shown a tag we don't need to show a detached branch - if [ -z "$tag" ] - set -l branch (command git show-ref --head -s --abbrev | head -n1 2>/dev/null) - echo "$detached_glyph $branch" - end -end - -function __bobthefish_fossil_branch -S -d 'Get the current fossil branch' - set -l branch (command fossil branch 2>/dev/null | string trim --left --chars=' *') - echo "$branch_glyph $branch" -end - -function __bobthefish_hg_branch -S -d 'Get the current hg branch' - set -l branch (command hg branch 2>/dev/null) - set -l book (command hg book | command grep \* | cut -d\ -f3) - echo "$branch_glyph $branch @ $book" -end - -function __bobthefish_pretty_parent -S -a child_dir -d 'Print a parent directory, shortened to fit the prompt' - set -q fish_prompt_pwd_dir_length - or set -l fish_prompt_pwd_dir_length 1 - - # Replace $HOME with ~ - set -l real_home ~ - set -l parent_dir (string replace -r '^'(__bobthefish_escape_regex "$real_home")'($|/)' '~$1' (__bobthefish_dirname $child_dir)) - - # Must check whether `$parent_dir = /` if using native dirname - if [ -z "$parent_dir" ] - echo -n / - return - end - - if [ $fish_prompt_pwd_dir_length -eq 0 ] - echo -n "$parent_dir/" - return - end - - string replace -ar '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*/' '$1/' "$parent_dir/" -end - -function __bobthefish_ignore_vcs_dir -a real_pwd -d 'Check whether the current directory should be ignored as a VCS segment' - for p in $theme_vcs_ignore_paths - set ignore_path (realpath $p 2>/dev/null) - switch $real_pwd/ - case $ignore_path/\* - echo 1 - return - end - end -end - -function __bobthefish_git_project_dir -S -a real_pwd -d 'Print the current git project base directory' - [ "$theme_display_git" = 'no' ] - and return - - command -q git - or return - - set -q theme_vcs_ignore_paths - and [ (__bobthefish_ignore_vcs_dir $real_pwd) ] - and return - - if [ "$theme_git_worktree_support" != 'yes' ] - set -l git_toplevel (command git rev-parse --show-toplevel 2>/dev/null) - - [ -z "$git_toplevel" ] - and return - - ## Support Git under WSL (see #336) - # command -q wslpath - # and set git_toplevel (command wslpath $git_toplevel) - - # If there are no symlinks, just use git toplevel - switch $real_pwd/ - case $git_toplevel/\* - echo $git_toplevel - return - end - - # Otherwise, we need to find the equivalent directory in the $PWD - set -l d $real_pwd - while not [ -z "$d" ] - if [ (realpath "$d") = "$git_toplevel" ] - echo $d - return - end - - [ "$d" = '/' ] - and return - - set d (__bobthefish_dirname $d) - end - return - end - - set -l git_dir (command git rev-parse --git-dir 2>/dev/null) - or return - - pushd $git_dir - set git_dir (__bobthefish_pwd) - popd - - switch $real_pwd/ - case $git_dir/\* - # Nothing works quite right if we're inside the git dir - # TODO: fix the underlying issues then re-enable the stuff below - - # # if we're inside the git dir, sweet. just return that. - # set -l toplevel (command git rev-parse --show-toplevel 2>/dev/null) - # if [ "$toplevel" ] - # switch $git_dir/ - # case $toplevel/\* - # echo $git_dir - # end - # end - return - end - - set -l project_dir (__bobthefish_dirname $git_dir) - - switch $real_pwd/ - case $project_dir/\* - echo $project_dir - return - end - - set project_dir (command git rev-parse --show-toplevel 2>/dev/null) - switch $real_pwd/ - case $project_dir/\* - echo $project_dir - end -end - -function __bobthefish_fossil_project_dir -S -a real_pwd -d 'Print the current fossil project base directory' - [ "$theme_display_fossil" = 'yes' ] - and command -q fossil - and set -f dir (command fossil json status 2>/dev/null | grep localRoot | string split ':' -f2 | string trim --chars='"/,') - or return - - set -q theme_vcs_ignore_paths - and [ (__bobthefish_ignore_vcs_dir $real_pwd) ] - and return - - echo "/$dir" -end - -function __bobthefish_hg_project_dir -S -a real_pwd -d 'Print the current hg project base directory' - [ "$theme_display_hg" = 'yes' ] - and command -q hg - or return - - set -q theme_vcs_ignore_paths - and [ (__bobthefish_ignore_vcs_dir $real_pwd) ] - and return - - set -l d $real_pwd - while not [ -z "$d" ] - if [ -e $d/.hg ] - command hg root --cwd "$d" 2>/dev/null - return - end - - [ "$d" = '/' ] - and return - - set d (__bobthefish_dirname $d) - end -end - -function __bobthefish_project_pwd -S -a project_root_dir -a real_pwd -d 'Print the working directory relative to project root' - set -q theme_project_dir_length - or set -l theme_project_dir_length 0 - - set -l project_dir (string replace -r '^'(__bobthefish_escape_regex "$project_root_dir")'($|/)' '' $real_pwd) - - if [ $theme_project_dir_length -eq 0 ] - echo -n $project_dir - return - end - - string replace -ar '(\.?[^/]{'"$theme_project_dir_length"'})[^/]*/' '$1/' $project_dir -end - -function __bobthefish_git_ahead -S -d 'Print the ahead/behind state for the current branch' - if [ "$theme_display_git_ahead_verbose" = 'yes' ] - __bobthefish_git_ahead_verbose - return - end - - set -l ahead 0 - set -l behind 0 - for line in (command git rev-list --left-right '@{upstream}...HEAD' 2>/dev/null) - switch "$line" - case '>*' - if [ $behind -eq 1 ] - echo '±' - return - end - set ahead 1 - case '<*' - if [ $ahead -eq 1 ] - echo "$git_plus_minus_glyph" - return - end - set behind 1 - end - end - - if [ $ahead -eq 1 ] - echo "$git_plus_glyph" - else if [ $behind -eq 1 ] - echo "$git_minus_glyph" - end -end - -function __bobthefish_git_ahead_verbose -S -d 'Print a more verbose ahead/behind state for the current branch' - set -l commits (command git rev-list --left-right '@{upstream}...HEAD' 2>/dev/null) - or return - - set -l behind (count (for arg in $commits; echo $arg; end | command grep '^<')) - set -l ahead (count (for arg in $commits; echo $arg; end | command grep -v '^<')) - - switch "$ahead $behind" - case '' # no upstream - case '0 0' # equal to upstream - return - case '* 0' # ahead of upstream - echo "$git_ahead_glyph$ahead" - case '0 *' # behind upstream - echo "$git_behind_glyph$behind" - case '*' # diverged from upstream - echo "$git_ahead_glyph$ahead$git_behind_glyph$behind" - end -end - -function __bobthefish_git_dirty_verbose -S -d 'Print a more verbose dirty state for the current working tree' - set -l changes (command git diff --numstat | awk '{ added += $1; removed += $2 } END { print "+" added "/-" removed }') - or return - - echo "$changes " | string replace -r '(\+0/(-0)?|/-0)' '' -end - -function __bobthefish_git_stashed -S -d 'Print the stashed state for the current branch' - if [ "$theme_display_git_stashed_verbose" = 'yes' ] - set -l stashed (command git rev-list --walk-reflogs --count refs/stash 2>/dev/null) - or return - - echo -n "$git_stashed_glyph$stashed" - else - command git rev-parse --verify --quiet refs/stash >/dev/null - and echo -n "$git_stashed_glyph" - end -end - - -# ============================== -# Segment functions -# ============================== - -function __bobthefish_start_segment -S -d 'Start a prompt segment' - set -l bg $argv[1] - set -e argv[1] - set -l fg $argv[1] - set -e argv[1] - - set_color normal # clear out anything bold or underline... - set_color -b $bg $fg $argv - - switch "$__bobthefish_current_bg" - case '' - # If there's no background, just start one - echo -n ' ' - case "$bg" - # If the background is already the same color, draw a separator - echo -ns $right_arrow_glyph ' ' - case '*' - # otherwise, draw the end of the previous segment and the start of the next - set_color $__bobthefish_current_bg - echo -ns $right_black_arrow_glyph ' ' - set_color $fg $argv - end - - set __bobthefish_current_bg $bg -end - -function __bobthefish_path_segment -S -a segment_dir -a path_type -d 'Display a shortened form of a directory' - set -l segment_color $color_path - set -l segment_basename_color $color_path_basename - - if not [ -w "$segment_dir" ] - set segment_color $color_path_nowrite - set segment_basename_color $color_path_nowrite_basename - end - - __bobthefish_start_segment $segment_color - - set -l directory - set -l parent - - switch "$segment_dir" - case / - set directory '/' - case "$HOME" - set directory '~' - case '*' - set parent (__bobthefish_pretty_parent "$segment_dir") - set directory (__bobthefish_basename "$segment_dir") - end - - [ "$theme_show_project_parent" != "no" -o "$path_type" != "project" ] - and echo -n $parent - - set_color -b $segment_basename_color - echo -ns $directory ' ' -end - -function __bobthefish_finish_segments -S -d 'Close open prompt segments' - if [ -n "$__bobthefish_current_bg" ] - set_color normal - set_color $__bobthefish_current_bg - echo -ns $right_black_arrow_glyph ' ' - end - - if [ "$theme_newline_cursor" = 'yes' ] - echo -ens "\n" - set_color $fish_color_autosuggestion - - if set -q theme_newline_prompt - echo -ens "$theme_newline_prompt" - else if [ "$theme_powerline_fonts" = 'no' -a "$theme_nerd_fonts" != 'yes' ] - echo -ns '> ' - else - echo -ns "$right_arrow_glyph " - end - else if [ "$theme_newline_cursor" = 'clean' ] - echo -ens "\n" - end - - set_color normal - set __bobthefish_current_bg -end - - -# ============================== -# Status segment -# ============================== - -function __bobthefish_prompt_status -S -a last_status -d 'Display flags for a non-zero exit status, private mode, root user, and background jobs' - set -l nonzero - set -l superuser - set -l bg_jobs - - # Last exit was nonzero - [ $last_status -ne 0 ] - and set nonzero 1 - - # If superuser (uid == 0) - # - # Note that iff the current user is root and '/' is not writeable by root this - # will be wrong. But I can't think of a single reason that would happen, and - # it is literally 99.5% faster to check it this way, so that's a tradeoff I'm - # willing to make. - [ -w / -o -w /private/ ] - and [ (id -u) -eq 0 ] - and set superuser 1 - - # Jobs display - if set -q AUTOJUMP_SOURCED - # Autojump special case: check if there are jobs besides the `autojump` - # job, since that one is (briefly) backgrounded every time we `cd` - set bg_jobs (jobs -c | string match -v --regex '(Command|autojump)' | wc -l) - [ "$bg_jobs" -eq 0 ] - and set bg_jobs # clear it out so it doesn't show when `0` - else - if [ "$theme_display_jobs_verbose" = 'yes' ] - set bg_jobs (jobs -p | wc -l) - [ "$bg_jobs" -eq 0 ] - and set bg_jobs # clear it out so it doesn't show when `0` - else - # `jobs -p` is faster if we redirect to /dev/null, because it exits - # after the first match. We'll use that unless the user wants to - # display the actual job count - jobs -p >/dev/null - and set bg_jobs 1 - end - end - - if [ "$nonzero" -o "$fish_private_mode" -o "$superuser" -o "$bg_jobs" ] - __bobthefish_start_segment $color_initial_segment_exit - if [ "$nonzero" ] - set_color normal - set_color -b $color_initial_segment_exit - if [ "$theme_show_exit_status" = 'yes' ] - echo -ns $last_status ' ' - else - echo -n $nonzero_exit_glyph - end - end - - if [ "$fish_private_mode" ] - set_color normal - set_color -b $color_initial_segment_private - echo -n $private_glyph - end - - if [ "$superuser" ] - set_color normal - if [ -z "$FAKEROOTKEY" ] - set_color -b $color_initial_segment_su - else - set_color -b $color_initial_segment_exit - end - - echo -n $superuser_glyph - end - - if [ "$bg_jobs" ] - set_color normal - set_color -b $color_initial_segment_jobs - if [ "$theme_display_jobs_verbose" = 'yes' ] - echo -ns $bg_job_glyph $bg_jobs ' ' - else - echo -n $bg_job_glyph - end - end - end -end - - -# ============================== -# Container and VM segments -# ============================== - -function __bobthefish_prompt_vagrant -S -d 'Display Vagrant status' - [ "$theme_display_vagrant" = 'yes' -a -f Vagrantfile ] - or return - - # .vagrant/machines/$machine/$provider/id - for file in .vagrant/machines/*/*/id - read -l id <"$file" - - if [ -n "$id" ] - switch "$file" - case '*/virtualbox/id' - __bobthefish_prompt_vagrant_vbox $id - case '*/vmware_fusion/id' - __bobthefish_prompt_vagrant_vmware $id - case '*/parallels/id' - __bobthefish_prompt_vagrant_parallels $id - end - end - end -end - -function __bobthefish_prompt_vagrant_vbox -S -a id -d 'Display VirtualBox Vagrant status' - set -l vagrant_status - set -l vm_status (VBoxManage showvminfo --machinereadable $id 2>/dev/null | command grep 'VMState=' | tr -d '"' | cut -d '=' -f 2) - - switch "$vm_status" - case 'running' - set vagrant_status "$vagrant_status$vagrant_running_glyph" - case 'poweroff' - set vagrant_status "$vagrant_status$vagrant_poweroff_glyph" - case 'aborted' - set vagrant_status "$vagrant_status$vagrant_aborted_glyph" - case 'saved' - set vagrant_status "$vagrant_status$vagrant_saved_glyph" - case 'stopping' - set vagrant_status "$vagrant_status$vagrant_stopping_glyph" - case '' - set vagrant_status "$vagrant_status$vagrant_unknown_glyph" - end - - [ -z "$vagrant_status" ] - and return - - __bobthefish_start_segment $color_vagrant - echo -ns $vagrant_status ' ' -end - -function __bobthefish_prompt_vagrant_vmware -S -a id -d 'Display VMWare Vagrant status' - set -l vagrant_status - if [ (pgrep -f "$id") ] - set vagrant_status "$vagrant_status$vagrant_running_glyph" - else - set vagrant_status "$vagrant_status$vagrant_poweroff_glyph" - end - - [ -z "$vagrant_status" ] - and return - - __bobthefish_start_segment $color_vagrant - echo -ns $vagrant_status ' ' -end - -function __bobthefish_prompt_vagrant_parallels -S -d 'Display Parallels Vagrant status' - set -l vagrant_status - set -l vm_status (prlctl list $id -o status 2>/dev/null | command tail -1) - - switch "$vm_status" - case 'running' - set vagrant_status "$vagrant_status$vagrant_running_glyph" - case 'stopped' - set vagrant_status "$vagrant_status$vagrant_poweroff_glyph" - case 'paused' - set vagrant_status "$vagrant_status$vagrant_saved_glyph" - case 'suspended' - set vagrant_status "$vagrant_status$vagrant_saved_glyph" - case 'stopping' - set vagrant_status "$vagrant_status$vagrant_stopping_glyph" - case '' - set vagrant_status "$vagrant_status$vagrant_unknown_glyph" - end - - [ -z "$vagrant_status" ] - and return - - __bobthefish_start_segment $color_vagrant - echo -ns $vagrant_status ' ' -end - -function __bobthefish_prompt_docker -S -d 'Display Docker machine name' - [ "$theme_display_docker_machine" = 'no' -o -z "$DOCKER_MACHINE_NAME" ] - and return - - __bobthefish_start_segment $color_vagrant - echo -ns $DOCKER_MACHINE_NAME ' ' -end - -function __bobthefish_k8s_context -S -d 'Get the current k8s context' - set -l config_paths "$HOME/.kube/config" - [ -n "$KUBECONFIG" ] - and set config_paths (string split ':' "$KUBECONFIG") $config_paths - - for file in $config_paths - [ -f "$file" ] - or continue - - while read -l key val - if [ "$key" = 'current-context:' ] - set -l context (string trim -c '"\' ' -- $val) - [ -z "$context" ] - and return 1 - - echo $context - return - end - end <$file - end - - return 1 -end - -function __bobthefish_k8s_namespace -S -d 'Get the current k8s namespace' - kubectl config view --minify --output "jsonpath={..namespace}" -end - -function __bobthefish_prompt_k8s_context -S -d 'Show current Kubernetes context' - [ "$theme_display_k8s_context" = 'yes' ] - or return - - set -l context (__bobthefish_k8s_context) - or return - - [ "$theme_display_k8s_namespace" = 'yes' ] - and set -l namespace (__bobthefish_k8s_namespace) - - [ -z "$context" -o "$context" = 'default' ] - and [ -z "$namespace" -o "$namespace" = 'default' ] - and return - - set -l segment $k8s_glyph ' ' - [ "$context" != 'default' ] - and set segment $segment $context - [ "$namespace" != 'default' ] - and set segment $segment ':' $namespace - - __bobthefish_start_segment $color_k8s - echo -ns $segment ' ' -end - - -# ============================== -# Cloud Tools -# ============================== - -function __bobthefish_prompt_aws_vault_profile -S -d 'Show AWS Vault profile' - [ "$theme_display_aws_vault_profile" = 'yes' ] - or return - - [ -n "$AWS_VAULT" -a -n "$AWS_CREDENTIAL_EXPIRATION" ] - or return - - set -l profile $AWS_VAULT - - set -l now (date --utc +%s) - set -l expiry (date -d "$AWS_CREDENTIAL_EXPIRATION" +%s) - set -l diff_mins (math "floor(( $expiry - $now ) / 60)") - - set -l diff_time $diff_mins"m" - [ $diff_mins -le 0 ] - and set -l diff_time '0m' - [ $diff_mins -ge 60 ] - and set -l diff_time (math "floor($diff_mins / 60)")"h"(math "$diff_mins % 60")"m" - - set -l segment $profile ' (' $diff_time ')' - set -l status_color $color_aws_vault - [ $diff_mins -le 0 ] - and set -l status_color $color_aws_vault_expired - - __bobthefish_start_segment $status_color - echo -ns $segment ' ' -end - - -# ============================== -# User / hostname info segments -# ============================== - -# Polyfill for fish < 2.5.0 -if not type -q prompt_hostname - if not set -q __bobthefish_prompt_hostname - set -g __bobthefish_prompt_hostname (uname -n | string replace -r '\..*' '') - end - - function prompt_hostname - echo $__bobthefish_prompt_hostname - end -end - -function __bobthefish_prompt_user -S -d 'Display current user and hostname' - [ "$theme_display_user" = 'yes' -o \( "$theme_display_user" != 'no' -a -n "$SSH_CLIENT" \) -o \( -n "$default_user" -a "$USER" != "$default_user" \) ] - and set -l display_user - - [ "$theme_display_sudo_user" = 'yes' -a -n "$SUDO_USER" ] - and set -l display_sudo_user - - [ "$theme_display_hostname" = 'yes' -o \( "$theme_display_hostname" != 'no' -a -n "$SSH_CLIENT" \) ] - and set -l display_hostname - - if set -q display_user - __bobthefish_start_segment $color_username - echo -ns (whoami) - end - - if set -q display_sudo_user - if set -q display_user - echo -ns ' ' - else - __bobthefish_start_segment $color_username - end - echo -ns "($SUDO_USER)" - end - - if set -q display_hostname - if set -q display_user - or set -q display_sudo_user - # reset colors without starting a new segment... - # (so we can have a bold username and non-bold hostname) - set_color normal - set_color -b $color_hostname[1] $color_hostname[2..-1] - echo -ns '@' (prompt_hostname) - else - __bobthefish_start_segment $color_hostname - echo -ns (prompt_hostname) - end - end - - set -q display_user - or set -q display_sudo_user - or set -q display_hostname - and echo -ns ' ' -end - - -# ============================== -# Virtual environment segments -# ============================== - -function __bobthefish_rvm_parse_ruby -S -a ruby_string -a scope -d 'Parse RVM Ruby string' - # Function arguments: - # - 'ruby-2.2.3@rails', 'jruby-1.7.19'... - # - 'default' or 'current' - set -l IFS @ - echo "$ruby_string" | read __ruby __rvm_{$scope}_ruby_gemset __ - set IFS - - echo "$__ruby" | read __rvm_{$scope}_ruby_interpreter __rvm_{$scope}_ruby_version __ - set -e __ruby - set -e __ -end - -function __bobthefish_rvm_info -S -d 'Current Ruby information from RVM' - # look for rvm install path - set -q rvm_path - or set -l rvm_path ~/.rvm /usr/local/rvm - - # More `sed`/`grep`/`cut` magic... - set -l __rvm_default_ruby (grep GEM_HOME $rvm_path/environments/default 2>/dev/null | sed -e"s/'//g" | sed -e's/.*\///') - set -l __rvm_current_ruby (rvm-prompt i v g) - - [ "$__rvm_default_ruby" = "$__rvm_current_ruby" ] - and return - - set -l __rvm_default_ruby_gemset - set -l __rvm_default_ruby_interpreter - set -l __rvm_default_ruby_version - set -l __rvm_current_ruby_gemset - set -l __rvm_current_ruby_interpreter - set -l __rvm_current_ruby_version - - # Parse default and current Rubies to global variables - __bobthefish_rvm_parse_ruby $__rvm_default_r |