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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#[cfg(any(test, doc))]
use crate::*;
#[cfg(not(any(test, doc)))]
use libnotcurses_sys::*;
struct State<'nc, 'p> {
nc: &'nc mut Nc,
showcase: &'p mut NcPlane,
info: &'p mut NcPlane,
cursor: &'p mut NcPlane,
}
impl<'nc, 'p> State<'nc, 'p> {
fn exit(&mut self, exit_code: i32) -> NcResult<()> {
self.showcase.destroy()?;
self.info.destroy()?;
self.cursor.destroy()?;
unsafe { self.nc.stop()? };
std::process::exit(exit_code);
}
}
impl<'nc, 'p> Drop for State<'nc, 'p> {
fn drop(&mut self) {
let _ = self.exit(0);
}
}
static LOREM_IPSUM: &str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat";
#[rustfmt::skip]
fn main() -> NcResult<()> {
let mut _nc = unsafe { Nc::new()? };
let stp = unsafe {_nc.stdplane() };
let mut _showcase = NcPlane::new_child_sized(stp, 1, 10, 10, 20)?;
let mut _info = NcPlane::new_child_sized(stp, 12, 1, 3, 54)?;
let mut _cursor = NcPlane::new_child_sized(stp, 12, 1, 1, 1)?;
let mut state = State {
nc: _nc,
showcase: _showcase,
info: _info,
cursor: _cursor,
};
state.showcase.set_base("▒", 0, NcChannels::from_rgb(0x662222, 0x557755))?;
state.showcase.set_channels(NcChannels::from_rgb(0x222222, 0x449944));
state.info.set_base(" ", 0, NcChannels::from_rgb(0x222222, 0xAAAAAA))?;
state.cursor.set_base("█", 0, NcChannels::from_rgb(0x662222, 0x557755))?;
state.showcase.set_scrolling(true);
state.info.set_scrolling(true);
state.showcase.putstr(&LOREM_IPSUM.repeat(2))?;
state.info.putstrln(" Showcase on a 10x20 plane the results of calling")?;
state.info.putstrln("`NcPlane.erase_region()` with different arguments.")?;
print_info_row(
&mut state,
2,
Some(NcChannels::from_rgb(0x113355, 0x8899CC)),
NcAlign::Center,
"(press 'q' to quit, or any other key to continue)"
)?;
render_and_wait_input(&mut state)?;
erase_region_slideshow(&mut state)?;
state.showcase.erase();
state.info.erase();
print_info_row(&mut state, 1, None, NcAlign::Center, "that's all, folks! ")?;
state
.showcase
.set_base(" ", 0, NcChannels::from_rgb(0x224411, 0x992222))?;
render_and_wait_input(&mut state)?;
state.exit(0)?;
Ok(())
}
#[rustfmt::skip]
fn erase_region_slideshow(state: &mut State) -> NcResult<()> {
let cursor = (5, 10);
erase_region(state, cursor, None, None, 0, 0, "everything (if cursor is in a legal position)")?;
erase_region(state, cursor, Some(0), Some(0), 0, 0, "everything in all cases")?;
erase_region(state, cursor, None, None, 1, 1, "current cursor position")?;
erase_region(state, cursor, None, None, -1, -1, "current cursor position")?;
erase_region(state, cursor, None, None, 1, 0, "the current row")?;
erase_region(state, cursor, None, None, 2, 0, "the current row and the one after")?;
erase_region(state, cursor, None, None, i32::MAX, 0, "all rows with or below the cursor")?;
erase_region(state, cursor, None, None, -1, 0, "the current row")?;
erase_region(state, cursor, None, None, -2, 0, "the current row and the one before")?;
erase_region(state, cursor, None, None, i32::MIN, 0, "all rows with or above the cursor")?;
erase_region(state, cursor, Some(0), None, 3, 0, "rows 1, 2 and 3")?;
erase_region(state, cursor, Some(4), None, 3, 0, "rows 4, 5 and 6")?;
erase_region(state, cursor, Some(7), None, 3, 0, "rows 8, 9 and 10")?;
erase_region(state, cursor, None, None, 0, 1, "the current column")?;
erase_region(state, cursor, None, None, 0, 3, "the current column and the two after")?;
erase_region(state, cursor, None, None, 0, i32::MAX, "all columns with or after the cursor")?;
erase_region(state, cursor, None, None, 0, -1, "the current column")?;
erase_region(state, cursor, None, None, 0, -3, "the current column and the two before")?;
erase_region(state, cursor, None, None, 0, i32::MIN, "all columns with or before the cursor")?;
erase_region(state, cursor, None, Some(0), 0, 3, "columns 1, 2 and 3")?;
erase_region(state, cursor, None, Some(4), 0, 3, "columns 5, 6 and 7")?;
erase_region(state, cursor, None, Some(15), 0, 3, "columns 16, 17 and 18")?;
erase_region(state, cursor, None, None, 5, 5, "a 5×5 area down-right from current position")?;
erase_region(state, cursor, None, None, -5, -5, "a 5×5 area up-left from current position")?;
erase_region(state, cursor, None, None, -5, 5, "a 5×5 area up-right from current position")?;
erase_region(state, cursor, None, None, 5, -5, "a 5×5 area down-left from current position")?;
erase_region(state, cursor, None, Some(4), 3, 4, "a 3×4 area down-right from (5, 4) to (7, 7)")?;
erase_region(state, cursor, None, Some(4), -3, -4, "a 3×4 area up-left from (5, 4) to (3, 1)")?;
erase_region(state, cursor, None, Some(4), -3, 4, "a 3×4 area up-right from (5, 4) to (7, 7)")?;
erase_region(state, cursor, None, Some(4), 3, -4, "a 3×4 area down-left from (5, 4) to (7, 1)")?;
Ok(())
}
fn erase_region(
state: &mut State,
cursor: (u32, u32),
ystart: Option<u32>,
xstart: Option<u32>,
ylen: i32,
xlen: i32,
what_is_erased: &str,
) -> NcResult<()> {
state.showcase.erase();
state.showcase.putstr(&LOREM_IPSUM.repeat(2))?;
state.showcase.cursor_move_yx(cursor.0, cursor.1)?;
show_cursor(state)?;
state.info.erase();
print_info_row(
state,
0,
Some(NcChannels::from_rgb(0x555555, 0x999999)),
NcAlign::Right,
&format!["cursor_yx({}, {})", cursor.0, cursor.1],
)?;
let ylen_str = match ylen {
i32::MAX => "i32::MAX".into(),
i32::MIN => "i32::MIN".into(),
_ => ylen.to_string(),
};
let xlen_str = match xlen {
i32::MAX => "i32::MAX".into(),
i32::MIN => "i32::MIN".into(),
_ => xlen.to_string(),
};
print_info_row(
state,
1,
None,
NcAlign::Center,
&format![
"erase_region({:?}, {:?}, {}, {})",
ystart, xstart, ylen_str, xlen_str
],
)?;
print_info_row(
state,
2,
Some(NcChannels::from_rgb(0x555577, 0xAAAABB)),
NcAlign::Center,
&format!["erases {}", what_is_erased],
)?;
state.showcase.erase_region(ystart, xstart, ylen, xlen)?;
render_and_wait_input(state)?;
Ok(())
}
fn render_and_wait_input(state: &mut State) -> NcResult<()> {
state.nc.render()?;
let res = state.nc.get_blocking(None)?;
if res == NcReceived::Char('q') {
state.exit(0)?;
}
Ok(())
}
fn show_cursor(state: &mut State) -> NcResult<()> {
let (sy, sx) = state.showcase.yx();
let (cy, cx) = state.showcase.cursor_yx();
state.cursor.move_yx(sy + cy as i32, sx + cx as i32)?;
Ok(())
}
fn print_info_row(
state: &mut State,
row: u32,
stain: Option<NcChannels>,
align: NcAlign,
string: &str,
) -> NcResult<()> {
let info_len = state.info.dim_x();
if let Some(channels) = stain {
state.info.cursor_move_yx(row, 0)?;
state.info.putstr(&" ".repeat(info_len as usize))?;
state.info.cursor_move_yx(row, 0)?;
state.info.stain(
Some(row),
Some(0),
Some(1),
None,
channels,
channels,
channels,
channels,
)?;
}
state.info.putstr_aligned_stained(row, align, string)?;
Ok(())
}