Так, херня, учил TCL

  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
#!/bin/sh
# \
exec tclkit "$0" ${1+"$@"}
source [file join [file dirname [info script]] util.tcl]
proc read-list {filename} {
set lst [list]
if {! ([file exists $filename] && [file isfile $filename])} {
return $lst
}
set fd [open $filename r]
fconfigure $fd -encoding utf-8
set cnt 1
while {! [eof $fd]} {
set line [gets $fd]
if {$line!="" && ! [regexp {^#} $line]} {
lappend lst [list $line $cnt]
incr cnt
}
}
close $fd
return $lst
}
proc save-list {lst filename} {
set fd [open $filename w]
fconfigure $fd -encoding utf-8
foreach item [lsort -index 1 -integer $lst] {
puts $fd [lindex $item 0]
}
close $fd
}
proc done {line} {
return [regexp {^x\s+.*} [lindex $line 0]]
}
proc do-it {line} {
if {! [done $line]} {
set done [clock format [clock seconds] -format " \[x%Y%m%d\]"]
return [list "x [lindex $line 0]$done" [lindex $line 1]]
} else {
return $line
}
}
proc undo-it {line} {
if [done $line] {
set txt [lindex $line 0]
regsub {^x\s+} $txt {} txt
return [list $txt [lindex $line 1]]
} else {
return $line
}
}
proc item2pos {list item} {
set pos -1
set p 0
foreach line $list {
if {[lindex $line 1]==$item} {
set pos $p
}
incr p
}
return $pos
}
proc do-item {lst item} {
set pos [item2pos $lst $item]
if {$pos>=0} {
set lst [lreplace $lst $pos $pos [do-it [lindex $lst $pos]]]
}
return $lst
}
proc undo-item {lst item} {
set pos [item2pos $lst $item]
if {$pos>=0} {
set lst [lreplace $lst $pos $pos [undo-it [lindex $lst $pos]]]
}
return $lst
}
proc dump {list expr all} {
foreach line [lsort -index 0 $list] {
if [regexp -nocase [escape $expr] [lindex $line 0]] {
if {$all || ! [regexp -nocase {^x\s+} [lindex $line 0]]} {
puts "[format "%3s" [lindex $line 1]]: [lindex $line 0]"
}
}
}
}
proc add-task {lst task} {
set index [llength $lst]
incr index
append task [clock format [clock seconds] -format " \[%Y%m%d\]"]
lappend lst [list $task $index]
}
#-------------------------------------------------------------------------------
set args [list]
set cmd list
set all 0
foreach arg $argv {
switch -regexp $arg {
^-l {set cmd list}
^-dump {set cmd list}
^-add {set cmd add}
^-do {set cmd do}
^-un {set cmd undo}
^-arc {set cmd archive}
^-all {set all 1}
default {lappend args [encoding convertfrom cp1251 $arg]}
}
}
set todofile [file join $env(HOME) todo todo.txt]
set donefile [file join $env(HOME) todo done.txt]
set todolist [read-list $todofile]
set donelist [read-list $todofile]
switch $cmd {
list {
if [llength $args] {
foreach expr $args {puts "---- $expr"
dump $todolist $expr $all}
} else {dump $todolist "" $all}
}
add {
set todolist [add-task $todolist [join $args]];
save-list $todolist $todofile
}
do {
foreach item $args {set todolist [do-item $todolist $item]}
save-list $todolist $todofile
}
undo {
foreach item $args {set todolist [undo-item $todolist $item]}
save-list $todolist $todofile
}
}