nice-curses
所属分类:网络编程
开发工具:D
文件大小:0KB
下载次数:0
上传日期:2019-06-16 16:28:36
上 传 者:
sh-1993
说明: 用于D编程语言的ncurses包装器和UI库,
(A ncurses wrapper and an UI library for D programming language ,)
文件列表:
CODESHELTER.md (4035, 2019-06-16)
LICENSE (1338, 2019-06-16)
dub.sdl (456, 2019-06-16)
examples/ (0, 2019-06-16)
examples/colors/ (0, 2019-06-16)
examples/colors/dub.sdl (175, 2019-06-16)
examples/colors/source/ (0, 2019-06-16)
examples/colors/source/app.d (434, 2019-06-16)
examples/hello/ (0, 2019-06-16)
examples/hello/dub.sdl (180, 2019-06-16)
examples/hello/source/ (0, 2019-06-16)
examples/hello/source/app.d (355, 2019-06-16)
examples/more-ui/ (0, 2019-06-16)
examples/more-ui/dub.sdl (177, 2019-06-16)
examples/more-ui/source/ (0, 2019-06-16)
examples/more-ui/source/app.d (894, 2019-06-16)
examples/rainbow/ (0, 2019-06-16)
examples/rainbow/dub.sdl (191, 2019-06-16)
examples/rainbow/source/ (0, 2019-06-16)
examples/rainbow/source/app.d (1494, 2019-06-16)
examples/ranges/ (0, 2019-06-16)
examples/ranges/dub.sdl (182, 2019-06-16)
examples/ranges/source/ (0, 2019-06-16)
examples/ranges/source/app.d (2445, 2019-06-16)
examples/timeout/ (0, 2019-06-16)
examples/timeout/dub.sdl (201, 2019-06-16)
examples/timeout/source/ (0, 2019-06-16)
examples/timeout/source/app.d (1209, 2019-06-16)
examples/ui/ (0, 2019-06-16)
examples/ui/dub.sdl (176, 2019-06-16)
examples/ui/source/ (0, 2019-06-16)
examples/ui/source/app.d (2383, 2019-06-16)
examples/unicode/ (0, 2019-06-16)
examples/unicode/dub.sdl (188, 2019-06-16)
examples/unicode/source/ (0, 2019-06-16)
examples/unicode/source/app.d (455, 2019-06-16)
source/ (0, 2019-06-16)
source/nice/ (0, 2019-06-16)
... ...
[![Code Shelter](https://www.codeshelter.co/static/badges/badge-flat.svg)](https://www.codeshelter.co/)
# Introduction
Using D bindings of ncurses library directly is a bit of a pain - error codes
instead of exceptions, pointers instead of strings, multitude of functions to
do essentially the same thing make writing D programs quite awkward.
'nice-curses' attempts to provide a nicer object-oriented interface to the
library, taking advantage of D's exception mechanism and its ability to
overload functions.
'nice-curses' can also serve as an UI library, providing several basic UI
elements and container for them.
The library consists of three modules: `nice.curses`, which provides wrappers
over ncurses library, `nice.ui.base`, which provides the basis on which UIs are
built, and `nice.ui.elements`, which provides some predefined UI element
classes. `nice.ui.base` publicly imports `nice.curses` and `nice.ui.elements`
publicly imports both `nice.curses` and `nice.ui.base`.
# Initialization
The library needs to be initialized prior to using it. Initialization is quite
simple: you create a Curses.Config struct (see Curses.Config subsection for
details) and feed it to the Curses constructor. In other words
Curses.Config cfg = { /* Desired configuration. */ };
auto curses = new Curses(cfg);
That's it. The library is initialized and ready for work. As a side note,
consider adding `scope(exit) destroy(curses);` somewhere nearby.
Note that Unicode input and output, while supported, is very likely to be
affected by the locale settings of your terminal. If you see garbage, check
which locale you're using.
## struct Curses.Config
A Curses.Config struct has following fields:
- `bool useColors = true` - set this to request color support from the library.
- `bool useStdColors = true` - set this to fill `colors` ColorTable field of a
Curses object with color pairs of default colors.
- `bool disableEcho = false` - set this to enter noecho mode;
- `Mode mode = Mode.normal` - choose one of the terminal modes (consult your
ncurses documentation for difference between them):
- normal
- cbreak
- halfdelay
- raw
- `int cursLevel = 1` - set cursor visibility. 0 means invisible, 1 means
normal and 2 means very visible.
- `bool initKeypad = true` - set this to make all spawned windows to enter
keypad mode (this is most likely what you want, consult `man keypad`).
- `bool nl = false` - when set, the program will enter `nl` mode, otherwise the
program will enter `nonl` mode (consult ncurses docs for this).
- `bool nodelay = false` - when set, the program will enter nodelay mode.
# Usage as an ncurses wrapper
Once the library is set up, you can either create new windows with `newWindow`
method of a Curses object, or use its field `stdscr`. Perform some drawing on
them, then call `refresh` method of a Window object on each, and then call
`update` method of the Curses object.
## class Curses
Curses objects have following public fields:
- `Window stdscr` - default drawing Window.
- `ColorTable colors` - a container for defined color pairs.
- `static bool echoMode` - indicates whether the program is in echo or noecho
mode.
Curses object provides following methods:
- `Window newWindow(int nlines, int ncols, int begin_y, int begin_x)` - create
a new window.
- `Window duplicateWindow(Window which)` - create an exact copy of a window.
- `void deleteWindow(Window which)` - delete a window and all its subwindows.
- `void setMode(Mode mode, int delayForHD = 1)` - enter one of the terminal
modes (normal, cbreak, halfdelay and raw). Optional argument `delayForHD`
sets the delay for halfdelay mode.
All the other methods are static:
- `static void beep()` - beeps.
- `static void delayOutput(int ms)` - pause the output for a given amount of
milliseconds.
- `static void echo(bool set)` - enter either echo or noecho mode.
- `static void flash()` - flashes the screen.
- `static void flushInput()` - discard any unprocessed input from the user.
- `static void nap(int ms)` - pause for a given number of milliseconds.
- `static void resetTTY()` - reset TTY to a previously saved state.
- `static void saveTTY()` - save TTY state.
- `static void setCursor(int level)` - set cursor visibility (0 means
invisible, 1 - normal, 2 - very visible).
- `static void ungetch(int ch)` - put a character back into input.
- `static void update()` - perform actual drawing from the backbuffer.
- `static RGB colorContent(short color)` - returns the (red, green, blue) color
content of a given color.
- `static string keyName(int key)` - return the name of a given key.
There are also several static properties:
- `static int lines` - number of lines in the screen.
- `static int cols` - number of columns in the screen.
- `static int maxColors` - maximum number of supported colors.
- `static int maxColorPairs` - maximum number of supported color pairs.
- `static int tabSize` - amount of spaces in a tab character.
- `static int escDelay` - the number of milliseconds to wait after reading an
escape character.
- `static int baudrate` - the output speed of the terminal.
- `static bool canChangeColor` - indicates whether it's possible to redefine
colors.
- `static bool hasColors` - indicates whether the terminal supports colors.
## class Window
Objects of Window class have a single public field: `ColorTable colors`, which
they inherit from the Curses object they were created with. There's no publicly
available constructor for Window class. You have to create them either via
`newWindow` or `duplicateWindow` methods of the Curses class or via `subwin`
and `derwin` methods of the Window class.
These are the methods for general manipulation of windows:
- `void keypad(bool set)` - enter or leave keypad mode.
- `void move(int y, int x)` - move the cursor to the specified position. May
throw a NCException if the move is impossible.
- `void moveWindow(int y, int x)` - move the window itself. May throw a
NCException if the move would result in a part of the window being offscreen.
- `void refresh()` - makes next call to `Curses.update` draw the latest changes
to the window.
- `void bkgd(chtype ch)` - see 'man 3 bkgd'.
- `void bkgdset(chtype ch)` - see 'man 3 bkgdset'.
- `void clear()` - clear the window. For difference with `erase` consult
'man 3 clear'.
- `void clearToBottom()` - clear the window from the current cursor position to
the bottom.
- `void clearToEOL()` - clear the window from the cursor position to the end of
line.
- `void deleteln()` - delete a line under the cursor.
- `void erase()` - clear the screen. For difference with `clear` consult
'man 3 clear'.
- `void insdel(int n)` - delete and insert `n` lines under the cursor.
- `void insertln()` - insert a line at the top of the current line.
- `void scroll(int n)` - scroll the window by `n` lines.
- `void timeout(int ms)` - sets the timeout value to ms milliseconds, see 'man 3 timeout'.
These are the methods for child windows management:
- `Window subwin(int nlines, int ncols, int y, int x)` - create a subwindow.
- `Window derwin(int nlines, int ncols, int y, int x)` - derive a window.
- `void deleteChild(Window child)` - deletes a child window.
For difference between `subwin` and `derwin` consult your ncurses documentation.
These are the drawing primitives. Those that take coordinates all throw a
NCException if the requested position is outside the window. For those
functions that take `cchar_t` arguments, consider constructing them via `CChar`
struct (see CChar section on details). Wherever a String template parameter is
used, it means an input range with element type implicitly castable to `dchar`.
Wherever a Range template parameter is mentioned, it means an input range with
element type implicitly castable to `chtype`.
- `void addch(C: wint_t, A: chtype)(int y, int x, C ch, A attr = Attr.normal)`
Draws a single character at the given position with given attribute.
- `void addch(C: wint_t, A: chtype)(C ch, A attr = Attr.normal)` Draws a single
character at the current cursor position with given attribute.
- `void addnstr(String, Range)(int y, int x, String str, int n, Range attrs, OOB onOOB = OOB.ignore)`
Draws a string at the given position, but no more than `n` characters. Each
character gets an attribute from `attrs` range (`str` and `attrs` are
iterated over in lockstep). Drawing stops when any of the following
conditions occur: `str` is exausted, `attrs` is exausted, `n` characters were
drawn, window's lower right corner was reached. If the string doesn't fit
into the window and `onOOB` is set to `OOB.except`, an exception will be
thrown. Otherwise the method will silently drop the rest of the input.
- `void addnstr(String, Range)(String str, int n, Range attrs, OOB onOOB = OOB.ignore)`
Same as before, but uses current cursor coordinates.
- `void addnstr(String, A: chtype)(int y, int x, String str, int n, A attr = Attr.normal, OOB onOOB = OOB.ignore)`
Same as the first one, but uses the same attribute for the entire string.
- `void addnstr(String, A: chtype)(String str, int n, A attr = Attr.normal, OOB onOOB = OOB.ignore)`
Same as the previous one, but uses current cursor coordinates.
- `void addstr(String, Range)(int y, int x, String str, Range attrs, OOB onOOB = OOB.ignore)`
Same as the first `addnstr`, but doesn't impose a restriction on the number
of written characters.
- `void addstr(String, Range)(String str, Range attrs, OOB onOOB = OOB.ignore)`
Same as the previous one, but uses current cursor's coordinates.
- `void addstr(String, A: chtype)(int y, int x, String str, A attr = Attr.normal, OOB onOOB = OOB.ignore)`
Same as the first `addstr`, but uses the same attribute for the entire
string.
- `void addstr(String, A: chtype)(String str, A attr = Attr.normal, OOB onOOB = OOB.ignore)`
Same as the previous one, but uses current cursor's cooridnates.
- `void addAligned(String, Range)(int y, int x, String str, Align alignment, Range attrs, OOB onOOB = OOB.ignore)`
The behaviour depends on the `alignment` parameter. If it's `Align.left` then
y and x are the coordinates of the text's upper-left corner, and the text
will be left-justified. If it's `Align.center` then y and x are the
coordinates of the first line's center, and the text will be centered around
this point. If it's `Align.right`, then y and x are the coordinates of the
text's upper-right corner, and the text will be right-justified. If `onOOB` is
set to `OOB.ignore` the text that doesn't fit into the window will be silently
discarded, otherwise an exception will be thrown.
- `void addAligned(String, A: chtype)
(int y, int x, String str, Align alignment, A attr = Attr.normal, OOB onOOB = OOB.ignore)`
Same as before, but uses the same attribute for the entire string.
- `void addAligned(String, Range)(int y, String str, Align alignment, Range attrs, OOB onOOB = OOB.ignore)`
Same as the first one, but uses the whole window's width and figures out x
coordinate from the `alignment` parameter.
- `void addAligned(String, A: chtype)(int y, String str, Align alignment, A attr, OOB onOOB = OOB.ignore)`
Same as the previous one, but uses the same attribute for the entire string.
- `void border(chtype left, chtype right, chtype top, chtype bottom,
chtype topLeft, chtype topRight, chtype bottomLeft, chtype bottomRight)`
Draws a border around window edges.
- `void border(cchar_t left, cchar_t right, cchar_t top, cchar_t bottom,
cchar_t topLeft, cchar_t topRight, cchar_t bottomLeft, cchar_t bottomRight)`
Draws a border around window edges using wide characters.
- `void box(chtype vertical, chtype horizontal)` - draws a box around window
edges.
- `void box(cchar_t vertical, cchar_t horizontal)` - ditto.
- `void delch(int y, int x)` - delete a character at the given position.
- `void delch()` - delete a character under the cursor.
- `void insert(int y, int x, chtype ch)` - insert a character at the given
position.
- `void insert(chtype ch)` - insert a character at the cursor position.
- `void insert(int y, int x, string str)` - insert a string at the given
position.
- `void insert(string str)` - insert a string at the current cursor position.
- `void hline(int y, int x, chtype ch, int n)` - draw a horizontal line of `n`
characters at the given position.
- `void hline(chtype ch, int n)` - draw a horizontal line of `n` characters at
the current cursor position.
- `void hline(int y, int x, cchar_t ch, int n)` - draw a horizontal line of `n`
characters at the given position.
- `void hline(cchar_t ch, int n)` - draw a horizontal line of `n` characters at
the current cursor position.
- `void vline(int y, int x, chtype ch, int n)` - draw a vertical line of `n`
characters at the given position.
- `void vline(chtype ch, int n)` - draw a vertical line of `n` characters at
the current cursor position.
- `void vline(int y, int x, cchar_t ch, int n)` - draw a vertical line of `n`
characters at the given position.
- `void vline(cchar_t ch, int n)` - draw a vertical line of `n` characters at
the current cursor position.
- `void overlay(Window target)` - overlay this window on top of another. Blanks
are not copied.
- `void overwrite(Window target)` - overlay this window on top of another.
Blanks are copied.
These are for retrieving information from the window.
- `int getch()` - get a single narrow key from the keyboard. Might throw in
timeout mode (see `timeout` method).
- `WChar getwch()` - get a single wide character from the keyboard. Might throw
in timeout mode (see `timeout` method).
- `int begX()` - get the X coordinate of the window's upper-left corner.
- `int begY()` - get the Y coordinate of the window's upper-left corner.
- `int curX()` - get the X coordinate of the cursor.
- `int curY()` - get the Y coordinate of the cursor.
- `int width()` - get the width of the window.
- `int height()` - get the height of the window.
- `string getstr(int maxLength, bool echoChars = true)` - get a string of
characters at most `maxLength` long. May or may not echo the characters
being typed.
- `string getstr(bool echoChars = true)` - get a string of characters. May or
may not echo the characters being typed.
- `string getstr(bool delegate(wint_t) predicate, bool echoChars = true)` - get
a string of characters, blocking all keys such that the given predicate
returns false on them. May or may not echo the characters being typed.
- `chtype[] inchstr()` - get an array of characters and attributes from the
cursor to the right margin of the window.
- `chtype[] inchstr(int n)` - same, but limit the maximum amount of the
characters read.
- `chtype[] inchstr(int y, int x)` - same, but move the cursor to the given
position first.
- `chtype[] inchstr(int y, int x, int n)` - same, but with limit on the length
of the resulting array and with moving the cursor.
## class ColorTable
A ColorTable object stores information about defined colors and color pairs.
It can be indexed in two ways:
- By a pair of `short`s: the first is foreground color index, the second is
background.
- By a single `short`: it is a color pair index.
Indexing yields an attribute that can be used as a parameter to `addch`,
`addstr` and the like. It may throw an exception if a requested pair is not in
the table.
Apart of this, a ColorTable object has following methods:
- `short addPair(short fg, short bg)` - define a new color pair and return its
index. Throws an exception if a new pair cannot be added.
- `void redefineColor(short color, short r, short g, short b)` - redefine a
color with given index. Throws an exception if the color cannot be redefined.
- `short addColor(short r, short g, short b)` - add a new color with given RGB
content. Return the index of the new color. Throws an exception if a new
color cannot be added.
- `short addColor(RGB color)` - same.
- `void removePair(short pairIndex)` - mark a pair for overwriting.
- `void removeColor(short colorIndex)` - mark a color for overwriting.
- `void initDefaultColors()` - initialize color pairs from StdColor enum. This
is called if you request standard colors in Curses configuration.
## Helper things
### struct WChar
The return type of the `getwch()` method. Has following (read-only) properties:
- `bool isSpecialKey` - indicates whether the WChar contains a result of
pressing a function key.
- `int key` - if `isSpecialKey` is `true`, contains the pressed key. Undefined
otherwise.
- `wint_t chr` - if `isSpecialKey` is `false`, contains the typed character.
Undefined otherwise.
### struct CChar
A struct representing a wide character with attributes. Uses `alias this`, so
it can be used whereever a `cchar_t` is expected.
Two constructors are available:
```
this(wint_t chr, chtype attr = Attr.normal)
this(const wint_t[] chars, chtype attr = Attr.normal)
```
The first one constructs a CChar from a single wide character, the second one -
from a string of wide characters.
### enum Attr
A set of possible (and binary OR-able) attributes for drawing characters.
Following attributes are available:
- `normal`
- `charText`
- `color`
- `standout`
- `underline`
- `reverse`
- `blink`
- `dim`
- `bold`
- `altCharSet`
- `invis`
- `protect `
- `horizontal`
- `left`
- `low`
- `right`
- `top`
- `vertical`
### enum StdColor
A set of default colors:
- `black`
- `red`
- `green`
- `yellow`
- `blue`
- `magenta`
- `cyan`
- `white`
### struct RGB
Used to represent colors. Has three fields:
- `short r` - the red component.
- `short g` - the green component.
- `short b` - the blue component.
### enum Key
A set of constants that `getch` may return. For the full set, see the table in
`man 3 getch`. As a general rule, you can get a Key.something from
corresponding KEY\_SOMETHING by dropping KEY\_ part and changing the rest to
lower case. The only exception is KEY\_BREAK, with corresponding Key.codeBreak,
to avoid using a keyword as an identifier.
### enum Align
- `left`
- `center`
- `right`
# Usage as an UI library
When using 'nice-curses' as a UI library, you still need to initialize it as
was shown before. In addition, you need to create an UI object. The constructor
takes a Curses object, a Window object, and an optional UI.Config struct:
UI.Config cfg = { /* Some configuration. */ };
auto ui = new UI(curses, curses.stdscr, cfg);
After that, you can construct some UI elements. All of the currently available
UI element classes take UI object as the first argument of their constructors
so there's no need to explicitly add them to the UI object via `addElement`
method.
Assuming you use the usual processing loop approach, you need then to read
keyboard input with `getwch` and feed it to the UI via its
`void keystroke(WChar key)` method. Note that this method may raise an
exception to deliver UI elements' reaction to the key to the main loop. Those
exceptions all inherit from UISignal class, and have `sender` field which
contains the element that reacted to the keystroke.
Note that while whenever a list of `WChar`s is given below, the conversion from
plain `char` or `Key` via `WChar`s constructor is omitted to avoid unnecessary
clutter.
Below will be given the list of currently available pre-made UIElement classes.
## class UI
An UI object has following publicly available methods:
- `void keystroke(WChar key)` - process a keystroke from the user. This can
change currently focused element and raise an exception inheriting from
UISignal class.
- `void draw(bool erase = true)` - draw the UI ... ...
近期下载者:
相关文件:
收藏者: