RSS Feed

Lisp Project of the Day


You can support this project by donating at:

Donate using PatreonDonate using Liberapay

Or see the list of project sponsors.


Tests 🤨
CI 🥺

This library is binding to NCurses. It allows for creating windowing interfaces for the console. With NCurses you can print to any place of the screen, change colors and probably do some other awesome things.

I took a few examples from the original NCurses documentation:

It was pretty straightforward to port them to Common Lisp.

The first example just wait for any key and print this key in bold:

(defun main (&rest argv)
  (declare (ignorable argv))
  (keypad *stdscr* 1)
  (printw "Type any character to see it in bold:
  (let ((ch (getch)))
      ((= ch 265)
       (printw "F1 Key pressed"))
       (printw "The pressed key is ")
       (attron a_bold)
       (printw (format nil "~A~%" (code-char ch)))
       (attroff a_bold)))

Second example a little bit complex. It uses fetches the size of the terminal and prints "Hello Lisp Word!" in the centre. After that, it draws a border using pseudographics:

Here is the code:

(defun main (&rest argv)
  "This example is based on:"
  (declare (ignorable argv))
  (init-pair 1

  (let ((text "Hello Lisp World!"))
    (multiple-value-bind (screen-height screen-width)
        (get-maxyx *stdscr*)
      (let* ((text-len (length text))
             (box-width (+ text-len 4))
             (box-height 5)
             (box-x (round (/ (- screen-width box-width)
             (box-y (round (/ (- screen-height box-height)
             (plus (char-code #\+))
             (h-line (char-code #\-))
             (v-line (char-code #\|)))

        (attron (color-pair 1))

        ;; It's time to print a text in the center of the screen:
        (mvprintw (+ box-y 2) (+ box-x 2) text)
        ;; Now we'll draw a border around the box.
        ;; Horizontal lines:
        (mvhline box-y box-x
                 h-line box-width)
        (mvhline (+ box-y (1- box-height)) box-x
                 h-line box-width)
        ;; Vertical lines:
        (mvvline box-y box-x
                 v-line box-height)
        (mvvline box-y (+ box-x (1- box-width))
                 v-line box-height)
        ;; And corners
        (mvaddch box-y box-x
        (mvaddch box-y (+ box-x (1- box-width))
        (mvaddch (+ box-y (1- box-height))
                 (+ box-x (1- box-width))
        (mvaddch (+ box-y (1- box-height)) box-x
        (move 0 0))))

  ;; Wait for key press

Full examples are available here:

That is it. If you need an interactive interface in the console, use cl-ncurses!

Brought to you by 40Ants under Creative Commons License