aboutsummaryrefslogtreecommitdiff
path: root/ccwl/ui.scm
blob: 3b46a2399007ee29113d5ed414d41a3fa6882ecc (plain)
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
;;; ccwl --- Concise Common Workflow Language
;;; Copyright © 2022, 2023 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of ccwl.
;;;
;;; ccwl is free software: you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; ccwl is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with ccwl.  If not, see <https://www.gnu.org/licenses/>.

(define-module (ccwl ui)
  #:use-module (rnrs io ports)
  #:use-module (srfi srfi-26)
  #:use-module (srfi srfi-28)
  #:use-module (ice-9 string-fun)
  #:use-module (term ansi-color)
  #:use-module (ccwl conditions)
  #:export (report-formatted-message
            report-ccwl-violation))

(define (report-formatted-message exception)
  "Report @var{exception}, a @code{&formatted-message} condition to the
user."
  (display (apply format
                  ;; We colorize the format specifiers instead of the
                  ;; arguments because we cannot always be sure that
                  ;; the arguments are strings.
                  (string-replace-substring
                   (string-replace-substring
                    (formatted-message-format exception)
                    "~a"
                    (colorize-string "~a" 'BOLD 'MAGENTA))
                   "~s"
                   (colorize-string "~s" 'BOLD 'MAGENTA))
                  (formatted-message-arguments exception))
           (current-error-port))
  (newline (current-error-port)))

(define (repeat thunk n)
  "Call THUNK N times."
  (unless (zero? n)
    (thunk)
    (repeat thunk (1- n))))

(define (save-excursion thunk port)
  "Call THUNK and restore PORT position to what it was before THUNK
was executed. For the curious, this function is named after the
save-excursion function in elisp."
  (let ((original-position #f))
    (dynamic-wind (lambda ()
                    (set! original-position (port-position port)))
                  thunk
                  (lambda ()
                    (set-port-position! port original-position)))))

(define (read-end port)
  "Return the position in PORT corresponding to the end of the
S-expression starting at the current port position. This function does
not affect the current port position."
  (save-excursion (lambda ()
                    (read port)
                    (port-position port))
                  port))

(define (read-sexp-string port)
  "Read an S-expression from PORT and return it as a string with
whitespace intact."
  (get-string-n port (- (read-end port)
                        (port-position port))))

(define (count-lines str)
  "Count the number of lines in STR."
  (call-with-input-string str
    (lambda (port)
      (let loop ()
        (if (eof-object? (get-line port))
            0
            (1+ (loop)))))))

(define (number-of-digits n)
  "Return the number of decimal digits in positive integer N."
  (if (< n 10)
      1
      (1+ (number-of-digits (quotient n 10)))))

(define (display-integer n minimum-width port)
  "Display integer N to PORT using at least MINIMUM-WIDTH
characters. If N is not large enough, it is padded with spaces."
  (display (make-string (max 0 (- minimum-width
                                  (number-of-digits n)))
                        #\space)
           port)
  (display n port))

(define (display-with-line-numbers str out starting-line-number)
  "Display STR to port OUT with each line prefixed with a line
number. Line numbers start from STARTING-LINE-NUMBER."
  (call-with-input-string str
    (lambda (in)
      (let ((last-line-number (+ starting-line-number
                                 (count-lines str))))
        (let loop ((line-number starting-line-number))
          (let ((line (get-line in)))
            (unless (eof-object? line)
              (display (make-string 4 #\space) out)
              (display-integer line-number
                               (number-of-digits last-line-number)
                               out)
              (display (format " | ~a~%" line) out)
              (loop (1+ line-number)))))))))

(define (put-line line port)
  "Display LINE to PORT followed by a newline character."
  (display line port)
  (newline port))

(define (string-blank? str)
  "Return non-#f if STR contains only whitespace characters, else
return #t."
  (string-every char-set:whitespace str))

(define (source-in-context port line-number column-number)
  "Return source from PORT at LINE-NUMBER, COLUMN-NUMBER in context
with S-expression at LINE-NUMBER, COLUMN-NUMBER highlit in
red. LINE-NUMBER and COLUMN-NUMBER are zero-based."
  (call-with-output-string
    (lambda (out)
      ;; Unless the 0th line is requested, display line preceding
      ;; syntax x.
      (unless (zero? line-number)
        ;; Get to line preceding syntax x.
        (repeat (cut get-line port)
                (1- line-number))
        ;; Display line preceding syntax x unless blank.
        (let ((line (get-line port)))
          (unless (string-blank? line)
            (put-line line out))))
      ;; Display part of line before syntax x.
      (display (get-string-n port column-number)
               out)
      ;; Display syntax x in red.  Color each line separately to
      ;; help line oriented functions like
      ;; `display-with-line-numbers'.
      (display (string-join (map (cut colorize-string <> 'BOLD 'RED)
                                 (string-split (read-sexp-string port)
                                               #\newline))
                            "\n")
               out)
      ;; Display part of line after syntax x.
      (put-line (get-line port) out)
      ;; Display line following syntax x unless blank.
      (let ((line (get-line port)))
        (unless (or (eof-object? line)
                    (string-blank? line))
          (put-line line out))))))

(define (report-ccwl-violation exception)
  (let ((file (ccwl-violation-file exception))
        (line (ccwl-violation-line exception))
        (column (ccwl-violation-column exception)))
    (display (colorize-string (format "~a:~a:~a: " file (1+ line) column)
                              'BOLD)
             (current-error-port))
    (display (colorize-string "error:" 'BOLD 'RED)
             (current-error-port))
    (display " " (current-error-port))
    (when (formatted-message? exception)
      (report-formatted-message exception))
    (display-with-line-numbers (call-with-input-file file
                                 (cut source-in-context <> line column))
                               (current-error-port)
                               (max 1 line))))