CLI Extension - :q
[clnl] / src / main / cli.lisp
1 (in-package #:clnl-cli)
2
3 (defvar *cli* nil)
4 (defvar *cli-dims* (list 0 0))
5
6 (defvar *info* nil)
7
8 (defun run ()
9  "RUN => RESULT
10
11 ARGUMENTS AND VALUES:
12
13   RESULT: undefined, should never get here
14
15 DESCRIPTION:
16
17   RUN runs the command line interface in the running terminal.
18
19   This should become the main REPL for a CLNL program.  If you want to use you're
20   own REPL, you should use the rest of the functions in CLNL to recreate it."
21  (initscr)
22  (init-interface)
23  (loop
24   :for str := (cffi:with-foreign-pointer-as-string (str 255) (wgetnstr *cli* str 255))
25   :while str
26   :do (print-command-and-response str (execute str)))
27  (endwin)
28  (sb-ext:exit :abort t))
29
30 (defun execute (str)
31  (handler-case
32   (with-output-to-string (*standard-output*)
33    (clnl:run-commands str))
34   (error (e) (format nil "Ok, something went wrong: ~A" e))))
35
36 ; for ui, we need to do at a minimum:
37 ; - cli, first pass, read things in, bottom of the screen,
38 ;     - just use getstr with a limit to be something like screen width - 10 for now
39 ;   - print what the user inputted, dont' bomb on error messages, give target for show
40 ;     - for overly long printing, go ahead and truncate with "..."
41 ; - for info, this should put out a simple message about the program, maintainer, purpose, links, etc
42
43 (defun init-interface ()
44  (let
45   ((cli-height (min 10 (floor (* *lines* .3)))))
46   (setup-info (- *lines* cli-height) *cols*)
47   (setup-cli cli-height *cols* (- *lines* cli-height))))
48
49 (defun refresh-cli ()
50  (wmove *cli* (1- (car *cli-dims*)) 0)
51  (whline *cli* (char-code #\Space) (cadr *cli-dims*))
52  (mvwprintw *cli* (1- (car *cli-dims*)) 0 ">")
53  (wmove *cli* (1- (car *cli-dims*)) 2)
54  (wrefresh *cli*))
55
56 (defun print-command-and-response (command response)
57  (loop
58   :for i :from 1 :to (- (car *cli-dims*) 3)
59   :do (wmove *cli* i 0)
60   :do (whline *cli* (char-code #\Space) (cadr *cli-dims*)))
61  (mvwprintw *cli* 1 0 (format nil "> ~A" command))
62  (mvwprintw *cli* 3 0 (format nil "~A" response))
63  (refresh-cli))
64
65 (defun setup-cli (height width top)
66  (setf *cli* (newwin height width top 0))
67  (setf *cli-dims* (list height width))
68  (whline *cli* 0 (cadr *cli-dims*))
69  (wmove *cli* (- (car *cli-dims*) 2) 0)
70  (whline *cli* (char-code #\.) (cadr *cli-dims*))
71  (wmove *cli* (1- (car *cli-dims*)) 2)
72  (wrefresh *cli*)
73  (refresh-cli))
74
75 (defun setup-info (num-tall num-wide)
76  (let*
77   ((info "
78        / \\
79       /   \\     Welcome to CLNL version ~A!
80      /     \\
81     /_______\\
82   
83   CLNL is an experiment at creating an alternate
84   implementation of NetLogo.
85   
86   You can enter in various netlogo commands below,
87   or use :q to quit the program.
88   
89   See http://github.com/frankduncan/clnl for more
90   information about CLNL and to keep apprised of
91   any updates that may happen.")
92    (info-height (length (cl-ppcre:split "\\n" info)))
93    (info-width (apply #'max (mapcar #'length (cl-ppcre:split "\\n" info)))))
94   (setf *info* (newwin
95                 (+ 3 info-height)
96                 (+ 2 info-width)
97                 (max 0 (floor (- num-tall info-height) 2))
98                 (max 0 (floor (- num-wide info-width) 2))))
99   (mvwprintw *info* 1 0
100    (format nil info
101     (asdf:component-version (asdf:find-system :clnl))))
102   (box *info* 0 0)
103   (wrefresh *info*)))