From 436f57cb5547dd7a9e213ffa52fa787271492791 Mon Sep 17 00:00:00 2001 From: Tom Willemsen Date: Tue, 2 Oct 2012 02:14:27 +0200 Subject: [PATCH] Add update command * undone/main.scm (args->alist): Place argument parsing for add/update in separate function for reuse. (delete-item): Renamed from `delete', which is in conflict with guile's own `delete'. (update): New function. --- undone/main.scm | 43 +++++++++++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 12 deletions(-) diff --git a/undone/main.scm b/undone/main.scm index 2fa47f7..b175954 100644 --- a/undone/main.scm +++ b/undone/main.scm @@ -2,6 +2,7 @@ #:use-module (ice-9 format) #:use-module (ice-9 rdelim) #:use-module (oop goops) + #:use-module (srfi srfi-1) #:use-module (undone view) #:export (main)) @@ -133,6 +134,16 @@ todo-list))) 1)) +(define (args->alist args) + (map (lambda (arg) + (let* ((pair (string-split arg #\=)) + (var (string->symbol (car pair))) + (val (cadr pair))) + (when (char=? (string-ref val 0) #\,) + (set! val (string-split (substring val 1) #\,))) + (cons var val))) + args)) + (define (add args) "Add item to the list." (set! todo-list @@ -141,18 +152,9 @@ (append `((id . ,(next-id)) (content . ,(string-trim-right (read-delimited "")))) - (map (lambda (arg) - (let* ((pair (string-split arg #\=)) - (var (string->symbol (car pair))) - (val (cadr pair))) - (if (char=? (string-ref val 0) #\,) - (set! val (string-split - (substring val 1) #\,))) + (args->alist args)))))) - (cons var val))) - args)))))) - -(define (delete args) +(define (delete-item args) "Delete the specified element." (set! todo-list (filter (lambda (elm) @@ -180,6 +182,20 @@ (set-current-module (resolve-module '(undone main))) (load rc-file)))) +(define (update args) + (let ((item + (find (lambda (elm) + (let ((id (assq 'id elm))) + (and id (eq? (cdr id) (string->number (car args)))))) + todo-list)) + (alist (args->alist (cdr args)))) + (set! todo-list (delete item todo-list)) + (for-each + (lambda (elm) + (set! item (assq-set! item (car elm) (cdr elm)))) + alist) + (set! todo-list (append (list item) todo-list)))) + (define (main args) (when (and rc-file (file-exists? rc-file)) (load-rc-file)) @@ -190,10 +206,13 @@ (add (list-tail args 2)) (save)) ((delete) - (delete (list-tail args 2)) + (delete-item (list-tail args 2)) (save)) ((view) (view (list-tail args 2))) + ((update) + (update (list-tail args 2)) + (save)) ((views) (display "Defined views:") (newline)