-
Notifications
You must be signed in to change notification settings - Fork 0
/
todo
117 lines (99 loc) · 3.51 KB
/
todo
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/bin/bash
# File to store todos
TODO_FILE="/tmp/todo.tmp"
TODAY=$(date +"%Y-%m-%d")
MAX_TASK_LEN=30
WIDTH=$(( $(tput cols) - 6 ))
COLORS=(31 32 33 34 35 36) # Color codes for tasks (red, green, yellow, blue, magenta, cyan)
COLOR_NAMES=("Red" "Green" "Yellow" "Blue" "Magenta" "Cyan")
# Ensure TODO_FILE exists
[ ! -f "$TODO_FILE" ] && touch "$TODO_FILE"
# Add today's date if not present
grep -q "^$TODAY" "$TODO_FILE" || echo -e "\n$TODAY" >> "$TODO_FILE"
# Functions
print_tasks() {
clear
local date=${1:-$TODAY}
leading_spaces=$(( (WIDTH / 2) - (${#date} / 2) ))
printf "%*s" "$leading_spaces" ""
echo -e "\033[48;5;7;30;1m $date \033[0m"
awk -v date="$date" -v maxlen="$MAX_TASK_LEN" -v width="$WIDTH" -v colors="${COLORS[*]}" '
BEGIN { split(colors, colArr, " "); count=0 }
$0 ~ date { showing=1; next }
/^[0-9]{4}-[0-9]{2}-[0-9]{2}/ && showing { exit }
showing && $0 != "" {
color = colArr[count % length(colArr)]
count++
task = $0
if (length(task) < width) { task = sprintf("%-*s", width, task) }
else if (length(task) > width) { task = substr(task, 1, width-1) ">" }
printf "\033[48;5;%sm%3d: %s\033[0m\n", color, count, task
}
' "$TODO_FILE"
}
add_task() {
local task=$1
echo "$task" | awk -v len="$MAX_TASK_LEN" '{ if(length > len) { print substr($0, 1, len-1) ">" } else { print } }' >> "$TODO_FILE"
}
remove_task() {
local date=${1:-$TODAY}
local task_list
task_list=$(awk -v date="$date" '
$0 ~ date { showing=1; next }
/^[0-9]{4}-[0-9]{2}-[0-9]{2}/ && showing { showing=0 }
showing && $0 != "" { print }
' "$TODO_FILE" | fzf --ansi --prompt="Select task to remove: ")
[ -z "$task_list" ] && return 1
local task_index=$(awk -v task="$task_list" '$0 == task { print NR-1; exit }' "$TODO_FILE")
awk 'NR != idx' idx="$task_index" "$TODO_FILE" > "${TODO_FILE}.tmp" && mv "${TODO_FILE}.tmp" "$TODO_FILE"
}
list_dates() {
local date=$(awk '/^[0-9]{4}-[0-9]{2}-[0-9]{2}/ { print }' "$TODO_FILE" | fzf --prompt="Select date: ")
[ -n "$date" ] && print_tasks "$date"
}
change_task_color() {
local date=${1:-$TODAY}
local task_list
task_list=$(awk -v date="$date" '
$0 ~ date { showing=1; next }
/^[0-9]{4}-[0-9]{2}-[0-9]{2}/ && showing { showing=0 }
showing && $0 != "" { print }
' "$TODO_FILE" | fzf --ansi --prompt="Select task to change color: ")
[ -z "$task_list" ] && return 1
local task_index=$(awk -v task="$task_list" '$0 == task { print NR-1; exit }' "$TODO_FILE")
echo -e "\nChoose a color:"
for i in "${!COLORS[@]}"; do
echo -e "\033[48;5;${COLORS[i]}m ${COLOR_NAMES[i]} ($i) \033[0m"
done
read -p "Enter color index: " color_index
color_index=${color_index:-0} # Default to first color if empty
awk -v idx="$task_index" -v color="${COLORS[color_index]}" 'NR == idx { sub(/\033\[48;5;[0-9]+m/, "\033[48;5;"color"m"); print; next } { print }' "$TODO_FILE" > "${TODO_FILE}.tmp" && mv "${TODO_FILE}.tmp" "$TODO_FILE"
}
print_help(){
echo "Usage: $0 {view [date]|list|add <task>|remove|color}"
}
# Command processing
case "$1" in
view)
print_tasks "$2"
;;
list)
list_dates
;;
add)
shift
add_task "$*"
;;
remove)
remove_task "$2"
;;
color)
change_task_color "$2"
;;
*)
print_tasks "$TODAY"
;;
-h|--help)
print_help
;;
esac