-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcl-ansi-term.lisp
More file actions
2336 lines (2017 loc) · 83.9 KB
/
cl-ansi-term.lisp
File metadata and controls
2336 lines (2017 loc) · 83.9 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
;;;
;;; This file is part of cl-ansi-term.
;;;
;;; Copyright © 2015–2018 Mark Karpov
;;;
;;; cl-ansi-term 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.
;;;
;;; cl-ansi-term 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 this program. If not, see <http://www.gnu.org/licenses/>.
(uiop:define-package :cl-ansi-term
(:nicknames :term)
(:use #:common-lisp
#:alexandria
#:anaphora)
(:export #:*effects-enabled*
#:*terminal-width*
#:register-hook
#:remove-hook
#:update-style-sheet
#:cat-print
#:print-styled
#:hr
#:vspace
#:banner
#:progress-bar
#:u-list
#:o-list
#:table
#:vtable
#:plist-vtable
#:plist-table
#:plists-table
#:plists-vtable
#:*prefer-plists-in-tables*
#:alists-table
#:alist-table
#:title
#:banner-fmt
#:title-fmt
#:with-table-output-to-string)
)
(in-package #:cl-ansi-term)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Parameters and Constants ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar *stream* *standard-output*
"Default stream. Can be let-bound to print to strings.")
(defparameter *effects-enabled* t
"If this variable is bound to non-NIL value, graphic rendition
effects (and other terminal-dependent effects) are enabled, otherwise they
are disabled.")
(defparameter *terminal-width* 90
"Many functions use this value to output text nicely. The default value is 80.
If you want to dynamically change this variable, write and register
:BEFORE-PRINTING hook and reassign terminal width before printing takes
place.")
(defparameter *column-width* 20 "The table cells' width (DEPRECATED).")
(defparameter *max-column-width* 80 "The maximum table cells' width.")
(defparameter *min-column-width* 3 "Should be at least 3")
(defparameter *hooks* (make-hash-table)
"This variable is bound to a hash table that provides access to lists of
functions by given key. We use keywords as keys. Arguments for the functions
depend entirely on EVENT on which every function is called.")
(defvar *styles* (list)
"The raw style sheet, a list of lists, that was given to `update-style-sheet', before interpretation of ANSI codes.")
(defvar *style-sheet* (make-hash-table)
"This hash table contains strings for various styles of terminal output,
defined with `update-style-sheet'.")
(defvar *style* :default
"This variable is bound to currently set style. Styles are set with
`set-style' function.")
(defparameter *coloration* nil
"Alist where CARs are indexes at which to insert ANSI escape sequences to
change graphical rendition and CDRs are keywords that denote style of the
rendition. This special variable can be used to affect `print-partially'.")
(defparameter +foreground-colors+
'((:default . 39)
(:black . 30)
(:red . 31)
(:green . 32)
(:yellow . 33)
(:blue . 34)
(:magenta . 35)
(:cyan . 36)
(:white . 37)
(:black* . 90)
(:red* . 91)
(:green* . 92)
(:yellow* . 93)
(:blue* . 94)
(:magenta* . 95)
(:cyan* . 96)
(:white* . 97))
"These are the basic foreground terminal colors. Colors that are denoted
by keywords ending with an asterisk are not in the ANSI standard (high
intensity variants of 8 basic colors).")
(defparameter +background-colors+
'((:b-default . 49)
(:b-black . 40)
(:b-red . 41)
(:b-green . 42)
(:b-yellow . 43)
(:b-blue . 44)
(:b-magenta . 45)
(:b-cyan . 46)
(:b-white . 47)
(:b-black* . 100)
(:b-red* . 101)
(:b-green* . 102)
(:b-yellow* . 103)
(:b-blue* . 104)
(:b-magenta* . 105)
(:b-cyan* . 106)
(:b-white* . 107))
"These are the basic background terminal colors. Colors that are denoted
by keywords ending with an asterisk are not in ANSI standard (high intensity
variants of 8 basic colors).")
(defparameter +effects+
'((:bold . 1)
(:faint . 2)
(:italic . 3)
(:underline . 4)
(:blink . 5)
(:bad-blink . 6)
(:inverse . 7)
(:hide . 8)
(:fraktur . 20)
(:normal . 22)
(:framed . 51)
(:encircled . 52)
(:overlined . 53))
"All supported rendition effects. Some of them are hardly ever supported
by real-world terminals.")
(defvar *prefer-plists-in-tables* nil
"If non-nil, if the TABLE function can't clearly distinguish
between a list of plists and a list of regular lists,
it will give precedence to displaying the data as plists.")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Macros ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar %newline-before-row% nil
"Necessary when doing with-output-to-string because something.")
(defmacro with-table-output-to-string ((&key effects) &body body)
"Call BODY with with-output-to-string, and fixing a glitch in the table output.
If :EFFECTS is t, enable effects.
Set %newline-before-row% to fix a difference in output between output to a string and to a stream. This is a bug we have to chase for."
`(with-output-to-string (s)
(let ((%newline-before-row% t)
(*effects-enabled* ,effects)
(*stream* s))
,@body)
s))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Hooks ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun register-hook (event function)
"Register a hook. When predefined EVENT occurs FUNCTION will be called.
You can register many functions to call on the same event.
Acceptable values of EVENT:
:BEFORE-PRINTING—FUNCTION is invoked just before printing takes place, no
argument is passed to the function
:AFTER-PRINTING—FUNCTION is invoked after printing, no argument is passed to
the function
:ON-STYLE-CHANGE—FUNCTION is invoked before style changing escape sequence
in printed. One argument is passed to FUNCTION, name of the style, which is
a keyword."
(push function (gethash event *hooks*))
nil)
(defun remove-hook (event)
"Remove all registered functions that are called on EVENT. Returns T if
there were any functions associated with EVENT and NIL otherwise."
(remhash event *hooks*))
(defun perform-hook (event &rest args)
"Execute functions corresponding to given EVENT. We use this function to
perform the hooks, so it's for internal use. Return T if there is at least
one function associated with EVENT and NIL otherwise."
(awhen (gethash event *hooks*)
(dolist (x it t)
(apply x args))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Graphic Rendition and Style Sheet ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ansi-escape-seq (&optional tokens)
"Convert list of rendition tokens into an ANSI escape sequence that will
select appropriate parameters of rendition if “printed” on an
ANSI-compatible terminal. If TOKENS is empty, escape sequence that resets
all rendition parameters will be returned."
(if (null tokens)
(format nil "~c[0m" #\escape)
(flet ((select-token (options default)
(or (car (intersection tokens
(mapcar #'car options)))
default)))
(let ((foreground-color (select-token +foreground-colors+ :default))
(background-color (select-token +background-colors+ :b-default))
(effect (select-token +effects+ :normal)))
(format nil "~c[~d;~d;~dm" #\escape
(cdr (assoc foreground-color +foreground-colors+))
(cdr (assoc background-color +background-colors+))
(cdr (assoc effect +effects+)))))))
(defun update-style-sheet (styles)
"Update the style sheet used by the application.
Every item of STYLES must be a list with:
- a first element that denotes the name of a style sheet entry. The names are yours.
- a rest of elements that represent a collection of tokens that define terminal rendition.
Example:
(update-style-sheet
'((:header :cyan :underline)
(:mark :red :reverse)
(:term :yellow :bold)))
Then use it:
(term:table (list '(:name :age) '(:me 7)) :header-style :header)
Tokens can represent various things: foreground color, background
color, and effects. Every type of token has its default value, so you
can omit some tokens.
For a full list of accepted tokens, see `+foreground-colors+', `+background-colors+' and `+effects+'.
If there are more than one token of the same type (for example :RED
and :GREEN—both tokens represent foreground color), result is
unpredictable and depends on internal workings of Common Lisp
implementation used.
You cannot redefine the :DEFAULT style, it always represents default
parameters of rendition.
Tokens are interpreted into their ANSI code (:bold is ^[[33;49;1m … )
and the new stylesheet is saved to `term::*style-sheet*'.
STYLES are saved into `term::*styles*'."
(setf *styles* styles)
(dolist (entry styles)
(destructuring-bind (style . tokens) entry
(if tokens
(setf (gethash style *style-sheet*) (ansi-escape-seq tokens))
(remhash style *style-sheet*))))
(setf (gethash :default *style-sheet*) (ansi-escape-seq))
nil)
(defparameter *enable-effects-on-dumb-terminals* t
"If non true, don't print ANSI escape codes on dumb terminals, like Emacs' Slime.")
;; (declaim (inline effects-p))
(defun effects-p (stream)
"Effects are now enabled everywhere, on real and dumb terminals, but we can control that.
This function evaluates to T if:
- `*effects-enabled*' is T
- STREAM has support for effects (calls to CL:INTERACTIVE-STREAM-P) and `*enable-effects-on-dumb-terminals*' is T."
(or *effects-enabled*
(and (interactive-stream-p stream)
*enable-effects-on-dumb-terminals*)))
(defun set-style (style &optional (stream *stream*))
"Sets terminal rendition according to defined STYLE. It does nothing if
`*effects-enabled*' is NIL or output stream is not interactive (e.g.
redirected to a file)."
(awhen (and (effects-p stream)
(gethash style *style-sheet*))
(perform-hook :on-style-change style)
(princ it stream)
(setf *style* style)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Utilities ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun print-partially (text start end &optional (stream *stream*))
"Partially print given TEXT starting from START character until END
character is reached. Output will be colorized if `*coloration*' is bound to
alist that describes how to colorize the output, see `*coloration*'. All
output goes to STREAM."
(do ((i start (1+ i)))
((= i end))
(when (and *coloration*
(= i (caar *coloration*)))
(set-style (cdr (pop *coloration*)) stream))
(princ (char text i) stream)))
(defun print-white-space (width &optional (stream *stream*))
"Print WIDTH white-spaces to STREAM."
(dotimes (i width)
(princ #\space stream)))
(defmacro with-reasonable-width (var &body body)
"Rebind variable VAR, correcting its value in BODY. If VAR is not a
positive number, `*terminal-width*' will be added to it to get positive
value that will be used."
`(let ((,var (+ ,var (if (plusp ,var) 0 *terminal-width*))))
,@body))
(defun align-object (width align &optional (stream *stream*))
"Print white-space to STREAM so object occupying WIDTH columns will be
aligned according to ALIGN if printed immediately after the white space."
(print-white-space
(case align
(:right (- *terminal-width* width))
(:center (floor (- *terminal-width* width)
2))
(t 0))
stream))
(defun string* (object)
"Converts printable object OBJECT to its aesthetic string representation."
(format nil "~a" object))
(defun print-filler (filler width style &optional (stream *stream*))
"Print WIDTH symbols of FILLER to STREAM. Use STYLE for graphic
rendition."
(multiple-value-bind (rough rest)
(floor width (length (string* filler)))
(set-style style)
(dotimes (i rough)
(princ filler stream))
(print-partially filler 0 rest stream)
(set-style :default)))
(defun largest-length (lines)
"Return the largest length of the given strings."
(loop :for elt :in lines
:maximize (length elt)))
#+nil
(assert (equalp 8 (largest-length '("rst" "ldvvvvvv" nil))))
(defun ensure-circular-list (object)
(apply #'circular-list
(ensure-cons object)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Functions ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun print-words (objects
&key
(base-style :default)
(margin 0)
(fill-column 0)
(align :left)
(stream *stream*))
"Print concatenation of OBJECTS using FILL-COLUMN so that line breaks
don't happen inside words, only between them. OBJECTS must be a list
designator. It can consist of printable objects and lists where CAR is a
printable object and CADR is a keyword that denotes style of the string
designator. Unspecified styles default to BASE-STYLE. MARGIN is not applied
for the first line. If FILL-COLUMN is not a positive number,
`*terminal-width*' will be added to it to get positive FILL-COLUMN. Output
can be aligned with ALIGN parameter. Output goes to STREAM."
(with-reasonable-width fill-column
(let ((fill-column (- fill-column margin))
(text (make-array 0
:element-type 'character
:fill-pointer 0
:adjustable t))
*coloration*
(len 0))
(with-output-to-string (stream text)
(flet ((proc-object (obj style)
(let ((obj (string* obj)))
(unless (emptyp obj)
(push (cons len style) *coloration*)
(incf len (length (string* obj)))
(princ obj stream)))))
(dolist (object (ensure-list objects))
(if (consp object)
(destructuring-bind (printable style)
object
(proc-object printable style))
(proc-object object base-style)))))
(setf *coloration* (nreverse *coloration*))
(do* ((start 0)
(end (min (+ start fill-column) len)
(min (+ start fill-column) len)))
((= start end))
(destructuring-bind (break-pos . new-start)
(or (awhen (position #\newline text
:start start
:end end)
(cons it (1+ it)))
(when (< (- end start)
fill-column)
(cons end end))
(awhen (position #\space text
:start start
:end end
:from-end t)
(cons it (1+ it)))
(cons end end))
(when (plusp start)
(print-white-space margin stream))
(align-object (+ (- break-pos start) margin)
align
stream)
(print-partially text start break-pos stream)
(do () ((or (null *coloration*)
(<= new-start (caar *coloration*))))
(set-style (cdr (pop *coloration*)) stream))
(when (or (null *coloration*)
(< new-start (caar *coloration*)))
(push (cons new-start *style*) *coloration*))
(set-style :default stream)
(terpri stream)
(setf start new-start))))))
(defun cat-print (objects
&key
(base-style :default)
(margin 0)
(fill-column 0)
(align :left)
(stream *stream*))
"Concatenate OBJECTS and print them. OBJECTS must be a list designator
that consists of printable objects and lists where CAR is a printable object
and CADR is a keyword that denotes style of the object. Unspecified styles
default to BASE-STYLE. MARGIN, FILL-COLUMN, and ALIGN control corresponding
parameters of output. Valid values for ALIGN are :LEFT (default), :CENTER,
and :RIGHT. Output goes to STREAM."
(perform-hook :before-printing)
(print-white-space margin stream)
(print-words objects
:base-style base-style
:margin margin
:fill-column fill-column
:align align
:stream stream)
(finish-output stream)
(perform-hook :after-printing)
nil)
(defun parse-control-string (string args)
"Parse the control string according to the format described in the
documentation of the PRINT-STYLED function. Return a list, suitable for passing to
`cat-print'."
(labels ((positions (char str &optional (start 0) acc)
(aif (position char str :start start :test #'char=)
(positions char str (1+ it) (cons it acc))
(nreverse acc)))
(worker (opened closed &optional pending acc)
(cond ((null closed) (nreverse acc))
((and opened (< (car opened) (car closed)))
(worker (cdr opened)
closed
(cons (car opened) pending)
acc))
(t (worker opened
(cdr closed)
(cdr pending)
(cons (cons (car pending)
(car closed))
acc)))))
(form-pairs (open close str)
(worker (positions open str)
(positions close str)))
(prepare (str start end)
(with-output-to-string (stream)
(dotimes (i (- end start))
(let ((char (char str (+ start i))))
(princ (if (char= char #\~)
(format nil "~a" (pop args))
char)
stream))))))
(let ((brackets (form-pairs #\[ #\] string))
(parens (form-pairs #\( #\) string))
(i 0)
result)
(dolist (item (mapcan (lambda (b)
(destructuring-bind (bs . be) b
(awhen (find-if (lambda (x)
(= (car x) (1+ be)))
parens)
(destructuring-bind (ps . pe) it
(list (list (cons (1+ bs) be)
(cons (1+ ps) pe)))))))
brackets))
(destructuring-bind ((bs . be) (ps . pe)) item
(when (< i (1- bs))
(push (prepare string i (1- bs)) result))
(push (list (prepare string bs be)
(intern (string-upcase (subseq string ps pe))
"KEYWORD"))
result)
(setf i (1+ pe))))
(when (< i (length string))
(push (prepare string i (length string)) result))
(nreverse result))))
(defun print-styled (control-string
&key
args
(base-style :default)
(margin 0)
(fill-column 0)
(align :left)
(stream *stream*))
"Print text with CAT-PRINT, but apply CONTROL-STRING with the arguments from ARGS, where each tilde character of CONTROL-STRING is replaced with an argument.
A special syntax can be used to apply styles.
Example:
(term:print-styled \"~ and ~\" :args '(\"foo\" \"bar\") :align :center)
is equivalent to
(term:cat-print \"foo and bar\" :align :center)
Any region of text in CONTROL-STRING can be printed in a
specified style following this pattern:
[text](:name-of-style)
where :name-of-style is a downcase keyword in the style sheet.
The style of the rest of the output defaults to BASE-STYLE.
ALIGN can be :LEFT (default), :CENTER, and :RIGHT.
MARGIN is the length of the left margin.
FILL-COLUMN sets the column width:
(term:print-styled \"~ and ~\" :args '(\"foo\" \"bar\") :align :center :fill-column 10)
foo and
bar
Output goes to STREAM."
(cat-print (parse-control-string control-string
(ensure-list args))
:base-style base-style
:margin margin
:fill-column fill-column
:align align
:stream stream))
(defun hr (&key
(filler #\-)
(style :default)
(width 0)
(align :left)
(stream *stream*))
"Print a horizontal line. Characters in the line are created by repeating
given FILLER until WIDTH characters accumulated. If WIDTH is not a positive
number, `*terminal-width*' will be added to it to get positive WIDTH. STYLE
controls graphic rendition. ALIGN should be a keyword: :LEFT, :RIGHT,
or :CENTER. Output goes to STREAM."
(perform-hook :before-printing)
(with-reasonable-width width
(align-object width align)
(print-filler filler width style stream))
(terpri stream)
(finish-output stream)
(perform-hook :after-printing)
nil)
(defun vspace (&key
(stream *stream*)
(space 3))
"Print vertical space, aka a SPACE amount of newlines,
to STREAM (standard output by default).
Hooks are performed before and after printing."
(perform-hook :before-printing)
(format stream "~v&" space)
(terpri stream)
(finish-output stream)
(perform-hook :after-printing))
(defun banner (title &key
(stream *stream*)
(base-style :default)
(width 0)
(space 1)
(left-space 5)
(align :left)
(filler #\-))
"Print TITLE in-between two horizontal spaces (hr), with vertical space before and after.
SPACE controls how many blank lines are added before and after the title,
LEFT-SPACE helps to center the title a bit (defaults to 5)."
(perform-hook :before-printing)
(with-reasonable-width width
(align-object width align)
(vspace :space space)
(print-filler filler width base-style stream)
(format stream "~&~a" (make-string left-space :initial-element #\Space))
(cat-print title :align align :base-style base-style :stream stream)
(print-filler filler width base-style stream)
(vspace :space space))
(terpri stream)
(finish-output stream)
(perform-hook :after-printing))
(defun banner-fmt (title &rest args)
"Same result as BANNER with default styling arguments, but accepts a TITLE with CL:FORMAT control strings that are formatted with ARGS.
Usage:
(banner-fmt \"file ~a\" \"test.csv\")
=>
--------------------------------------------------------------------------------
file test.csv
--------------------------------------------------------------------------------
Is equivalent to:
(term:banner (format nil \"title ~a\" \"test.csv\"))
but you can pass key arguments to the latter."
(banner (apply #'format nil title args)))
(defun title (title &rest rest &key &ALLOW-OTHER-KEYS)
"Like a BANNER, but with no borders, so simply print TITLE with vertical space above and below.
Accepts the :ALIGN parameter: :left, :center, :right.
Passes other key arguments to BANNER."
(apply #'banner title :filler " " rest))
(defun title-fmt (title &rest args)
"Call the title function, but format TITLE with ARGS before.
See BANNER-FMT."
(title (apply #'format nil title args)))
(defun progress-bar (label progress
&key
(margin 0)
(label-style :default)
(filler #\#)
(bar-style :default)
(num-style :default)
(bar-width -40)
(stream *stream*))
"Print a progress bar with FILLER characters advanced to PROGRESS percent.
Needs an interactive terminal to have full effect.
On a dumb terminal like Emacs' Slime REPL, it doesn't respect the styles and it doesn't erase the bar on subsequent calls.
If PROGRESS is less than 100, move cursor to the
beginning of current line, so next invocation of `progress-bar' will rewrite
it.
This function doesn't print anything if PROGRESS is less than 100 and
output stream is not interactive or `*effects-enabled*' is NIL.
Insert MARGIN spaces, then LABEL (style for the label is set with LABEL-STYLE).
The size of the progress bar is set by BAR-WIDTH. If BAR-WIDTH is not a positive
number, `*terminal-width*' will be added to it to get positive BAR-WIDTH.
BAR-STYLE is the style that will be used for the bar itself, while NUM-STYLE
will be used for number of percents and some additional elements.
Output goes to STREAM."
(unless (effects-p stream)
(print-white-space margin stream)
(princ label stream)
(princ " ")
(format stream "~a" (make-string progress :initial-element filler))
)
(unless (and (< progress 100)
(not (effects-p stream)))
(perform-hook :before-printing)
(with-reasonable-width bar-width
(let* ((total-cells (- bar-width 8))
(filled-cells (floor (/ (* total-cells progress) 100)))
(empty-cells (- total-cells filled-cells)))
(print-white-space margin stream)
(set-style label-style stream)
(princ label stream)
(set-style :default)
(print-white-space
(- *terminal-width*
bar-width
margin
(length label))
stream)
(set-style num-style stream)
(princ #\[ stream)
(print-filler filler
filled-cells
bar-style
stream)
(print-white-space empty-cells stream)))
(set-style num-style stream)
(princ #\] stream)
(set-style :default stream)
(print-white-space 1 stream)
(set-style num-style stream)
(format stream "~3d" progress)
(set-style :default stream)
(print-white-space 1 stream)
(set-style num-style stream)
(princ #\% stream)
(set-style :default stream)
(if (< progress 100)
(format stream "~c[0G" #\escape)
(terpri stream))
(finish-output stream)
(perform-hook :after-printing))
nil)
(defun u-list (tree
&key
(bullet "*-~^")
(mark-suffix #\*)
(bullet-style :default)
(item-style :default)
(mark-style :default)
(margin 0)
(level-margin 2)
(fill-column 0)
(stream *stream*))
"Print an unordered list according to TREE. If we consider TREE a list,
every element must be either a printable object to print as a list item or a
list where CAR is the list item and CDR is sublist of the item.
Example:
(term:u-list '((:one one-a (:one-b :one-b-1 :one-b-2)) :two))
* ONE
- ONE-A
- ONE-B
~ ONE-B-1
~ ONE-B-2
* TWO
BULLET is a string. Each character will be used, each time in a row,
as the list bullet. They can be cycled over.
Example:
(term:u-list '((:one one-a (:one-b :one-b-1 :one-b-2)) :two)
:bullet #\+)
+ ONE
+ ONE-A
+ ONE-B
+ ONE-B-1
+ ONE-B-2
+ TWO
(term:u-list '((:one one-a (:one-b :one-b-1 :one-b-2)) :two)
:bullet \"+-\")
+ ONE
- ONE-A
- ONE-B
+ ONE-B-1
+ ONE-B-2
+ TWO
BULLET-STYLE is used for bullets. It can be also a list, in this
case it's possible to specify different styles for different levels of
nesting.
ITEM-STYLE is used to render the list items. MARK-STYLE is used for
items that end with MARK-SUFFIX (it can be any printable object).
LEVEL-MARGIN must be a positive integer that specifies how to increase
margin for every level of nesting, you can also use plain MARGIN.
FILL-COLUMN is used to split long items, if it's not a positive number,
`*terminal-width*' will be added to it to get positive FILL-COLUMN.
Output goes to STREAM."
(let ((bullet (ensure-circular-list
(coerce (string bullet) 'list)))
(bullet-style (ensure-circular-list bullet-style) )
(mark-suffix (string* mark-suffix)))
(labels ((print-item (level item bullet bullet-style)
(let ((margin (+ margin (* level level-margin))))
(print-white-space margin stream)
(set-style (car bullet-style) stream)
(princ (car bullet) stream)
(set-style :default stream)
(print-white-space (1- level-margin) stream)
(let* ((item (ensure-cons item))
(words (string* (car item))))
(print-words words
:base-style (if (ends-with-subseq mark-suffix
words)
mark-style
item-style)
:margin (+ margin level-margin)
:fill-column fill-column
:stream stream)
(dolist (subitem (cdr item))
(print-item (1+ level)
subitem
(cdr bullet)
(cdr bullet-style)))))))
(perform-hook :before-printing)
(dolist (item tree)
(print-item 0 item bullet bullet-style))
(finish-output stream)
(perform-hook :after-printing)
nil)))
(defun o-list (tree
&key
(index :arabic)
(delimiter #\.)
(mark-suffix #\*)
(index-style :default)
(item-style :default)
(mark-style :default)
(margin 0)
(level-margin 3)
(fill-column 0)
(stream *stream*))
"Print an ordered list according to TREE. If we consider TREE a list,
every element must be either a printable object to print as a list item or a
list where CAR is list item and CDR is sublist of the item.
Example:
(term:o-list '((:one one-a (:one-b :one-b-1 :one-b-2)) :two))
1. ONE
1. ONE-A
2. ONE-B
1. ONE-B-1
2. ONE-B-2
2. TWO
INDEX must be a
list designator, its elements should be keywords that denote how to
represent numeration. Acceptable values are:
:ARABIC—indexes will be printed as arabic numerals (default value)
:ROMAN—indexes will be printed as roman numerals
:LETTER—indexes will be printed as letters of Latin alphabet
:CAPITAL—the same as :LETTER, but capital letters are used
If there are more levels of nesting than elements in the list, it will be
cycled. The same applies to DELIMITER, which must be a string designator.
INDEX-STYLE is used for indexes. It can be also list, in this case it's
possible to specify different styles for different levels of nesting.
ITEM-STYLE is used to render the list items.
MARK-STYLE is used for items
that end with MARK-SUFFIX (it can be any printable object). LEVEL-MARGIN
must be a positive integer that specifies how to increase margin for every
level of nesting, you can also use plain MARGIN.
FILL-COLUMN is used to
split long items, if it's not a positive number, `*terminal-output*' will be
added to it to get positive FILL-COLUMN.
Output goes to STREAM."
(let ((index (ensure-circular-list index))
(index-style (ensure-circular-list index-style))
(delimiter (ensure-circular-list
(coerce (string delimiter) 'list)))
(mark-suffix (string* mark-suffix)))
(labels ((print-item (level i item index index-style delimiter)
(let ((margin (+ margin (* level level-margin)))
(image (case (car index)
(:roman (format nil "~@r~c"
(1+ i)
(car delimiter)))
(:letter (format nil "~c~c"
(code-char (+ 97 i))
(car delimiter)))
(:capital (format nil "~c~c"
(code-char (+ 65 i))
(car delimiter)))
(t (format nil "~d~c"
(1+ i)
(car delimiter))))))
(print-white-space margin stream)
(set-style (car index-style) stream)
(princ image stream)
(set-style :default stream)
(print-white-space (- level-margin (length image)) stream)
(set-style item-style stream)
(let* ((item (ensure-cons item))
(words (string* (car item))))
(print-words words
:base-style (if (ends-with-subseq mark-suffix
words)
mark-style
item-style)
:margin (+ margin level-margin)
:fill-column fill-column
:stream stream)
(do ((subitems (cdr item) (cdr subitems))
(i 0 (1+ i)))
((null subitems))
(print-item (1+ level)
i
(car subitems)
(cdr index)
(cdr index-style)
(cdr delimiter)))))))
(perform-hook :before-printing)
(do ((items tree (cdr items))
(i 0 (1+ i)))
((null items))
(print-item 0 i (car items) index index-style delimiter))
(finish-output stream)
(perform-hook :after-printing)
nil)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; ;;
;; Functions: tables ;;
;; ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; trivial-types
(defmacro %proper-list-p (var &optional (element-type '*))
`(loop
(typecase ,var
(null (return t))
(cons (if (or ,(eq element-type '*)
(typep (car ,var) ,element-type))
(setq ,var (cdr ,var))
(return)))
(t (return)))))
;; trivial-types
(defun issues/association-list-p (var)
"Returns true if OBJECT is an association list.
Examples:
(association-list-p 1) => NIL
(association-list-p '(1 2 3)) => NIL
(association-list-p nil) => T