You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
2.0 KiB
Fish

#!/usr/bin/env fish
# get file name from path
function fname
string replace -r "^"(dirname "$argv[1]")/ "" "$argv[1]"
end
# convert a note name or otherwise stuff to a notes path
function n_path
if string match -qr "^"(realpath ~/notes) "$argv[1]"
echo $argv[1]
end
echo (realpath ~/notes)/(string replace -ar "\s" "_" "$argv[1]")
end
# ensure .md ending
function ensure_md
if test (count $argv) -eq 0 -a -t 0
read inp
else
set inp "$argv[1]"
end
if string match -qr "\.md\$" "$inp"
echo "$inp"
else
echo "$inp.md"
end
end
# edit a note, arg1 = note name
function note_edit
eval $EDITOR (n_path "$argv[1]" | ensure_md)
end
# create a new note
# agr1 = note name (path)
# arg2 = (opt) note content
function note_new
set path (n_path "$argv[1]" | ensure_md)
if test -f "$path"
echo "note at $path already exists"
exit 1
end
# create path for note
mkdir -p (dirname "$path")
echo -e "# "(fname "$argv[1]")"\n\n$argv[2]" > $path
note_edit "$path"
end
# show a note
# arg1 = path to note (or folder)
# arg2 = delimiter (default=", "
function note_search
set root (realpath ~/notes)/
set results
set delim ", "
test -n "$argv[2]"; and set delim "$argv[2]"
for f in (rg -l "$argv[1]" ~/notes)
set f (string replace "$root" "" "$f")
set results "$f" $results
end
echo -e (string join "$delim" $results)
end
# print the contents of a note
# arg1 = desired note path
function note_print
set path (n_path "$argv[1]" | ensure_md)
if test ! -f (n_path "$path")
echo "note at $path is not a file!"
exit 1
end
cat "$path"
end
# main
if test "$argv[1]" = "create"
note_new $argv[2..-1]
else if test "$argv[1]" = "edit"
note_edit $argv[2..-1]
else if test "$argv[1]" = "find" -o "$argv[1]" = "search"
note_search $argv[2..-1]
else if test "$argv[1]" = "git"
eval git -C ~/notes/ $argv[2..-1]
else if test "$argv[1]" = "print" -o "$argv[1]" = "show"
note_print $argv[2..-1]
else if test "$argv[1]" = "list" -o "$argv[1]" = "tree"
tree ~/notes/
else
echo "Unknown argument: $argv[1]"
end