-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtodo.psm1
681 lines (559 loc) · 13 KB
/
todo.psm1
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
$assemblyPath = ($PSScriptRoot + '\staging\todotxtlib.net.dll')
$assemblyLoadPath = ($PSScriptRoot + '\lib')
if(!(Test-Path $assemblyLoadPath))
{
New-Item $assemblyLoadPath -ItemType directory
}
$assemblyLoadPath = $assemblyLoadPath + '\todotxtlib.net.dll'
# Before we try to load up the newest version of the DLL, we need to see if it's already loaded
# so we'll try to New-Object a task list; if it fails, we'll know it's safe to copy the dll
Try
{
Copy-Item -Path $assemblyPath -Destination $assemblyLoadPath
}
Catch
{
[system.exception]
}
Add-Type -Path $assemblyLoadPath
## Figure out licensing and copyright stuff (including manifest)
function LoadConfiguration() {
param([string] $path)
## Set up the defaults
$script:TODOTXT_VERBOSE = $FALSE
$script:TODOTXT_FORCE = $FALSE
$script:TODOTXT_AUTO_ARCHIVE = $FALSE
$script:TODOTXT_PRESERVE_LINE_NUMBERS = $FALSE
$script:TODOTXT_DATE_ON_ADD = $TRUE
$script:PRI_A = 'Yellow'
$script:PRI_B = 'Green'
$script:PRI_C = 'Cyan'
$script:PRI_X = 'White'
## Override the defaults with the configuration file
if(Test-Path $path)
{
.$path
}
}
<#
.Synopsis
TODO.TXT Command Line Interface for PowerShell 2.0
.Description
The ToDo function is an entry point for running functions to manipulate a todo.txt file
using the same command syntax as todo.sh
.Example
ToDo list
List all of the todo items in your todo file.
.Example
ToDo listall
List all of the items in the todo and done files.
.Example
ToDo add "THING I NEED TO DO +project @context"
Adds "THING I NEED TO DO" to your todo.txt file on its own line,
assigning it to a project and context.
.Example
ToDo append 34 "TEXT TO APPEND"
Adds "TEXT TO APPEND" to the end of the task on line 34.
.Example
ToDo archive
Moves all done tasks from todo.txt to done.txt.
.Example
ToDo del 34
Deletes the task on line 34 in todo.txt.
.Example
ToDo del 34 "foo"
Deletes the text "foo" from line 34 in todo.txt
.Example
ToDo move 34 .\otherfile.txt
Moves item 34 to otherfile.txt
#>
function ToDo {
param()
if(!$configLocation)
{
$configLocation = ($PSScriptRoot + '\todo_cfg.ps1')
}
LoadConfiguration $configLocation
## TODO process command line options for overrides
$cmd = $args[0]
$fore = $Host.UI.RawUI.ForegroundColor
if(!$cmd -or $cmd -eq "list" -or $cmd -eq "ls")
{
$todoArgs = @{path=$TODO_FILE; search=$args[1..$args.Length]}
Format-Priority((Get-ToDo @todoArgs))
}
elseif($cmd -eq "listall" -or $cmd -eq "lsa")
{
$todoArgs = @{path=$TODO_FILE; search=$args[1..$args.Length]; includeCompletedTasks=$TRUE}
Format-Priority((Get-ToDo @todoArgs))
}
elseif($cmd -eq "listfile" -or $cmd -eq "lf")
{
$todoArgs = @{path=$args[1]; search=$args[2..$args.Length]}
Format-Priority((Get-ToDo @todoArgs))
}
elseif($cmd -eq "add" -or $cmd -eq "a")
{
Add-Todo $args[1..$args.Length]
}
elseif($cmd -eq "addm")
{
$split = $args[$args.Length - 1].Split([environment]::newline, [StringSplitOptions]'RemoveEmptyEntries')
($split) | % {
Add-ToDo $_
}
}
elseif($cmd -eq "rm" -or $cmd -eq "del")
{
Remove-ToDo $args[1] $args[2]
}
elseif($cmd -eq "listproj" -or $cmd -eq "lsprj" )
{
Get-Project
}
elseif($cmd -eq "listcon" -or $cmd -eq "lsc" )
{
Get-Context
}
elseif($cmd -eq "listpri" -or $cmd -eq "lsp")
{
Format-Priority((Get-Priority $args[1]))
}
elseif($cmd -eq "replace")
{
Replace-ToDo $args[1] ([String]::Join(" ", $args[2..$args.Length]))
}
elseif($cmd -eq "prepend" -or $cmd -eq "prep")
{
Prepend-ToDo $args[1] ([String]::Join(" ", $args[2..$args.Length]))
}
elseif($cmd -eq "append" -or $cmd -eq "app")
{
Append-ToDo $args[1] ([String]::Join(" ", $args[2..$args.Length]))
}
elseif($cmd -eq "do")
{
Set-ToDoComplete $args[1..$args.Length]
}
elseif($cmd -eq "archive")
{
Archive-ToDo
}
elseif($cmd -eq "pri" -or $cmd -eq "p")
{
Set-ToDoPriority $args[1] $args[2]
}
elseif($cmd -eq "depri" -or $cmd -eq "dp")
{
Deprioritize-ToDo $args[1..$args.Length]
}
elseif($cmd -eq "move" -or $cmd -eq "mv")
{
if($args[3])
{
Move-ToDo $args[1] $args[2] $args[3]
}
else
{
Move-ToDo $args[1] $args[2]
}
}
elseif($cmd -eq "help")
{
Get-Help Todo
}
}
function Format-Priority {
param(
[object[]] $tasks
)
$tasks | % {
if($_.Raw -match "\(A\)")
{
$Host.UI.RawUI.ForegroundColor = $PRI_A
}
elseif($_.Raw -match "\(B\)")
{
$Host.UI.RawUI.ForegroundColor = $PRI_B
}
elseif($_.Raw -match "\(C\)")
{
$Host.UI.RawUI.ForegroundColor = $PRI_C
}
elseif($_.Raw -match "\([D-Z]\)")
{
$Host.UI.RawUI.ForegroundColor = $PRI_X
}
else
{
$Host.UI.RawUI.ForegroundColor = $fore
}
$_
}
$Host.UI.RawUI.ForegroundColor = $fore
}
function ParseToDoList {
param(
[string] $path = $TODO_FILE,
[boolean] $includeCompletedTasks = $FALSE
)
if($includeCompletedTasks -and $DONE_FILE -and (Test-Path $DONE_FILE))
{
$listLocations = @($path, $DONE_FILE)
}
else
{
$listLocations = @($path)
}
$todos = New-Object todotxtlib.net.TaskList
$results = @(Get-Content $listLocations)
for ($i=0; $i -lt $results.Length; $i++)
{
$todo = New-Object todotxtlib.net.Task($results[$i], ($i + 1))
$todos.Add($todo)
}
return ,$todos
}
function Move-ToDo {
param (
[int] $item,
[string] $dest,
[string] $src = $TODO_FILE
)
if($dest)
{
if(!(Test-Path $dest))
{
Set-Content $dest ''
}
$srcList = ParseToDoList $src
$destList = ParseToDoList $dest
if($item -le $srcList.Count)
{
$oldItem = ($srcList[$item - 1]).Body
$confirmed = $TRUE
if(!$TODOTXT_FORCE)
{
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Moves the task."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Does nothing."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice("Move Item", "Move '$oldItem'?", $options, 1)
if($result -eq 1)
{
$confirmed = $FALSE
}
}
if($confirmed)
{
$task = New-Object todotxtlib.net.Task(($srcList[$item - 1].Raw), ($destList.Count + 1))
## add it to the destination file
$destList.Add($task)
$destList.ToOutput() | Set-Content $dest
## remove it from the original
$srcList.RemoveTask($item, $TODOTXT_PRESERVE_LINE_NUMBERS)
$srcList.ToOutput() | Set-Content $src
if($TODOTXT_VERBOSE)
{
Write-Host "$item $oldItem"
Write-Host "TODO: $item moved from '$src' to '$dest'."
}
}
else
{
if($TODOTXT_VERBOSE)
{
Write-Host "TODO: No tasks moved."
}
}
}
else
{
Write-Host "No task $item."
}
}
}
function Set-ToDoComplete {
param([int[]] $items)
if($items)
{
$list = ParseToDoList
$items | % {
if($_ -le $list.Count)
{
if($list[$_ - 1].Done)
{
Write-Host "$_ is already marked done."
}
else
{
$list[$_ - 1].ToggleCompleted()
if($TODOTXT_VERBOSE)
{
Write-Host ("$_ " + $list[$_ - 1].Body)
Write-Host "TODO: $_ marked as done."
}
}
}
else
{
Write-Host "No task $_."
}
}
$list.ToOutput() | Set-Content $TODO_FILE
if($TODOTXT_AUTO_ARCHIVE)
{
Archive-ToDo
}
}
}
function Archive-ToDo {
## Todo figure out what to do if $DONE_FILE isn't specified
if($DONE_FILE)
{
$list = ParseToDoList
$completed = $list.RemoveCompletedTasks($TODOTXT_PRESERVE_LINE_NUMBERS)
$completed.ToOutput() | Add-Content $DONE_FILE
$list.ToOutput() | Set-Content $TODO_FILE
if($TODOTXT_VERBOSE)
{
$completed.ToNumberedOutput() | % {Write-Host $_}
Write-Host "TODO: $TODO_FILE archived."
}
}
}
function Deprioritize-ToDo {
param([int[]] $items)
$list = ParseToDoList
$items | % {
if($_ -le $list.Count)
{
$list.SetItemPriority($_, '')
if($TODOTXT_VERBOSE)
{
Write-Host ("$_ " + $list[$_ - 1].Text)
Write-Host "TODO: $_ deprioritized."
}
}
else
{
Write-Host "No task $_."
}
}
$list.ToOutput() | Set-Content $TODO_FILE
}
function Set-ToDoPriority {
param([int] $item,
[string] $priority)
if($priority -match "^[A-Z]{1}$")
{
$list = ParseToDoList
if($item -le $list.Count)
{
$list.SetItemPriority($item, $priority)
$list.ToOutput() | Set-Content $TODO_FILE
if($TODOTXT_VERBOSE)
{
Write-Host ("$item " + $list[$item - 1].Text)
Write-Host "TODO: $item prioritized ($priority)."
}
}
else
{
Write-Host "No task $item."
}
}
## TODO show usage
}
function Get-ToDo {
param(
[string[]] $search,
[boolean] $includeCompletedTasks = $FALSE,
[string] $path = $TODO_FILE
)
## TODO Error/warning message for no todo location set
$list = ParseToDoList $path $includeCompletedTasks
if($search)
{
$search = [String]::Join(" ", $search).Trim()
}
if(!$search)
{
$result = $list
}
else
{
## TODO - check for '-' at the beginning of the search term and handle notMatch
$result = ($list.Search($search))
}
return ,$result
}
function Add-ToDo {
param(
[string[]] $item
)
$item = ([String]::Join(" ", $item)).Trim()
if($TODOTXT_DATE_ON_ADD)
{
$item = ((Get-Date -format "yyyy-MM-dd") + " " + $item)
}
Add-Content $TODO_FILE ($item)
if($TODOTXT_VERBOSE)
{
$taskNum = (Get-Content $TODO_FILE | Measure-Object).Count
Write-Host "$taskNum $item"
Write-Host "$taskNum added."
}
}
function Get-Context {
$matches = (select-string $TODO_FILE -pattern '\s(@\w+)' -AllMatches) | % {$_.Matches}
$matches | % {$_.Groups[1]} | Sort-Object | Get-Unique | Select -property @{N='Context';E={$_.Value}}
}
function Get-Project {
$matches = (select-string $TODO_FILE -pattern '\s(\+\w+)' -AllMatches) | % {$_.Matches}
$matches | % {$_.Groups[1]} | Sort-Object | Get-Unique | Select -property @{N='Project';E={$_.Value}}
}
function Get-Priority {
param(
[string] $priority
)
$list = ParseToDoList
,$list.GetPriority($priority)
}
function Prepend-ToDo {
param(
[int] $item,
[string] $term
)
$list = ParseToDoList
if($term)
{
if($item -le $list.Count)
{
$list.PrependToTask($item, $term)
$list.ToOutput() | Set-Content $TODO_FILE
if($TODOTXT_VERBOSE)
{
Write-Host ("$item " + $list[$item-1].Body)
}
}
}
}
function Append-ToDo {
param(
[int] $item,
[string] $term
)
$list = ParseToDoList
if($term)
{
if($item -le $list.Count)
{
$list.AppendToTask($item, $term)
$list.ToOutput() | Set-Content $TODO_FILE
if($TODOTXT_VERBOSE)
{
Write-Host ("$item " + $list[$item-1].Body)
}
}
}
}
function Replace-ToDo {
param(
[int] $item,
[string] $term
)
$list = ParseToDoList
if($term)
{
if($item -le $list.Count)
{
$oldText = $list[$item-1].Body
$list.ReplaceInTask($item, $term)
$list.ToOutput() | Set-Content $TODO_FILE
if($TODOTXT_VERBOSE)
{
Write-Host "$item $oldText"
Write-Host "TODO: Replaced task with:"
Write-Host "$item $term"
}
}
}
}
function Remove-ToDo {
param(
[int] $item,
[string] $term
)
$list = ParseToDoList
if($item -le $list.Count)
{
$oldItem = ($list[$item - 1]).Body
if($term)
{
$success = $list.RemoveFromTask($item, $term)
$list.ToOutput() | Set-Content $TODO_FILE
if($success)
{
$newItem = ($list[$item - 1]).Body
Write-Host "$item $oldItem"
Write-Host "TODO: Removed '$term' from task."
Write-Host "$item $newItem"
}
else
{
Write-Host "$item $oldItem"
Write-Host "TODO: '$term' not found; no removal done."
}
}
else
{
$confirmed = $TRUE
if(!$TODOTXT_FORCE)
{
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "Deletes the task."
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "Retains the task."
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice("Delete Item", "Delete '$oldItem'?", $options, 1)
if($result -eq 1)
{
$confirmed = $FALSE
}
}
if($confirmed)
{
$list.RemoveTask($item, $TODOTXT_PRESERVE_LINE_NUMBERS)
$list.ToOutput() | Set-Content $TODO_FILE
if($TODOTXT_VERBOSE)
{
Write-Host ("$item $oldItem")
Write-Host ("TODO: $item deleted")
}
}
else
{
Write-Host "TODO: No tasks were deleted"
}
}
}
else
{
Write-Host "TODO: No task $item"
}
}
export-modulemember -function Get-ToDo
export-modulemember -function Add-ToDo
export-modulemember -function Remove-ToDo
export-modulemember -function Get-Context
export-modulemember -function Get-Project
export-modulemember -function Get-Priority
export-modulemember -function Append-ToDo
export-modulemember -function Prepend-ToDo
export-modulemember -function Replace-ToDo
export-modulemember -function Set-ToDoDone
export-modulemember -function Set-ToDoPriority
export-modulemember -function Archive-ToDo
export-modulemember -function Set-ToDoComplete
export-modulemember -function Deprioritize-ToDo
export-modulemember -function Move-ToDo
export-modulemember -function ToDo
export-modulemember -function ParseToDoList