code / scripts

Lines748 Shell528 Python72 Lua67 Bourne Again Shell62 make15
1 others 4
Markdown4
(47 lines)
1 #!/bin/sh
2 set -e
3 dir="${XDG_DATA_HOME:-$HOME/.local/share}/foo"
4 mkdir -p "$dir"
5 if [ $# -eq 0 ]; then
6 file="$(printf '%s\n' "$dir"/* | fzf --preview='cat {}')"
7 ext="${file##*.}"
8 else
9 ext="$1"
10 if [ $# -eq 1 ]; then
11 name="$(ask 'Name: ')"
12 else
13 name="$2"
14 fi
15 file="$dir/$name.$ext"
16 if ! [ -s "$file" ]; then
17 touch "$file"
18 case $ext in
19 py|python) echo "#!/usr/bin/env python3" >"$file" ;;
20 lua) echo "#!/usr/bin/env lua" >"$file" ;;
21 sh) echo "#!/bin/sh" >"$file" ;;
22 c) cat <<-EOF >"$file"
23 #!/usr/bin/env -S tcc -run
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 int main(int argc, char *argv[]) {
30 return 0;
32 EOF
33 ;;
34 esac
35 chmod +x "$file"
36 fi
37 fi
39 while true; do
40 action="$(printf '%s\n' Edit Run Delete Quit | fzf --height=6)"
41 case $action in
42 Run) "$file" || true ;;
43 Edit) ${EDITOR:-vim} "$file" ;;
44 Delete) bin "$file" && exit 0 ;;
45 Quit) exit 0 ;;
46 esac
47 done