Skip to content

Commit

Permalink
feat: truncate prompt current_directory and title to keep last compon…
Browse files Browse the repository at this point in the history
…ents

close #336
  • Loading branch information
edouard-lopez committed Jan 23, 2024
1 parent 586869b commit e2377ea
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ Fully **customizable** (colors, symbols and features):
- Async update when [configured with fish-async-prompt](https://github.com/pure-fish/pure/wiki/Async-git-Prompt) ;
- Update terminal title with _current folder_ and _command_ ;
- Detect when running in a container ;
- Shorten _current folder_ component 🏴;
- Shorten _current folder_ component in prompt and window title 🏴;
- Truncate _current folder_ component in prompt and window title 🏴;

🏴: Enabled or disabled via a [feature flag](#-features-flags).

Expand Down Expand Up @@ -117,6 +118,8 @@ set --universal pure_color_system_time pure_color_mute
| **`pure_show_subsecond_command_duration`** | `false` | Show subsecond (ex. 1.5s) in command duration. |
| **`pure_show_system_time`** | `false` | `true`: shows system time before the prompt symbol (as `%H:%M:%S`). |
| **`pure_threshold_command_duration`** | `5` | Show command duration when above this value (seconds). |
| **`pure_truncate_prompt_current_directory_keeps`** | `0` | Truncate working directory path in prompt, but keeps the last to `n` components (`0` full path in current directory) |
| **`pure_truncate_window_title_current_directory_keeps`** | `0` | Truncate working directory path in window title, but keeps the last to `n` components (`0` full path in window title) |

### 🎨 Colours

Expand Down
2 changes: 2 additions & 0 deletions conf.d/pure.fish
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ _pure_set_default pure_color_prompt_on_success pure_color_success
# Current Working Directory
_pure_set_default pure_color_current_directory pure_color_primary
_pure_set_default pure_shorten_prompt_current_directory_length 0
_pure_set_default pure_truncate_prompt_current_directory_keeps -1

# Git
_pure_set_default pure_enable_git true
Expand Down Expand Up @@ -75,6 +76,7 @@ _pure_set_default pure_reverse_prompt_symbol_in_vimode true
# Title
_pure_set_default pure_symbol_title_bar_separator -
_pure_set_default pure_shorten_window_title_current_directory_length 0
_pure_set_default pure_truncate_window_title_current_directory_keeps -1

# Check for new release on startup
_pure_set_default pure_check_for_new_release false
Expand Down
9 changes: 9 additions & 0 deletions functions/_pure_parse_directory.fish
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,14 @@ function _pure_parse_directory \
set folder (fish_prompt_pwd_dir_length=1 prompt_pwd)
end
end

if test $pure_truncate_prompt_current_directory_keeps -ge 1
set folder (
string split '/' $folder \
| tail -n $pure_truncate_prompt_current_directory_keeps \
| string join '/'
)
end

echo $folder
end
12 changes: 12 additions & 0 deletions tests/_pure.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,15 @@ before_all
source (status dirname)/../conf.d/pure.fish
echo $pure_color_k8s_namespace
) = pure_color_primary

@test "configure: pure_truncate_prompt_current_directory_keeps" (
set --erase pure_truncate_prompt_current_directory_keeps
source (status dirname)/../conf.d/pure.fish
echo $pure_truncate_prompt_current_directory_keeps
) = -1

@test "configure: pure_truncate_window_title_current_directory_keeps" (
set --erase pure_truncate_window_title_current_directory_keeps
source (status dirname)/../conf.d/pure.fish
echo $pure_truncate_window_title_current_directory_keeps
) = -1
43 changes: 39 additions & 4 deletions tests/_pure_parse_directory.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,39 @@ source (status dirname)/../functions/_pure_parse_directory.fish
@echo (_print_filename (status filename))


function before_all
function before_each
_purge_configs
_disable_colors
set --universal pure_shorten_prompt_current_directory_length 0
set --universal pure_truncate_prompt_current_directory_keeps 0
end

function after_all
function after_each
set --erase pure_shorten_prompt_current_directory_length
end

before_all
before_each
@test "_pure_parse_directory: returns current directory" (
mkdir -p /tmp/current/directory/
cd /tmp/current/directory/

_pure_parse_directory
) = $PWD

before_each
@test '_pure_parse_directory: replaces $HOME by ~' (
pushd $HOME

_pure_parse_directory
popd
) = '~'

before_each
@test '_pure_parse_directory: shortens directory in prompt' (
string length (_pure_parse_directory 1)
) -lt (string length $PWD)

before_each
@test '_pure_parse_directory: shorten current directory' (
set --universal pure_shorten_prompt_current_directory_length 2

Expand All @@ -40,5 +44,36 @@ before_all

_pure_parse_directory
) = /tm/cu/directory
after_each

before_each
@test '_pure_parse_directory: truncate 1, keeps only last directory name from current directory path' (
set --universal pure_truncate_prompt_current_directory_keeps 1

mkdir -p /tmp/current/directory/
cd /tmp/current/directory/

_pure_parse_directory
) = directory

before_each
@test '_pure_parse_directory: truncate n last component from current directory path' (
set --universal pure_truncate_prompt_current_directory_keeps 2

mkdir -p /tmp/current/directory/
cd /tmp/current/directory/

_pure_parse_directory
) = current/directory

before_each
@test '_pure_parse_directory: truncate 0, keeps full current directory path' (
set --universal pure_truncate_prompt_current_directory_keeps 0

mkdir -p /tmp/current/directory/
cd /tmp/current/directory/

_pure_parse_directory
) = /tmp/current/directory


after_all
1 change: 1 addition & 0 deletions tests/_pure_prompt_current_folder.test.fish
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ function before_all
_purge_configs
_disable_colors
set --universal pure_shorten_prompt_current_directory_length 0
set --universal pure_truncate_prompt_current_directory_keeps 0
end

function after_all
Expand Down

0 comments on commit e2377ea

Please sign in to comment.