Skip to content

Commit

Permalink
feat: change default dir to ~/.local/share/to-fish/
Browse files Browse the repository at this point in the history
...so that we don't clutter up HOME.

If ~/.tofish already exists, keep using that.
  • Loading branch information
joehillen committed Apr 25, 2023
1 parent a84843a commit 2170d96
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ A directory bookmarking tool for fish-shell.

## Usage

`to` puts bookmarks in the directory `~/.tofish`
`to` puts bookmarks in the directory `~/.local/share/to-fish/`

Use `set -U TO_DIR` to change where bookmarks are stored.

Expand All @@ -15,12 +15,16 @@ Usage:
to add [BOOKMARK] [DEST] Create a BOOKMARK for DEST
Default BOOKMARK: name of current directory
Default DEST: path to current directory
to add DEST Create a bookmark for DEST if it is a directory
to ls List all bookmarks
to mv OLD NEW Change the name of a bookmark from OLD to NEW
to rm BOOKMARK Remove BOOKMARK
to clean Remove bookmarks that have a missing destination
to resolve BOOKMARK Print the destination of a bookmark
to help Show this message
Bookmarks are stored in: ~/.local/share/to-fish
To change, run: set -U TO_DIR <dir>
```

## Installation
Expand Down
37 changes: 31 additions & 6 deletions functions/to.fish
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ function __to_usage
echo ' to add [BOOKMARK] [DEST] Create a BOOKMARK for DEST'
echo ' Default BOOKMARK: name of current directory'
echo ' Default DEST: path to current directory'
echo ' to add DEST Create a bookmark for DEST if it is a directory'
echo ' to ls List all bookmarks'
echo ' to mv OLD NEW Change the name of a bookmark from OLD to NEW'
echo ' to rm BOOKMARK Remove BOOKMARK'
Expand All @@ -17,12 +18,36 @@ function __to_usage
return 1
end

# https://github.com/fish-shell/fish-shell/issues/6173#issuecomment-1067114363
function is_empty_dir
test -d "$argv"
or return 1 # not a directory, so not an empty directory
# count counts how many arguments it received
# if this glob doesn't match, it won't get arguments
# and so it will return 1
# because we *want* an empty directory, turn that around.
# the `{.*,*}` ensures it matches hidden files as well.
not count $argv/{.*,*} >/dev/null
end

function __to_dir
if test -z "$TO_DIR"
echo ~/.tofish
else
if test -n "$TO_DIR"
echo $TO_DIR
return
end

set -l dir

if test -d "$HOME/.tofish" && not is_empty_dir $HOME/.tofish
set dir $HOME/.tofish
else if test -n "$XDG_DATA_HOME"
set dir $XDG_DATA_HOME/to-fish
else
set dir $HOME/.local/share/to-fish
end

set -U TO_DIR $dir
echo $TO_DIR
end

function __to_bm_path
Expand Down Expand Up @@ -52,11 +77,11 @@ function __to_complete_directories
set -l cl (commandline -ct | string split -m 1 /)
set -l bm $cl[1]
set -l bmdir (__to_resolve $bm 2>/dev/null)
if test -z $bmdir
if test -z "$bmdir"
__fish_complete_directories
else
set -e cl[1]
if test -z $cl
if test -z "$cl"
__fish_complete_directories $bmdir/ | string replace -r 'Directory$' $bm
else
__fish_complete_directories $bmdir/$cl | string replace -r 'Directory$' $bm
Expand Down Expand Up @@ -91,7 +116,7 @@ function to -d 'Bookmarking tool'

# Create tofish directory
if not test -d "$dir"
if mkdir "$dir"
if command mkdir $dir
echo "Created bookmark directory: $dir"
else
echo "Failed to Create bookmark directory: $dir"
Expand Down

0 comments on commit 2170d96

Please sign in to comment.