From 015eab3dd8a55c29e463608b29a00e4e449036ec Mon Sep 17 00:00:00 2001 From: Arun Isaac Date: Mon, 14 Mar 2022 14:19:17 +0530 Subject: bin: Raise exception on missing file. * bin/tissue: Import (rnrs conditions). (tissue-edit, tissue-show): Raise exception on missing file. --- bin/tissue | 104 ++++++++++++++++++++++++++++++++++--------------------------- 1 file changed, 58 insertions(+), 46 deletions(-) diff --git a/bin/tissue b/bin/tissue index 94f0188..ff593c6 100755 --- a/bin/tissue +++ b/bin/tissue @@ -19,7 +19,8 @@ ;;; You should have received a copy of the GNU General Public License ;;; along with tissue. If not, see . -(import (rnrs io ports) +(import (rnrs conditions) + (rnrs io ports) (srfi srfi-1) (srfi srfi-26) (srfi srfi-37) @@ -278,9 +279,14 @@ Start $EDITOR to edit issue #ISSUE-NUMBER. (unless (getenv "EDITOR") (error "Please set the EDITOR environment variable to your favorite editor. For example, export EDITOR=emacsclient")) - (invoke (getenv "EDITOR") - (issue-file (list-ref (issues) - (1- (string->number issue-number)))))))) + (let ((issue-file (issue-file (list-ref (issues) + (1- (string->number issue-number)))))) + ;; Files may be renamed or deleted, but not + ;; committed. Therefore, only read the file if it exists. + (if (file-exists? issue-file) + (invoke (getenv "EDITOR") issue-file) + (raise-exception (make-message-condition + (string-append "No such file or directory: " issue-file)))))))) (define tissue-show (match-lambda* @@ -291,48 +297,54 @@ Show the text of issue #ISSUE-NUMBER. " (command-line-program))) ((issue-number) - (call-with-input-file (issue-file (list-ref (issues) - (1- (string->number issue-number)))) - (lambda (port) - (port-transduce - (compose - ;; Detect preformatted text blocks. - (tfold (match-lambda* - (((pre? . _) line) - (cons (if (string-prefix? "```" line) - (not pre?) - pre?) - line))) - (cons #f #f)) - (tmap (lambda (pre?+line) - (match pre?+line - ((pre? . line) - (cond - ;; Print headlines in bold. - ((string-prefix? "#" line) - (display (bold line))) - ;; Print lists in cyan. - ((string-prefix? "*" line) - (display (cyan line))) - ;; Print links in cyan, but only the actual - ;; link, and not the => prefix or the label. - ((string-match "^(=>[ \t]*)([^ ]*)([^\n]*)" line) - => (lambda (m) - (display (match:substring m 1)) - (display (cyan (match:substring m 2))) - (display (match:substring m 3)))) - ;; Print preformatted text backticks in - ;; magenta. - ((string-prefix? "```" line) - (display (magenta line))) - (else - ;; If part of preformatted block, print in - ;; magenta. Else, print in default color. - (display (if pre? (magenta line) line)))))) - (newline)))) - (const #t) - get-line-dos-or-unix - port)))))) + (let ((issue-file (issue-file (list-ref (issues) + (1- (string->number issue-number)))))) + ;; Files may be renamed or deleted, but not + ;; committed. Therefore, only read the file if it exists. + (if (file-exists? issue-file) + (call-with-input-file (issue-file issue) + (lambda (port) + (port-transduce + (compose + ;; Detect preformatted text blocks. + (tfold (match-lambda* + (((pre? . _) line) + (cons (if (string-prefix? "```" line) + (not pre?) + pre?) + line))) + (cons #f #f)) + (tmap (lambda (pre?+line) + (match pre?+line + ((pre? . line) + (cond + ;; Print headlines in bold. + ((string-prefix? "#" line) + (display (bold line))) + ;; Print lists in cyan. + ((string-prefix? "*" line) + (display (cyan line))) + ;; Print links in cyan, but only the actual + ;; link, and not the => prefix or the label. + ((string-match "^(=>[ \t]*)([^ ]*)([^\n]*)" line) + => (lambda (m) + (display (match:substring m 1)) + (display (cyan (match:substring m 2))) + (display (match:substring m 3)))) + ;; Print preformatted text backticks in + ;; magenta. + ((string-prefix? "```" line) + (display (magenta line))) + (else + ;; If part of preformatted block, print in + ;; magenta. Else, print in default color. + (display (if pre? (magenta line) line)))))) + (newline)))) + (const #t) + get-line-dos-or-unix + port))) + (raise-exception (make-message-condition + (string-append "No such file or directory: " issue-file)))))))) (define (print-usage) (format #t "Usage: ~a COMMAND [OPTIONS] [ARGS] -- cgit v1.2.3