| 1 | module ncurses |
| 2 | |
| 3 | // Window is an opaque handle to an ncurses window. |
| 4 | pub type Window = voidptr |
| 5 | |
| 6 | $if !windows { |
| 7 | $if $pkgconfig('ncursesw') { |
| 8 | #pkgconfig --cflags --libs ncursesw |
| 9 | } $else $if $pkgconfig('ncurses') { |
| 10 | #pkgconfig --cflags --libs ncurses |
| 11 | } $else { |
| 12 | #flag darwin -D_DARWIN_C_SOURCE -lncurses |
| 13 | #flag linux -lncursesw |
| 14 | #flag freebsd -lncursesw |
| 15 | #flag openbsd -lcurses |
| 16 | #flag netbsd -lcurses |
| 17 | #flag dragonfly -lncurses |
| 18 | #flag solaris -lncurses |
| 19 | } |
| 20 | #insert "@VEXEROOT/vlib/ncurses/ncurses_helpers.h" |
| 21 | |
| 22 | fn C.v_ncurses_initscr() Window |
| 23 | fn C.v_ncurses_stdscr() Window |
| 24 | fn C.v_ncurses_endwin() int |
| 25 | fn C.v_ncurses_cbreak() int |
| 26 | fn C.v_ncurses_nocbreak() int |
| 27 | fn C.v_ncurses_raw() int |
| 28 | fn C.v_ncurses_noraw() int |
| 29 | fn C.v_ncurses_echo() int |
| 30 | fn C.v_ncurses_noecho() int |
| 31 | fn C.v_ncurses_keypad(win Window, enabled int) int |
| 32 | fn C.v_ncurses_nodelay(win Window, enabled int) int |
| 33 | fn C.v_ncurses_timeout(delay int) |
| 34 | fn C.v_ncurses_wtimeout(win Window, delay int) |
| 35 | fn C.v_ncurses_curs_set(visibility int) int |
| 36 | fn C.v_ncurses_clear() int |
| 37 | fn C.v_ncurses_refresh() int |
| 38 | fn C.v_ncurses_getch() int |
| 39 | fn C.v_ncurses_addstr(text &char) int |
| 40 | fn C.v_ncurses_mvaddstr(y int, x int, text &char) int |
| 41 | fn C.v_ncurses_newwin(lines int, cols int, begin_y int, begin_x int) Window |
| 42 | fn C.v_ncurses_delwin(win Window) int |
| 43 | fn C.v_ncurses_box(win Window, vertical u32, horizontal u32) int |
| 44 | fn C.v_ncurses_getmaxx(win Window) int |
| 45 | fn C.v_ncurses_getmaxy(win Window) int |
| 46 | fn C.v_ncurses_wrefresh(win Window) int |
| 47 | fn C.v_ncurses_wclear(win Window) int |
| 48 | fn C.v_ncurses_wgetch(win Window) int |
| 49 | fn C.v_ncurses_wmove(win Window, y int, x int) int |
| 50 | fn C.v_ncurses_waddstr(win Window, text &char) int |
| 51 | fn C.v_ncurses_mvwaddstr(win Window, y int, x int, text &char) int |
| 52 | fn C.v_ncurses_start_color() int |
| 53 | fn C.v_ncurses_has_colors() int |
| 54 | fn C.v_ncurses_init_pair(pair i16, fg i16, bg i16) int |
| 55 | fn C.v_ncurses_color_pair(pair int) int |
| 56 | fn C.v_ncurses_attron(attr int) int |
| 57 | fn C.v_ncurses_attroff(attr int) int |
| 58 | fn C.v_ncurses_wattron(win Window, attr int) int |
| 59 | fn C.v_ncurses_wattroff(win Window, attr int) int |
| 60 | fn C.v_ncurses_key_f(n int) int |
| 61 | } |
| 62 | |
| 63 | $if windows { |
| 64 | pub const ok = 0 |
| 65 | pub const err = -1 |
| 66 | pub const key_code_yes = 0 |
| 67 | pub const key_down = 0 |
| 68 | pub const key_up = 0 |
| 69 | pub const key_left = 0 |
| 70 | pub const key_right = 0 |
| 71 | pub const key_home = 0 |
| 72 | pub const key_end = 0 |
| 73 | pub const key_npage = 0 |
| 74 | pub const key_ppage = 0 |
| 75 | pub const key_ic = 0 |
| 76 | pub const key_dc = 0 |
| 77 | pub const key_backspace = 0 |
| 78 | pub const key_enter = 0 |
| 79 | pub const cursor_invisible = 0 |
| 80 | pub const cursor_normal = 1 |
| 81 | pub const cursor_visible = 2 |
| 82 | pub const color_black = 0 |
| 83 | pub const color_red = 1 |
| 84 | pub const color_green = 2 |
| 85 | pub const color_yellow = 3 |
| 86 | pub const color_blue = 4 |
| 87 | pub const color_magenta = 5 |
| 88 | pub const color_cyan = 6 |
| 89 | pub const color_white = 7 |
| 90 | pub const a_normal = 0 |
| 91 | pub const a_standout = 0 |
| 92 | pub const a_underline = 0 |
| 93 | pub const a_reverse = 0 |
| 94 | pub const a_blink = 0 |
| 95 | pub const a_dim = 0 |
| 96 | pub const a_bold = 0 |
| 97 | } $else { |
| 98 | pub const ok = int(C.OK) |
| 99 | pub const err = int(C.ERR) |
| 100 | pub const key_code_yes = int(C.KEY_CODE_YES) |
| 101 | pub const key_down = int(C.KEY_DOWN) |
| 102 | pub const key_up = int(C.KEY_UP) |
| 103 | pub const key_left = int(C.KEY_LEFT) |
| 104 | pub const key_right = int(C.KEY_RIGHT) |
| 105 | pub const key_home = int(C.KEY_HOME) |
| 106 | pub const key_end = int(C.KEY_END) |
| 107 | pub const key_npage = int(C.KEY_NPAGE) |
| 108 | pub const key_ppage = int(C.KEY_PPAGE) |
| 109 | pub const key_ic = int(C.KEY_IC) |
| 110 | pub const key_dc = int(C.KEY_DC) |
| 111 | pub const key_backspace = int(C.KEY_BACKSPACE) |
| 112 | pub const key_enter = int(C.KEY_ENTER) |
| 113 | pub const cursor_invisible = 0 |
| 114 | pub const cursor_normal = 1 |
| 115 | pub const cursor_visible = 2 |
| 116 | pub const color_black = int(C.COLOR_BLACK) |
| 117 | pub const color_red = int(C.COLOR_RED) |
| 118 | pub const color_green = int(C.COLOR_GREEN) |
| 119 | pub const color_yellow = int(C.COLOR_YELLOW) |
| 120 | pub const color_blue = int(C.COLOR_BLUE) |
| 121 | pub const color_magenta = int(C.COLOR_MAGENTA) |
| 122 | pub const color_cyan = int(C.COLOR_CYAN) |
| 123 | pub const color_white = int(C.COLOR_WHITE) |
| 124 | pub const a_normal = int(C.A_NORMAL) |
| 125 | pub const a_standout = int(C.A_STANDOUT) |
| 126 | pub const a_underline = int(C.A_UNDERLINE) |
| 127 | pub const a_reverse = int(C.A_REVERSE) |
| 128 | pub const a_blink = int(C.A_BLINK) |
| 129 | pub const a_dim = int(C.A_DIM) |
| 130 | pub const a_bold = int(C.A_BOLD) |
| 131 | } |
| 132 | |
| 133 | fn unsupported_panic() { |
| 134 | panic('ncurses is not supported on ${@OS}') |
| 135 | } |
| 136 | |
| 137 | @[inline] |
| 138 | fn bool_to_c(enabled bool) int { |
| 139 | return if enabled { 1 } else { 0 } |
| 140 | } |
| 141 | |
| 142 | // is_supported reports whether this target has an ncurses backend in the standard library. |
| 143 | pub fn is_supported() bool { |
| 144 | $if windows { |
| 145 | return false |
| 146 | } $else { |
| 147 | return true |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // initscr initializes the curses screen and returns the default window handle. |
| 152 | pub fn initscr() Window { |
| 153 | $if windows { |
| 154 | unsupported_panic() |
| 155 | return unsafe { nil } |
| 156 | } $else { |
| 157 | return C.v_ncurses_initscr() |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | // stdscr returns the current default screen window handle. |
| 162 | pub fn stdscr() Window { |
| 163 | $if windows { |
| 164 | unsupported_panic() |
| 165 | return unsafe { nil } |
| 166 | } $else { |
| 167 | return C.v_ncurses_stdscr() |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | // endwin restores the terminal after `initscr`. |
| 172 | pub fn endwin() int { |
| 173 | $if windows { |
| 174 | unsupported_panic() |
| 175 | return err |
| 176 | } $else { |
| 177 | return C.v_ncurses_endwin() |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | // cbreak disables line buffering while keeping signal keys active. |
| 182 | pub fn cbreak() int { |
| 183 | $if windows { |
| 184 | unsupported_panic() |
| 185 | return err |
| 186 | } $else { |
| 187 | return C.v_ncurses_cbreak() |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // nocbreak restores normal line buffering after `cbreak`. |
| 192 | pub fn nocbreak() int { |
| 193 | $if windows { |
| 194 | unsupported_panic() |
| 195 | return err |
| 196 | } $else { |
| 197 | return C.v_ncurses_nocbreak() |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | // raw enables raw input mode. |
| 202 | pub fn raw() int { |
| 203 | $if windows { |
| 204 | unsupported_panic() |
| 205 | return err |
| 206 | } $else { |
| 207 | return C.v_ncurses_raw() |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // noraw disables raw input mode. |
| 212 | pub fn noraw() int { |
| 213 | $if windows { |
| 214 | unsupported_panic() |
| 215 | return err |
| 216 | } $else { |
| 217 | return C.v_ncurses_noraw() |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | // echo enables terminal echo for characters typed by the user. |
| 222 | pub fn echo() int { |
| 223 | $if windows { |
| 224 | unsupported_panic() |
| 225 | return err |
| 226 | } $else { |
| 227 | return C.v_ncurses_echo() |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // noecho disables terminal echo for characters typed by the user. |
| 232 | pub fn noecho() int { |
| 233 | $if windows { |
| 234 | unsupported_panic() |
| 235 | return err |
| 236 | } $else { |
| 237 | return C.v_ncurses_noecho() |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // keypad enables or disables keypad processing for a window. |
| 242 | pub fn keypad(win Window, enabled bool) int { |
| 243 | $if windows { |
| 244 | _ = win |
| 245 | _ = enabled |
| 246 | unsupported_panic() |
| 247 | return err |
| 248 | } $else { |
| 249 | return C.v_ncurses_keypad(win, bool_to_c(enabled)) |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | // nodelay enables or disables nonblocking reads for a window. |
| 254 | pub fn nodelay(win Window, enabled bool) int { |
| 255 | $if windows { |
| 256 | _ = win |
| 257 | _ = enabled |
| 258 | unsupported_panic() |
| 259 | return err |
| 260 | } $else { |
| 261 | return C.v_ncurses_nodelay(win, bool_to_c(enabled)) |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // timeout sets the blocking behavior for `getch` on the default screen. |
| 266 | pub fn timeout(delay int) { |
| 267 | $if windows { |
| 268 | _ = delay |
| 269 | unsupported_panic() |
| 270 | } $else { |
| 271 | C.v_ncurses_timeout(delay) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // wtimeout sets the blocking behavior for `wgetch` on a specific window. |
| 276 | pub fn wtimeout(win Window, delay int) { |
| 277 | $if windows { |
| 278 | _ = win |
| 279 | _ = delay |
| 280 | unsupported_panic() |
| 281 | } $else { |
| 282 | C.v_ncurses_wtimeout(win, delay) |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // curs_set changes cursor visibility using the module cursor constants. |
| 287 | pub fn curs_set(visibility int) int { |
| 288 | $if windows { |
| 289 | _ = visibility |
| 290 | unsupported_panic() |
| 291 | return err |
| 292 | } $else { |
| 293 | return C.v_ncurses_curs_set(visibility) |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // clear clears the default screen buffer. |
| 298 | pub fn clear() int { |
| 299 | $if windows { |
| 300 | unsupported_panic() |
| 301 | return err |
| 302 | } $else { |
| 303 | return C.v_ncurses_clear() |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | // refresh flushes pending changes for the default screen. |
| 308 | pub fn refresh() int { |
| 309 | $if windows { |
| 310 | unsupported_panic() |
| 311 | return err |
| 312 | } $else { |
| 313 | return C.v_ncurses_refresh() |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // getch reads one key code from the default screen. |
| 318 | pub fn getch() int { |
| 319 | $if windows { |
| 320 | unsupported_panic() |
| 321 | return err |
| 322 | } $else { |
| 323 | return C.v_ncurses_getch() |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | // addstr writes a string to the default screen at the current cursor position. |
| 328 | pub fn addstr(text string) int { |
| 329 | $if windows { |
| 330 | _ = text |
| 331 | unsupported_panic() |
| 332 | return err |
| 333 | } $else { |
| 334 | return C.v_ncurses_addstr(&char(text.str)) |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // mvaddstr moves the cursor on the default screen and writes a string there. |
| 339 | pub fn mvaddstr(y int, x int, text string) int { |
| 340 | $if windows { |
| 341 | _ = y |
| 342 | _ = x |
| 343 | _ = text |
| 344 | unsupported_panic() |
| 345 | return err |
| 346 | } $else { |
| 347 | return C.v_ncurses_mvaddstr(y, x, &char(text.str)) |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | // newwin creates a new curses window. |
| 352 | pub fn newwin(lines int, cols int, begin_y int, begin_x int) Window { |
| 353 | $if windows { |
| 354 | _ = lines |
| 355 | _ = cols |
| 356 | _ = begin_y |
| 357 | _ = begin_x |
| 358 | unsupported_panic() |
| 359 | return unsafe { nil } |
| 360 | } $else { |
| 361 | return C.v_ncurses_newwin(lines, cols, begin_y, begin_x) |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | // delwin destroys a window created by `newwin`. |
| 366 | pub fn delwin(win Window) int { |
| 367 | $if windows { |
| 368 | _ = win |
| 369 | unsupported_panic() |
| 370 | return err |
| 371 | } $else { |
| 372 | return C.v_ncurses_delwin(win) |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | // box draws a border around a window. Pass `0` for the default border characters. |
| 377 | pub fn box(win Window, vertical int, horizontal int) int { |
| 378 | $if windows { |
| 379 | _ = win |
| 380 | _ = vertical |
| 381 | _ = horizontal |
| 382 | unsupported_panic() |
| 383 | return err |
| 384 | } $else { |
| 385 | return C.v_ncurses_box(win, u32(vertical), u32(horizontal)) |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | // getmaxx returns the width of a window in columns. |
| 390 | pub fn getmaxx(win Window) int { |
| 391 | $if windows { |
| 392 | _ = win |
| 393 | unsupported_panic() |
| 394 | return 0 |
| 395 | } $else { |
| 396 | return C.v_ncurses_getmaxx(win) |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | // getmaxy returns the height of a window in rows. |
| 401 | pub fn getmaxy(win Window) int { |
| 402 | $if windows { |
| 403 | _ = win |
| 404 | unsupported_panic() |
| 405 | return 0 |
| 406 | } $else { |
| 407 | return C.v_ncurses_getmaxy(win) |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | // wrefresh flushes pending changes for a specific window. |
| 412 | pub fn wrefresh(win Window) int { |
| 413 | $if windows { |
| 414 | _ = win |
| 415 | unsupported_panic() |
| 416 | return err |
| 417 | } $else { |
| 418 | return C.v_ncurses_wrefresh(win) |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // wclear clears a specific window. |
| 423 | pub fn wclear(win Window) int { |
| 424 | $if windows { |
| 425 | _ = win |
| 426 | unsupported_panic() |
| 427 | return err |
| 428 | } $else { |
| 429 | return C.v_ncurses_wclear(win) |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | // wgetch reads one key code from a specific window. |
| 434 | pub fn wgetch(win Window) int { |
| 435 | $if windows { |
| 436 | _ = win |
| 437 | unsupported_panic() |
| 438 | return err |
| 439 | } $else { |
| 440 | return C.v_ncurses_wgetch(win) |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // wmove moves the cursor inside a specific window. |
| 445 | pub fn wmove(win Window, y int, x int) int { |
| 446 | $if windows { |
| 447 | _ = win |
| 448 | _ = y |
| 449 | _ = x |
| 450 | unsupported_panic() |
| 451 | return err |
| 452 | } $else { |
| 453 | return C.v_ncurses_wmove(win, y, x) |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // waddstr writes a string to a specific window at its current cursor position. |
| 458 | pub fn waddstr(win Window, text string) int { |
| 459 | $if windows { |
| 460 | _ = win |
| 461 | _ = text |
| 462 | unsupported_panic() |
| 463 | return err |
| 464 | } $else { |
| 465 | return C.v_ncurses_waddstr(win, &char(text.str)) |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | // mvwaddstr moves the cursor in a window and writes a string there. |
| 470 | pub fn mvwaddstr(win Window, y int, x int, text string) int { |
| 471 | $if windows { |
| 472 | _ = win |
| 473 | _ = y |
| 474 | _ = x |
| 475 | _ = text |
| 476 | unsupported_panic() |
| 477 | return err |
| 478 | } $else { |
| 479 | return C.v_ncurses_mvwaddstr(win, y, x, &char(text.str)) |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | // start_color enables ncurses color support. |
| 484 | pub fn start_color() int { |
| 485 | $if windows { |
| 486 | unsupported_panic() |
| 487 | return err |
| 488 | } $else { |
| 489 | return C.v_ncurses_start_color() |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | // has_colors reports whether the terminal supports colors. |
| 494 | pub fn has_colors() bool { |
| 495 | $if windows { |
| 496 | return false |
| 497 | } $else { |
| 498 | return C.v_ncurses_has_colors() != 0 |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | // init_pair defines a foreground/background color pair for use with `color_pair`. |
| 503 | pub fn init_pair(pair int, fg int, bg int) int { |
| 504 | $if windows { |
| 505 | _ = pair |
| 506 | _ = fg |
| 507 | _ = bg |
| 508 | unsupported_panic() |
| 509 | return err |
| 510 | } $else { |
| 511 | return C.v_ncurses_init_pair(i16(pair), i16(fg), i16(bg)) |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | // color_pair returns the attribute mask for a color pair number. |
| 516 | pub fn color_pair(pair int) int { |
| 517 | $if windows { |
| 518 | _ = pair |
| 519 | unsupported_panic() |
| 520 | return 0 |
| 521 | } $else { |
| 522 | return C.v_ncurses_color_pair(pair) |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | // attron enables an attribute on the default screen. |
| 527 | pub fn attron(attr int) int { |
| 528 | $if windows { |
| 529 | _ = attr |
| 530 | unsupported_panic() |
| 531 | return err |
| 532 | } $else { |
| 533 | return C.v_ncurses_attron(attr) |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | // attroff disables an attribute on the default screen. |
| 538 | pub fn attroff(attr int) int { |
| 539 | $if windows { |
| 540 | _ = attr |
| 541 | unsupported_panic() |
| 542 | return err |
| 543 | } $else { |
| 544 | return C.v_ncurses_attroff(attr) |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // wattron enables an attribute on a specific window. |
| 549 | pub fn wattron(win Window, attr int) int { |
| 550 | $if windows { |
| 551 | _ = win |
| 552 | _ = attr |
| 553 | unsupported_panic() |
| 554 | return err |
| 555 | } $else { |
| 556 | return C.v_ncurses_wattron(win, attr) |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | // wattroff disables an attribute on a specific window. |
| 561 | pub fn wattroff(win Window, attr int) int { |
| 562 | $if windows { |
| 563 | _ = win |
| 564 | _ = attr |
| 565 | unsupported_panic() |
| 566 | return err |
| 567 | } $else { |
| 568 | return C.v_ncurses_wattroff(win, attr) |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | // key_f returns the key code for function keys like F1, F2, and so on. |
| 573 | pub fn key_f(n int) int { |
| 574 | $if windows { |
| 575 | _ = n |
| 576 | unsupported_panic() |
| 577 | return 0 |
| 578 | } $else { |
| 579 | return C.v_ncurses_key_f(n) |
| 580 | } |
| 581 | } |
| 582 | |