dev.fron.io rc / eb302de
scripts: add format Tony Olagbaiye 10 days ago
1 changed file(s) with 57 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 (define-module (rc scripts format)
1 #:use-module (guix ui)
2 #:use-module (guix grafts)
3 #:use-module (guix packages)
4 #:use-module (guix profiles)
5 #:use-module (guix store)
6 #:use-module (guix utils)
7 #:use-module (guix scripts)
8 #:use-module (guix scripts package)
9 #:use-module (guix scripts build)
10 #:use-module (guix scripts system search)
11 #:use-module ((guix status) #:select (with-status-verbosity))
12 #:use-module (guix gexp)
13 #:use-module (guix monads)
14 #:use-module (srfi srfi-1)
15 #:use-module (srfi srfi-26)
16 #:use-module (srfi srfi-35)
17 #:use-module (srfi srfi-37)
18 #:use-module (ice-9 match)
19 #:use-module (ice-9 pretty-print)
20 #:export (guix-format))
21
22 (define (show-help)
23 (display (G_ "Usage: ... | guix format
24 Format stdin and return it pretty-printed to stdout\n"))
25 (newline)
26 (show-bug-report-information))
27
28 (define-command (guix-format . args)
29 (synopsis "format guile scheme code")
30
31 (with-error-handling
32 (scmfmt)))
33
34
35 ;; Helper methods for maintaining comments and whitespace.
36 (define (copy-line-comment)
37 (let ((char (read-char)))
38 (if (not (eof-object? char))
39 (if (eq? char #\newline)
40 (newline)
41 (begin (write-char char) (copy-line-comment))))))
42 (define (maintain-empty-lines)
43 (let ((char1 (read-char)) (char2 (peek-char)))
44 (if (and (eq? char1 #\newline) (eq? char2 #\newline))
45 (write-char (read-char)))))
46
47 ;; The main method. This reads from and writes to stdin/stdout.
48 (define (scmfmt)
49 (let ((char (peek-char)))
50 (if (not (eof-object? char))
51 (begin
52 (cond ((eq? char #\;) (copy-line-comment))
53 ((eq? char #\newline) (maintain-empty-lines))
54 ((char-whitespace? char) (read-char))
55 (#t (pretty-print (read))))
56 (scmfmt)))))