(declare (unit tick-edit)) (module tick-edit () (import scheme) (import (chicken base) (chicken file) (chicken format) (chicken io) (chicken pathname) (chicken port) (chicken sort) (chicken string)) (import commands optimism simple-logger srfi-1 srfi-13) (import tick) (define (get-user-comments thash time) ;; Return a list ( . ) (let ((change-files (sort (glob (make-pathname (ticket-changes-dir thash) ;; Users can only edit their own comments (string-append time "*" (get-username) ".wiki"))) string<))) (filter-map (lambda (change-file) (receive (summary comment) (with-input-from-file change-file (lambda () (let ((summary (read-line)) (comment (let loop () (let ((text (read-line))) (cond ((eof-object? text) "") ((string=? "" (string-trim-both text)) (loop)) (else text)))))) (values summary comment)))) (and (string-suffix? "wrote:" summary) (cons change-file ;; Drop heading markup ("=== ") (sprintf "~a ~a" (substring summary 4) (if (> (string-length comment) 62) (string-append (substring comment 0 62) " ...") comment)))))) change-files))) (define (edit-comment thash comment-file) (let* ((tmp-comment-file (create-temporary-file)) (heading #f) ;; will be set when reading the comment file (cur-comment-text ;; Drop the heading line and get only the comment body (with-output-to-string (lambda () (with-input-from-file comment-file (lambda () (set! heading (read-line)) ;; Drop heading (let loop () (let ((line (read-line))) (unless (eof-object? line) (print line) (loop)))))))))) (set! cur-comment-text (string-trim-both cur-comment-text)) (with-output-to-file tmp-comment-file (cut display (string-trim-both cur-comment-text))) (let ((new-comment-text (prompt-editor 'comment file: tmp-comment-file))) (delete-file tmp-comment-file) (cond ((equal? cur-comment-text new-comment-text) (print "Comment has not been modified.") (exit)) ((or (eof-object? new-comment-text) (string=? "" (string-trim-both new-comment-text))) (die! "Comment cannot be empty.")) (else (with-output-to-file comment-file (lambda () (print heading) (print new-comment-text))) (let ((orig-comment-date (get-date/time-from-comment-heading heading))) (write-ticket-change thash (sprintf "modified the comment made on ~a" orig-comment-date)) (db-add-change thash (sprintf "Change comment made on ~a" orig-comment-date)) )))))) (define-command 'edit #<#EOF edit Edit body (description) and comments of tickets. : --body Edit ticket body. --comment|-c [