Last active
October 7, 2024 14:22
-
-
Save darksinge/8c9b73e384540210ae00dab0290fee64 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
set fstate "$HOME/.local/state/tmp-stack.json" | |
function fee | |
if test (count $argv) -eq 0 | |
__fee_push | |
return 0 | |
end | |
if string match -qr '^[-+]?[0-9]+$' -- $argv[1] | |
__fee_at $argv[1] | |
else | |
switch $argv[1] | |
case ls | |
__fee_ls $argv[2..-1] | |
case la | |
__fee_la | |
case rm | |
__fee_rm $argv[2..-1] | |
case pop | |
__fee_pop $argv[2..-1] | |
case push | |
__fee_push | |
case cat | |
__fee_cat $argv[2..-1] | |
case clear | |
__fee_clear | |
case '*' | |
echo "Unknown command: $argv[1]" | |
echo "Usage: fee [ls|la|rm|pop|push|cat|clear|<number>]" | |
echo " ls : List all entries" | |
echo " la : List all entries in JSON format" | |
echo " rm : Remove an entry" | |
echo " pop : Remove and display the last entry" | |
echo " push : Add a new entry (default if no command given)" | |
echo " cat : Display the content of an entry" | |
echo " clear : Remove all entries" | |
echo " <number>: Display the entry at the given index" | |
return 1 | |
end | |
end | |
end | |
function __fee_push | |
set tmp (mktemp) | |
cat > $tmp | |
cat "$tmp" >/dev/stderr | |
set tmpstate (mktemp) | |
set timestamp (date -u +"%Y-%m-%dT%H:%M:%SZ") | |
jq ". + [{\"metadata\": {\"createdAt\": \"$timestamp\"}, \"value\": \"$tmp\"}]" $fstate > $tmpstate | |
mv $tmpstate $fstate | |
echo $tmp | |
end | |
function __fee_ls | |
if test "$argv[1]" = "-1" | |
# print each temp file in plain text without formatting | |
jq -r '.[].value' $fstate | |
return 0 | |
end | |
set lines (jq -r 'to_entries[] | "\(.key) \(.value.metadata.createdAt) \(.value.value)"' $fstate) | |
for line in $lines | |
set -l parts (string split ' ' $line) | |
set -l index $parts[1] | |
set -l timestamp $parts[2] | |
set -l filepath $parts[3] | |
echo (set_color yellow)$index(set_color normal) (set_color cyan)$timestamp(set_color normal) (set_color green)$filepath(set_color normal) | |
end | |
end | |
function __fee_la | |
jq . $fstate | |
end | |
function __fee_pop | |
set tmp (mktemp) | |
set value (jq -r '.[-1].value' $fstate) | |
jq '.[:-1]' $fstate > $tmp && mv $tmp $fstate | |
echo $value | |
end | |
function __fee_at | |
cat $fstate | jq -r ".[$argv].value" | |
end | |
function __fee_cat | |
set index 0 | |
if test (count $argv) -gt 0 | |
set index $argv[0] | |
end | |
echo "argv: $index" | |
set fpath (__fee_at $index) | |
echo "fpath: $fpath" | |
if test -n "$fpath" -a -s "$fpath" | |
cat $fpath | |
else | |
echo "<EMPTY>" | |
end | |
end | |
function __fee_rm | |
set tmpstate (mktemp) | |
set index $argv[1] | |
set removed (jq -r ".[$index].value" $fstate) | |
jq "del(.[$index])" $fstate > $tmpstate | |
mv $tmpstate $fstate | |
echo $removed | |
end | |
function __fee_clear | |
set file_count (gum style --foreground 6 "$(jq 'length' $fstate)") | |
set a (gum style --foreground 3 "This action will remove ") | |
set b (gum style --foreground 3 " files.") | |
set c (gum join "$a" "$file_count") | |
gum join "$c" "$b" | |
if gum confirm "Proceed?" | |
jq -r '.[].value' $fstate | xargs rm -f | |
echo '[]' > $fstate | |
echo "All temporary files cleared and state reset." | |
else | |
echo "Operation cancelled." | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment