-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdt
executable file
·88 lines (77 loc) · 2.31 KB
/
dt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
DISTRACKED_DIR="$HOME/.distracked"
init() {
if [[ ! -e "$DISTRACKED_DIR" ]]; then mkdir "$DISTRACKED_DIR"; fi
cd "$DISTRACKED_DIR"; if [[ ! -e "main" ]]; then touch "main"; fi
# The rest of the script will assume that:
# - We are in the configuration directory.
# - There is at least one stack, called "main".
}
current_stack() {
ls -t |head -n 1
}
stack_list() {
echo -n '* '
ls -t |cat
}
stack_head() {
tail -n 1 "$*"
}
docstr() {
true
}
do_subcmd() {
subcmd="$1"
shift
init
case "$subcmd" in
"all" ) docstr "display entire contents of all stacks";
for f in $(ls -t); do echo -ne "\n-- $f --\n* "; tac "$f"; done;;
"cat" ) docstr "display entire contents of current stack";
echo -ne "-- $(current_stack) --\n* " ; tac "$(current_stack)" ;;
"edit" ) docstr "open all stacks in text editor for bulk modification";
editor $(ls -t) ;;
"head" ) docstr "display top of stack";
echo -n '* ' ; stack_head "$(current_stack)" ;;
"heads" ) docstr "display tops of all stacks";
for f in $(ls -t); do echo -ne "\n-- $f --\n* "; stack_head "$f"; done;;
"help" ) docstr "display this message";
return 1 ;;
"ls" ) docstr "list all stacks";
stack_list ;;
"pop" ) docstr "remove top of stack";
stack_head "$(current_stack)" > .yank
sed -i.bak '$d' "$(current_stack)"; rm *.bak ;;
"push" ) docstr "add given (default: last popped) item at top of stack";
item=${*:-$(cat .yank)}
echo "$item" >> "$(current_stack)" ;;
"rm" ) docstr "delete given stack";
rm "$*" ;;
"switch") docstr "switch to given stack, creating it if necessary";
touch "$1" ; stack_list ;;
"" ) echo -n '* ' ; stack_head "$(current_stack)" ;;
*)
# Unrecognized subcmd
return 1
;;
esac
return 0
}
usage() {
echo "usage: $0 [subcmd]"
((echo "subcmd: | behavior:"
declare -f do_subcmd |\
grep -B1 '\bdocstr\b' |\
grep -o '".*"' |\
grep -o '[^"]*' |\
sed 'N;s/\n/ | /'
) | column -s '|' -t
) | sed 's/^/ /'
exit 1
}
# Script entry point
do_subcmd "$@"
if [[ "$?" -ne 0 ]]
then
usage
fi