forked from markwu/todo-cli-plugins
-
Notifications
You must be signed in to change notification settings - Fork 1
/
edit
executable file
·60 lines (48 loc) · 1.41 KB
/
edit
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
#!/bin/bash
# A simple tool to quickly edit one task in todo.txt.
# Requires bash version 4 or greater because it supports 'read -i'
[ -z $EDITOR ] && EDITOR=vim
usage="edit [ITEM#] [SRC]"
[ "$1" = "usage" ] && {
echo " $usage"
echo " If ITEM# given, edits the task. (Requires Bash 4+)"
echo " Otherwise Edits SRC with \$EDITOR ($EDITOR)."
echo " Optional argument (file name) specify the source file."
echo ""
exit 0
}
_fail_usage(){ echo "usage: $TODO_SH $usage"; exit 1; }
### Parse and validate inputs.
task_num=$2
src=$3
# Allow skipping 'ITEM#' param.
if ! [[ $task_num =~ ^[0-9]+$ ]]; then src=$task_num; task_num=0; fi
# Normalize and default SRC
[ -z $src ] && src="todo"
src="$TODO_DIR/$src"
if [ ! -e $src ]; then
src=${src}.txt
if [ ! -e $src ]; then
echo Source File: $src, does not exist!; exit 1;
fi
fi
### Multi-line edit.
if [ $task_num == "0" ]; then
$EDITOR $src
exit 0;
fi
### Single-line edit.
# Ensure the specified task number exists.
max_num=$(sed -n '$=' $src)
if [ $task_num -gt $max_num ]; then
echo ERROR: ITEM# must be $max_num or less.
_fail_usage
fi
# Read original task.
item=$(sed -n ${task_num}p $src)
if [ $? != 0 ]; then exit 1; fi
# accept new task description.
read -e -p "$task_num: " -i """$item""" new_item
if [ $? != 0 ]; then echo "ERROR: Requires Bash version 4 or greater!"; exit 99; fi
# print new task to file.
sed -i "$task_num c \\$new_item" $src