From ace88cd18f07370c424716e5a1eb586d67cad9d4 Mon Sep 17 00:00:00 2001 From: DustVoice Date: Thu, 24 Oct 2019 17:22:07 +0200 Subject: [PATCH] Make column wrap and strip --- .../construct_column_aware_prompt.fish | 43 +++++++++++++++++++ .config/fish/functions/fish_prompt.fish | 15 ++++++- 2 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 .config/fish/functions/construct_column_aware_prompt.fish diff --git a/.config/fish/functions/construct_column_aware_prompt.fish b/.config/fish/functions/construct_column_aware_prompt.fish new file mode 100644 index 0000000..a255f19 --- /dev/null +++ b/.config/fish/functions/construct_column_aware_prompt.fish @@ -0,0 +1,43 @@ +function construct_column_aware_prompt -a prompt_prefix prompt_dir + set -l prompt_concat "$prompt_prefix$prompt_dir" + + if test (expr length "$prompt_concat") -le $COLUMNS + echo $prompt_prefix$prompt_dir + else + if test (expr length "$prompt_prefix") -le $COLUMNS + set -l split_prompt_concat (string split "/" $prompt_dir) + + if test (count $split_prompt_concat) -lt 2 + echo (string sub -s 1 -l (math $COLUMNS - (expr length "$prompt_prefix")) $prompt_dir) + else + for i in (seq (count $split_prompt_concat)) + if test (expr length "> $split_prompt_concat[$i]/") -gt $COLUMNS + set split_prompt_concat[$i] (string sub -s 1 -l (math $COLUMNS - 8) $split_prompt_concat[$i])'[...]' + end + end + + set -l tmp_lines "$prompt_prefix" + + for item in $split_prompt_concat + set -l tmp_line "$tmp_lines$item/" + if test (expr length "$tmp_line") -le $COLUMNS + set tmp_lines $tmp_line + else + echo $tmp_lines + set tmp_lines "> $item/" + end + end + + set -l tmp_lines_length (expr length "$tmp_lines") + if test $tmp_lines_length -gt 0 + echo (string sub -s 1 -l (math $tmp_lines_length - 1) $tmp_lines) + end + end + else + set -l rec (construct_column_aware_prompt "" $prompt_dir) + for item in rec + echo $item + end + end + end +end diff --git a/.config/fish/functions/fish_prompt.fish b/.config/fish/functions/fish_prompt.fish index 9e8ef5f..727b296 100644 --- a/.config/fish/functions/fish_prompt.fish +++ b/.config/fish/functions/fish_prompt.fish @@ -5,9 +5,20 @@ function fish_prompt --description 'Write out the prompt' set prompt_symbol '$' end + set -l prompt_prefix (set_color yellow)(whoami)'@'(set_color purple)(prompt_hostname)' ' + set -l prompt_dir (set_color green)(prompt_pwd) + + set -l column_aware_prompt (construct_column_aware_prompt $prompt_prefix $prompt_dir) + if test -z $WINDOW - printf '%s%s@%s%s %s%s%s %s ' (set_color yellow) (whoami) (set_color purple) (prompt_hostname) (set_color green) (prompt_pwd) (set_color normal) $prompt_symbol + for dir in $column_aware_prompt + echo $dir + end + + set_color normal + + echo $prompt_symbol' ' else - printf '%s%s@%s%s%s (%s)%s%s%s %s ' (set_color yellow) (whoami) (set_color purple) (prompt_hostname) (set_color white) (echo $WINDOW) (set_color green) (prompt_pwd) (set_color normal) $prompt_symbol + printf '%s%s@%s%s%s (%s)%s%s%s\n%s ' (set_color yellow) (whoami) (set_color purple) (prompt_hostname) (set_color white) (echo $WINDOW) (set_color green) (prompt_pwd) (set_color normal) $prompt_symbol end end