hello-world.lisp
· 2.8 KiB · Common Lisp
Raw
;;; hello-name.lisp - A simple croatoan "Hello World" + name input example
;;;
;;; Prerequisites:
;;; 1. ncurses must be installed on your system
;;; macOS: brew install ncurses (usually pre-installed)
;;; Linux: apt-get install libncurses-dev
;;; 2. Load croatoan via Quicklisp:
;;; (ql:quickload :croatoan)
;;;
;;; To run: Load this file and call (hello-name)
;;; Note: Run from a real terminal, not from SLIME/Emacs REPL
;;; (ncurses doesn't work properly in SLIME)
(defpackage :hello-name
(:use :cl :croatoan)
(:export #:hello-name))
(in-package :hello-name)
(defun hello-name ()
"Display 'Hello World' and prompt for the user's name."
(with-screen (scr :input-echoing t
:input-buffering nil
:cursor-visible t
:enable-colors t)
;; Clear and set up
(clear scr)
;; Display "Hello, World!" at top
(move scr 2 5)
(setf (color-pair scr) '(:cyan :black))
(setf (attributes scr) '(:bold))
(add-string scr "Hello, World!")
;; Prompt for name
(setf (attributes scr) nil)
(setf (color-pair scr) '(:white :black))
(move scr 4 5)
(add-string scr "Enter your name: ")
(refresh scr)
;; Read the name character by character
(let ((name (make-array 0 :element-type 'character
:adjustable t
:fill-pointer 0)))
(event-case (scr event)
;; Enter key - finish input
(#\Newline
(return-from event-case))
(#\Return
(return-from event-case))
;; Backspace handling
(#\Backspace
(when (> (length name) 0)
(vector-pop name)
;; Move cursor back and erase character
(move scr 4 (+ 22 (length name)))
(add-char scr #\Space)
(move scr 4 (+ 22 (length name))))
(refresh scr))
;; Delete key (also backspace on some terminals)
(#\Rubout
(when (> (length name) 0)
(vector-pop name)
(move scr 4 (+ 22 (length name)))
(add-char scr #\Space)
(move scr 4 (+ 22 (length name))))
(refresh scr))
;; Regular character input
(otherwise
(when (and (characterp event)
(graphic-char-p event))
(vector-push-extend event name)
(add-char scr event)
(refresh scr))))
;; Display greeting
(setf (color-pair scr) '(:green :black))
(setf (attributes scr) '(:bold))
(move scr 6 5)
(add-string scr (format nil "Nice to meet you, ~A!" name))
(setf (attributes scr) nil)
(setf (color-pair scr) '(:white :black))
(move scr 8 5)
(add-string scr "Press any key to exit...")
(refresh scr)
;; Wait for a keypress before exiting
(get-char scr)
;; Return the name
name)))
| 1 | ;;; hello-name.lisp - A simple croatoan "Hello World" + name input example |
| 2 | ;;; |
| 3 | ;;; Prerequisites: |
| 4 | ;;; 1. ncurses must be installed on your system |
| 5 | ;;; macOS: brew install ncurses (usually pre-installed) |
| 6 | ;;; Linux: apt-get install libncurses-dev |
| 7 | ;;; 2. Load croatoan via Quicklisp: |
| 8 | ;;; (ql:quickload :croatoan) |
| 9 | ;;; |
| 10 | ;;; To run: Load this file and call (hello-name) |
| 11 | ;;; Note: Run from a real terminal, not from SLIME/Emacs REPL |
| 12 | ;;; (ncurses doesn't work properly in SLIME) |
| 13 | |
| 14 | (defpackage :hello-name |
| 15 | (:use :cl :croatoan) |
| 16 | (:export #:hello-name)) |
| 17 | |
| 18 | (in-package :hello-name) |
| 19 | |
| 20 | (defun hello-name () |
| 21 | "Display 'Hello World' and prompt for the user's name." |
| 22 | (with-screen (scr :input-echoing t |
| 23 | :input-buffering nil |
| 24 | :cursor-visible t |
| 25 | :enable-colors t) |
| 26 | ;; Clear and set up |
| 27 | (clear scr) |
| 28 | |
| 29 | ;; Display "Hello, World!" at top |
| 30 | (move scr 2 5) |
| 31 | (setf (color-pair scr) '(:cyan :black)) |
| 32 | (setf (attributes scr) '(:bold)) |
| 33 | (add-string scr "Hello, World!") |
| 34 | |
| 35 | ;; Prompt for name |
| 36 | (setf (attributes scr) nil) |
| 37 | (setf (color-pair scr) '(:white :black)) |
| 38 | (move scr 4 5) |
| 39 | (add-string scr "Enter your name: ") |
| 40 | (refresh scr) |
| 41 | |
| 42 | ;; Read the name character by character |
| 43 | (let ((name (make-array 0 :element-type 'character |
| 44 | :adjustable t |
| 45 | :fill-pointer 0))) |
| 46 | (event-case (scr event) |
| 47 | ;; Enter key - finish input |
| 48 | (#\Newline |
| 49 | (return-from event-case)) |
| 50 | (#\Return |
| 51 | (return-from event-case)) |
| 52 | |
| 53 | ;; Backspace handling |
| 54 | (#\Backspace |
| 55 | (when (> (length name) 0) |
| 56 | (vector-pop name) |
| 57 | ;; Move cursor back and erase character |
| 58 | (move scr 4 (+ 22 (length name))) |
| 59 | (add-char scr #\Space) |
| 60 | (move scr 4 (+ 22 (length name)))) |
| 61 | (refresh scr)) |
| 62 | |
| 63 | ;; Delete key (also backspace on some terminals) |
| 64 | (#\Rubout |
| 65 | (when (> (length name) 0) |
| 66 | (vector-pop name) |
| 67 | (move scr 4 (+ 22 (length name))) |
| 68 | (add-char scr #\Space) |
| 69 | (move scr 4 (+ 22 (length name)))) |
| 70 | (refresh scr)) |
| 71 | |
| 72 | ;; Regular character input |
| 73 | (otherwise |
| 74 | (when (and (characterp event) |
| 75 | (graphic-char-p event)) |
| 76 | (vector-push-extend event name) |
| 77 | (add-char scr event) |
| 78 | (refresh scr)))) |
| 79 | |
| 80 | ;; Display greeting |
| 81 | (setf (color-pair scr) '(:green :black)) |
| 82 | (setf (attributes scr) '(:bold)) |
| 83 | (move scr 6 5) |
| 84 | (add-string scr (format nil "Nice to meet you, ~A!" name)) |
| 85 | |
| 86 | (setf (attributes scr) nil) |
| 87 | (setf (color-pair scr) '(:white :black)) |
| 88 | (move scr 8 5) |
| 89 | (add-string scr "Press any key to exit...") |
| 90 | (refresh scr) |
| 91 | |
| 92 | ;; Wait for a keypress before exiting |
| 93 | (get-char scr) |
| 94 | |
| 95 | ;; Return the name |
| 96 | name))) |