项目作者: dys-bigwig

项目描述 :
ncurses API/FFI for Racket
高级语言: Racket
项目地址: git://github.com/dys-bigwig/racket-ncurses.git
创建时间: 2018-12-03T10:11:57Z
项目社区:https://github.com/dys-bigwig/racket-ncurses

开源协议:

下载


racket-ncurses

Heavily WIP and therefore subject to change. However, feel free to use what is currently supported to your heart’s content. Feedback is greatly appreciated. Requires libncurses5.

First, install this package by typing “raco pkg install https://github.com/dys-bigwig/racket-ncurses.git“ into your terminal.

Now you can start using the libary by adding (require ncurses) to the top of your project.\
Here’s the (admittedly silly) example from test.rkt to give an example of what you can currently do:

  1. #lang racket
  2. (require ncurses)
  3. (define (test)
  4. (noecho!)
  5. (curs-set 0)
  6. (border)
  7. (define-values (screen-height screen-width)
  8. (getmaxyx))
  9. (init-pair! 1 COLOR_WHITE COLOR_RED)
  10. (let ([ch (getch)])
  11. (addstr (format "YOU PRESSED ~a!" ch)
  12. (color-pair 1) A_BOLD A_UNDERLINE
  13. #:y (quotient screen-height 2)
  14. #:x (quotient (- screen-width
  15. (string-length "YOU PRESSED ~a!"))
  16. 2)))
  17. (define foowin (newwin 10 10 4 4))
  18. (addstr "WIZARDS" #:win foowin)
  19. (refresh)
  20. (getch #:win foowin)
  21. (delwin foowin))
  22. (with-ncurses test)

The with-ncurses function is used at the start of your program to automatically initialise ncurses - if you’ve used the Python api for ncurses before, it serves the same purpose as its wrapper function. You pass your main function as a callback, and it will be run after the curses environment has been initialised. It takes #:start-color? as a keyword argument; however, color will be enabled by default, so you need only use this argument if you wish for color to be disabled, in which case you would pass #f as the argument.

Though the usage of curses itself is somewhat outside the scope of this readme, here is a brief explanation of the functions used, with a particular emphasis upon those elements which are racket-specific:

  • getmaxyx - returns the height and width (maximum y and x values) of the screen as values. Due to the result being returned as values, define-values is best used for binding its results.
  • init-pair! - used to initialise a pair of colors for use as a text attribute. The first color is the foreground (i.e. the color of the text itself) and the second colour is the background. The list of predefined colors is:
    1. COLOR_BLACK
    2. COLOR_RED
    3. COLOR_GREEN
    4. COLOR_YELLOW
    5. COLOR_BLUE
    6. COLOR_MAGENTA
    7. COLOR_CYAN
    8. COLOR_WHITE
  • getch - reads a single character. You can use the keyword argument #:win to read from specific window. If no argument is given, then stdscr (the default screen which is initialised automatically using with-curses) will be used.
  • addstr - draws a string to the chosen window (default is stdscr). The keyword arguments #:y, #:x, and #:win can be used to specify the location the string is to be drawn, and the window to which it is to be drawn. The default x and y positions are the current cursor position. Any number of attributes - such as bold, underline… and a single color-pair - can be provided after the string argument as a rest-arg. In the above example, (color-pair 1), A_BOLD and A_UNDERLINE are given as attributes to be apllied to the string. The list of text attributes is:
    1. A_NORMAL
    2. A_STANDOUT
    3. A_UNDERLINE
    4. A_REVERSE
    5. A_BLINK
    6. A_DIMMED
    7. A_BOLD
    8. A_ALTCHARSET
    9. A_INVISIBLE
    10. A_PROTECT
    11. A_HORIZONTAL
    12. A_LEFT
    13. A_LOW
    14. A_RIGHT
    15. A_TOP
    16. A_VERTICAL
  • addch - same as addstr, but adds a single Racket char (e.g. #\A)
  • addchstr - similar to addstr, but with some difference which are outside the scope of this readme. For one, it does not advance the cursor, whereas addstr does.
  • newwin - creates a new window, taking the following arguments: lines, columns, x-origin, y-origin.
  • delwin - deletes a window.
  • refresh - refreshes the window given as #:win kwarg, with the default being stdscr.
  • echo! and noecho! - toggles whether typed text is echoed back to the user or not.
  • curs-set - toggles cursor visibility, with 0 being invisible and 2 being bold.
  • nodelay takes #f or #t, setting getch to be blocking or non-blocking respectively.

If you’re working on a particular window (other than stdscr) and you don’t want to keep providing the #:win kwarg over-and-over again, you can use parameterize (https://docs.racket-lang.org/guide/parameterize.html) to set stdscr to your window of choice within the parameterize block.