This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.d
67 lines (60 loc) · 2.09 KB
/
util.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* misc. utility functions */
module util;
import std.conv : to;
import std.file : remove;
import cs = deimos.curses;
import box : Box;
public alias stdscr = cs.stdscr;
public alias ColourPair = cs.COLOR_PAIR;
/*
* Check the screen's dimensions and display a white-on-red
* message if it isn't the right size.
*
* mincols is the minimum amount of columns, minrows is the
* minimum amount of rows, dumpfile is the name of the file
* to dump the screen state to, and dumped is set to true if
* the screen state has been dumped, or false otherwise.
*/
void checkDimensions(in ushort mincols, in ushort minrows,
in char *dumpfile, ref bool dumped)
{
int rows, cols;
cs.getmaxyx(cs.stdscr, rows, cols); /* get terminal size */
/* if the screen state has not been dumped
* AND the terminal is too small: */
if (!dumped && (rows < minrows || cols < mincols)) {
cs.scr_dump(dumpfile); /* dump screen to file */
dumped = true;
cs.attron(ColourPair(1)); /* enable white-on-red palette */
cs.mvprintw(0, 0, "terminal size too small (%d x %d)", cols, rows);
cs.mvprintw(1, 0, "must be at least %d columns by %d rows",
mincols, minrows);
cs.attroff(ColourPair(1)); /* disable palette */
cs.refresh();
/* else, if the screen state HAS been dumped
AND the temrinal is still too small */
} else if (dumped && (rows < minrows || cols < mincols)) {
/* print the message again */
cs.attron(ColourPair(1)); /* enable white-on-red palette */
cs.mvprintw(0, 0, "terminal size too small (%d x %d)", cols, rows);
cs.mvprintw(1, 0, "must be at least %d columns by %d rows",
mincols, minrows);
cs.attroff(ColourPair(1)); /* disable palette */
cs.refresh();
/* else, if the screen state HAS been dumped
AND the terminal is a good size */
} else if (dumped && (rows >= minrows || cols >= mincols)) {
/* restore screen state */
cs.scr_restore(dumpfile); /* restore screen from file */
cs.use_default_colors();
cs.refresh();
remove(dumpfile.to!string()); /* remove the dump file */
dumped = false;
}
}
/* destruct ncurses */
void endCurses() nothrow @nogc
{
cs.refresh();
cs.endwin();
}