~mapperr/tsk

ref: 3be509fc9290d91bdc760b8c015f1e14e1480ded tsk/tsk -rwxr-xr-x 3.5 KiB
3be509fcmapperr Sorting and aligning 1 year, 11 months ago
                                                                                
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#! /bin/sh

cmd="$1"
test -z "$cmd" || shift

taskdir="$HOME/.tsk"

task_template="tags: 
title: 
due: 
description:
"

helpmsg() {
name=`basename $0`
echo "
$name ls | l [query]
    lists and searchs tasks
    
$name add | a
    adds a new task

$name edit | e task_id
    open EDITOR to edit a task, even if it not exists

$name rm | r task_id
    removes a task

$name git | g [git_args]
    runs 'git git_args' in the task directory

$name sync | y
    syncs: commit changes and pull/push with git

$name clean
    purge your tasks, use with caution
"
}

is_task_list_empty() {
    cd $taskdir
    ls -1 *.md &>/dev/null
    RET=$?
    cd - &>/dev/null
    test ! $RET = 0
}

get_task_file_list() {
    is_task_list_empty && return
    cd $taskdir
    ls -1 *.md
    cd - &>/dev/null
}

get_task_id_list() {
    get_task_file_list | sed 's/\.md$//g' | sort -rn
}

get_task_id_from_filename() {
    local filename="$1"
    basename "$filename" | sed 's/\.md$//'
}

get_task_field() {
    local task_id="$1"
    local field_name="$2"
    cat $taskdir/$task_id.md | grep "^$field_name:" | sed "s/^$field_name://"
}

list_tasks() {
    for task_id in `get_task_id_list`; do
        title=`get_task_field $task_id 'title'`
        tags=`get_task_field $task_id 'tags'`
        tags=`for tag in $tags; do echo -n "[$tag] "; done`
        if echo -n "$task_id" |  grep '^[0-9]$' &>/dev/null; then
            echo "$task_id:  $tags$title"
        else
            echo "$task_id: $tags$title"
        fi
    done
}

is_there_a_task_with_id() {
    local task_id_to_check="$1"
    for task_id in `get_task_id_list`; do
        if [ "$task_id" = "$task_id_to_check" ]; then
            return 0
        fi
    done
    return 1
}

get_first_free_id() {
    counter=1
    while true; do
        if ! is_there_a_task_with_id $counter; then
            echo $counter
            return
        fi
        counter=`expr $counter + 1`
    done
}

get_new_id() {
    task_file_list=`get_task_file_list`
    if test -z "$task_file_list"; then
        echo 1
    else
        get_first_free_id
    fi
}

add_task() {
    newid=`get_new_id`
    newfile=$taskdir/$newid.md
    echo "$task_template" > $newfile
    $EDITOR $newfile
}

edit_task() {
    local task_id="$1"
    $EDITOR $taskdir/$task_id.md
}

remove_task() {
    local task_id="$1"
    task_file="$taskdir/$task_id.md"
    if test ! -f $task_file; then
        echo "task $task_id does not exists"
        return
    fi
    title=`get_task_field $task_id 'title'`
    echo -n "remove task $task_id [$title] (y/n)? "
    read c
    if test "$c" = "y" || test "$c" = "Y"; then
        rm -f $task_file
        echo "task $task_id removed"
    else
        echo "aborted"
    fi
}

clean_tasks() {
    echo -n "delete all tasks (y/n)? "
    read c
    if test "$c" = "y" || test "$c" = "Y"; then
        rm -f $taskdir/*.md
        echo "tasks removed"
    else
        echo "aborted"
    fi
}

test -d $taskdir || mkdir -p $taskdir

if [ "$cmd" = "ls" ] || [ "$cmd" = "l" ]; then
    list_tasks

elif [ "$cmd" = "add" ] || [ "$cmd" = "a" ]; then
    add_task

elif [ "$cmd" = "edit" ] || [ "$cmd" = "e" ]; then
    edit_task $1

elif [ "$cmd" = "rm" ] || [ "$cmd" = "r" ]; then
    remove_task $1

elif [ "$cmd" = "git" ] || [ "$cmd" = "g" ]; then
    cd $taskdir
    git $@

elif [ "$cmd" = "sync" ] || [ "$cmd" = "y" ]; then
    cd $taskdir
    git status | grep 'nothing to commit' &>/dev/null
    test $? = 1 && git add -A && git commit -am 'tsk autosync'
    git pull && git push

elif [ "$cmd" = "clean" ]; then
    clean_tasks

else
    helpmsg
fi