Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add max length feature to cwd #326

Merged
merged 4 commits into from
Feb 16, 2025
Merged

Conversation

Theoreticallyhugo
Copy link
Collaborator

in reference to #324 this pr attempts to implement the suggestion.

tested on macos

not yet documented.

use set -g @dracula-cwd-length "0" to set the max length, where 0 is unrestricted.

whenever the paths length is cut, it is displayed without / or ~/ to the left of it to prevent confusion.

example:
max length is 1:
/ becomes / as usual
/Users becomes /Users as usual
/Users/username becomes ~ as usual
/Users/username/.config becomes ~/.config as usual
/Users/username/.config/tmux becomes tmux. no / to the left indicates a cut path
max length is 3:
/Users/username/.config becomes ~/.config as usual
/Users/username/.config/tmux/plugins becomes ~/.config/tmux/plugins as usual
/Users/username/.config/tmux/plugins/tmux becomes tmux. no / to the left indicates a cut path
max length is 0:
unchanged behaviour to previously

my solution seems hacky, but i couldnt think of anything else at this time of day.
please critique or suggest a different solution.

documentation and testing on linux will follow.

@bmodotdev
Copy link
Contributor

At least one edge case here. For ease of testing, I modified the script to accept limit in $1 and path in $2:

# HOME=/home/user
$ ./scripts/cwd.sh 3 /home/user.bak/one/two/three
~.bak/one space/two/three

It's a small edge case, but it could be devastating to mislead of their current directory; e.g. they could think they are safely deleting files from a specific directory.

I might try something like below:

# Truncation is only needed $cwd contains $HOME,
# which requires $cwd to be greater than $HOME
if (("${#HOME}" < "${#cwd}")); then
  home_length=$((${#HOME} + 1))
  [ "${HOME}/" == "${cwd::$home_length}" ] && cwd="~/${cwd:$home_length}"
fi

I would also add that "@dracula-cwd-length" plugin option might be misinterpreted as length of chars, not length of dirs. Perhaps there is a better name for this option like "depth". Also, speaking of length of chars, this PR doesn't really address it. You may have a dir limit of 3, but still get a huge cwd from this plugin if the dir names themselves are too long. I think it would be good to accommodate that as well.

As well, when a directory is not in the $HOME path, and it exceeds the limit, I think a specific symbol would be better than leaving it blank. Other programs like starship use ".../" or even better, an ellipsis "…/"

And again, testing should probably be quite heavy on this, as it being incorrect could be very damaging.

scripts/cwd.sh Outdated
@@ -1,5 +1,8 @@
#!/usr/bin/env bash

current_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $current_dir/utils.sh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

source $current_dir/utils.sh
^-------------------^ SC1091 (info): Not following: ./utils.sh was not specified as input (see shellcheck -x).
^----------^ SC2086 (info): Double quote to prevent globbing and word splitting.

@Theoreticallyhugo
Copy link
Collaborator Author

hi, thank you very much for your feedback!

i looked into the bad home dir substitution and think i got a fix that may run a little faster than yours (please absolutely do correct me if i'm wrong).
i also added the ellipsis you proposed. i didn't know what other programs were using for this and am happy to choose a more established solution.
and i added the max number of chars in a sensible way ... i hope.

i'd be happy if you could test the changes.

the options are now:

set -g @dracula-cwd-max-dirs "3"
set -g @dracula-cwd-max-chars "10"

as usual, the respective options are ignored when set to "0"

@ethancedwards8 ethancedwards8 merged commit 3e1e7a1 into master Feb 16, 2025
2 checks passed
@ethancedwards8
Copy link
Member

Awesome, thanks!

@Theoreticallyhugo Theoreticallyhugo deleted the feature/cwd_max_length branch February 20, 2025 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants