~toastal/klossi
~toastal/klossi/lib/ncurses.dats
~toastal/klossi/lib/ncurses.dats
0 | // SPDX-FileCopyrightText: 2024 toastal <toastal@posteo.net> |
1 | // SPDX-License-Identifier: MPL-2.0 |
2 |
|
3 | #include "share/atspre_staload.hats" |
4 | #include "share/atspre_staload_libats_ML.hats" |
5 |
|
6 | %{^ |
7 | #include <ncurses.h> |
8 | %} |
9 |
|
10 | staload "lib/ncurses.sats" |
11 |
|
12 | (* curs_getch ****************************************************************) |
13 | extern fn _getch : () -> int = "mac#getch" |
14 |
|
15 | implement getch (): int = _getch() |
16 |
|
17 |
|
18 | (* curs_initscr **************************************************************) |
19 | exception EndWinFailed of () |
20 |
|
21 | extern fn _initscr : () -> ptr = "mac#initscr" |
22 | extern fn _endwin : () -> int = "mac#endwin" |
23 |
|
24 | implement initscr(): window = let |
25 | val window = WINDOW (_) |
26 | val WINDOW (w) = window |
27 | val () = w := _initscr() |
28 | prval () = fold@ (window) |
29 | in |
30 | window |
31 | end |
32 |
|
33 | implement endwin (win): void = let |
34 | val ~WINDOW _ = win |
35 | in |
36 | if _endwin() < 0 then $raise EndWinFailed() |
37 | end |
38 |
|
39 | (* curs_inopts ***************************************************************) |
40 | exception CBreakFailed of bool |
41 | exception EchoFailed of bool |
42 |
|
43 | extern fn _cbreak : () -> int = "mac#cbreak" |
44 | extern fn _nocbreak : () -> int = "mac#nocbreak" |
45 | extern fn _echo : () -> int = "mac#echo" |
46 | extern fn _noecho : () -> int = "mac#noecho" |
47 |
|
48 | implement cbreak(): void = |
49 | if _cbreak() < 0 then $raise CBreakFailed(true) |
50 |
|
51 | implement nocbreak(): void = |
52 | if _nocbreak() < 0 then $raise CBreakFailed(false) |
53 |
|
54 | implement echo(): void = |
55 | if _echo() < 0 then $raise EchoFailed(true) |
56 |
|
57 | implement noecho(): void = |
58 | if _noecho() < 0 then $raise EchoFailed(false) |
59 |
|
60 |
|
61 | (* curs_printw ***************************************************************) |
62 | exception FormattingError of string |
63 |
|
64 | extern fn _printw : (string, string) -> int = "mac#printw" |
65 |
|
66 | implement printw (str: string): void = |
67 | if _printw("%s", str) < 0 then $raise FormattingError(str) |
68 |
|
69 |
|
70 | (* curs_refresh **************************************************************) |
71 | exception RefreshFailed of () |
72 |
|
73 | extern fn _refresh : () -> int = "mac#refresh" |
74 |
|
75 |
|
76 |
|
77 | implement refresh (): void = |
78 | if _refresh() < 0 then $raise RefreshFailed() |