ncursesw-win 0.6.3

An extension wrapper around the ncursesw-rs crate that encapsulates the exposed raw pointers of the core NCurses TUI library
Documentation
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/*
    src/screen.rs

    Copyright (c) 2020-2022 Stephen Whittle  All rights reserved.

    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the "Software"),
    to deal in the Software without restriction, including without limitation
    the rights to use, copy, modify, merge, publish, distribute, sublicense,
    and/or sell copies of the Software, and to permit persons to whom
    the Software is furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included
    in all copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
    THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
    IN THE SOFTWARE.
*/

#![allow(deprecated)]

use std::{
    ptr, fmt, time, hash::{Hash, Hasher}, convert::{TryFrom, TryInto},
    path::Path, os::unix::io::AsRawFd, io::{Write, Read}
};
use ncursesw::{SCREEN, panels, mouse};
use crate::{
    ColorType, ColorsType, ColorAttributeTypes, HasHandle, NCurseswWinError,
    ChtypeChar, WideChar, ComplexChar, Panel, InputMode, CursorType, KeyBinding,
    Window, Size, Origin, Legacy
};

pub struct Screen {
    handle:       SCREEN, // pointer to NCurses screen internal structure
    free_on_drop: bool    // free SCREEN handle on drop of structure
}

impl Screen {
    pub(in crate) fn _from(handle: SCREEN, free_on_drop: bool) -> Self {
        assert!(!handle.is_null(), "Screen::_from() : handle.is_null()");

        Self { handle, free_on_drop }
    }

    pub(in crate) fn _handle(&self) -> SCREEN {
        self.handle
    }
}

impl Screen {
    pub fn new<S, O, I>(term: Option<S>, output: &O, input: &I) -> result!(Self)
        where S: Into<String>,
              O: AsRawFd + Write,
              I: AsRawFd + Read
    {
        Ok(Screen::_from(ncursesw::newterm(term, output, input)?, true))
    }

    #[deprecated(since = "0.5.0", note = "Use Screen::new() instead")]
    pub fn newterm<S, O, I>(term: Option<S>, output: &O, input: &I) -> result!(Self)
        where S: Into<String>,
              O: AsRawFd + Write,
              I: AsRawFd + Read
    {
        Self::new(term, output, input)
    }

    /// # Safety
    /// 
    /// convert a 'SCREEN' pointer from the underlying 'ncursesw' crate
    /// to a 'ncurseswwin::Screen'.
    pub unsafe fn from_ptr(screen: SCREEN) -> Self {
        Self::_from(screen, false)
    }

    /// # Safety
    /// 
    /// Return's the 'Screen' as a pointer so we can use some of underlying 'ncursesw'
    /// crates functions, for example 'ncursesw::extend::ColorPair::new_sp()' (as imported
    /// in this crate as 'crate::extend::ColorPair::new_sp()')
    pub unsafe fn as_ptr(&self) -> SCREEN {
        self.handle
    }

    /// Set the input mode to use within NCurses on this Screen.
    ///
    /// The terminal gets input from the user. Then it's sometimes buffered up. At
    /// some point it's passed into the program's input buffer.
    ///
    /// - Character: Input is passed in 1 character at a time, but special
    ///   characters (such as Ctrl+C and Ctrl+S) are automatically processed for
    ///   you by the terminal.
    /// - Cooked: Input is passed in 1 line at a time, with the special character
    ///   processing mentioned above enabled.
    /// - RawCharacter: Input is passed in 1 character at a time, and special
    ///   character sequences are not processed automatically.
    /// - RawCooked: Input is passed in 1 line at a time, and special
    ///   character sequences are not processed automatically.
    ///
    /// The default mode is inherited from the terminal that started the program
    /// (usually Cooked), so you should _always_ set the desired mode explicitly
    /// at the start of your program.
    pub fn set_input_mode(&self, mode: InputMode) -> result!(()) {
        match match mode {
            InputMode::Character    => ncursesw::cbreak_sp(self.handle),
            InputMode::Cooked       => ncursesw::nocbreak_sp(self.handle),
            InputMode::RawCharacter => ncursesw::raw_sp(self.handle),
            InputMode::RawCooked    => ncursesw::noraw_sp(self.handle)
        } {
            Err(source) => Err(NCurseswWinError::NCurseswError { source }),
            Ok(_)       => Ok(())
        }
    }

    /// Set echo on or off within NCurses on this Screen.
    ///
    /// Enables or disables the automatic echoing of input into the window as
    /// the user types. Default to on, but you probably want it to be off most
    /// of the time.
    pub fn set_echo(&self, flag: bool) -> result!(()) {
        match if flag {
            ncursesw::echo_sp(self.handle)
        } else {
            ncursesw::noecho_sp(self.handle)
        } {
            Err(source) => Err(NCurseswWinError::NCurseswError { source }),
            Ok(_)       => Ok(())
        }
    }

    pub fn set_filter(&self, flag: bool) {
        if flag {
            ncursesw::filter_sp(self.handle)
        } else {
            ncursesw::nofilter_sp(self.handle)
        }
    }

    /// Control whether NCurses translates the return key into newline on input on this Screen.
    ///
    /// This determines wether ncurses translates newline into return and line-feed on output (in either
    /// case, the call addch('\n') does the equivalent of return and line feed on the virtual screen).
    /// Initially, these translations do occur. If you disable then ncurses will be able to make
    /// better use of the line-feed capability, resulting in faster cursor motion.
    /// Also, ncurses will then be able to detect the return key.
    pub fn set_newline(&self, flag: bool) -> result!(()) {
        match if flag {
            ncursesw::nl_sp(self.handle)
        } else {
            ncursesw::nonl_sp(self.handle)
        } {
            Err(source) => Err(NCurseswWinError::NCurseswError { source }),
            Ok(_)       => Ok(())
        }
    }

    pub fn set_qiflush(&self, flag: bool) {
        if flag {
            ncursesw::qiflush_sp(self.handle)
        } else {
            ncursesw::noqiflush_sp(self.handle)
        }
    }

    pub fn assume_default_colors<S, C, T>(&self, colors: S) -> result!(())
        where S: ColorsType<C, T>,
              C: ColorType<T>,
              T: ColorAttributeTypes
    {
        Ok(ncursesw::assume_default_colors_sp(self.handle, colors)?)
    }

    pub fn baudrate(&self) -> result!(u32) {
        Ok(u32::try_from(ncursesw::baudrate_sp(self.handle))?)
    }

    pub fn beep(&self) -> result!(()) {
        Ok(ncursesw::beep_sp(self.handle)?)
    }

    pub fn can_change_color(&self) -> bool {
        ncursesw::can_change_color_sp(self.handle)
    }

    pub fn cursor_set(&self, cursor: CursorType) -> result!(CursorType) {
        Ok(ncursesw::curs_set_sp(self.handle, cursor)?)
    }

    #[deprecated(since = "0.6.0", note = "Use Screen::cursor_set() instead")]
    pub fn curs_set(&self, cursor: CursorType) -> result!(CursorType) {
        self.cursor_set(cursor)
    }

    pub fn define_key(&self, definition: Option<&str>, keycode: KeyBinding) -> result!(()) {
        Ok(ncursesw::define_key_sp(self.handle, definition, keycode)?)
    }

    pub fn def_prog_mode(&self) -> result!(()) {
        Ok(ncursesw::def_prog_mode_sp(self.handle)?)
    }

    pub fn def_shell_mode(&self) -> result!(()) {
        Ok(ncursesw::def_shell_mode_sp(self.handle)?)
    }

    pub fn delay_output(&self, ms: time::Duration) -> result!(()) {
        Ok(ncursesw::delay_output_sp(self.handle, ms)?)
    }

    pub fn doupdate(&self) -> result!(()) {
        Ok(ncursesw::doupdate_sp(self.handle)?)
    }

    pub fn erasechar(&self) -> result!(char) {
        Ok(ncursesw::erasechar_sp(self.handle)?)
    }

    pub fn flash(&self) -> result!(()) {
        Ok(ncursesw::flash_sp(self.handle)?)
    }

    pub fn flushinp(&self) -> result!(()) {
        Ok(ncursesw::flushinp_sp(self.handle)?)
    }

    pub fn get_escdelay(&self) -> result!(time::Duration) {
        Ok(ncursesw::get_escdelay_sp(self.handle)?)
    }

    pub fn getwin<I: AsRawFd + Read>(&self, file: &I) -> result!(Window) {
        Ok(Window::_from(Some(self.handle), ncursesw::getwin_sp(self.handle, file)?, true))
    }

    pub fn halfdelay(&self, tenths: time::Duration) -> result!(()) {
        Ok(ncursesw::halfdelay_sp(self.handle, tenths)?)
    }

    pub fn has_colors(&self) -> bool {
        ncursesw::has_colors_sp(self.handle)
    }

    pub fn has_ic(&self) -> bool {
        ncursesw::has_ic_sp(self.handle)
    }

    pub fn has_il(&self) -> bool {
        ncursesw::has_il_sp(self.handle)
    }

    pub fn has_key(&self, ch: KeyBinding) -> bool {
        ncursesw::has_key_sp(self.handle, ch)
    }

    pub fn has_mouse(&self) -> bool {
        mouse::has_mouse_sp(self.handle)
    }

    pub fn intrflush(&self, flag: bool) -> result!(()) {
        Ok(ncursesw::intrflush_sp(self.handle, flag)?)
    }

    pub fn is_term_resized(&self, size: Size) -> result!(bool) {
        Ok(ncursesw::is_term_resized_sp(self.handle, size.try_into()?))
    }

    pub fn keybound(&self, keycode: KeyBinding, count: i32) -> Option<String> {
        ncursesw::keybound_sp(self.handle, keycode, count)
    }

    pub fn key_defined(&self, definition: &str) -> result!(Option<KeyBinding>) {
        Ok(ncursesw::key_defined_sp(self.handle, definition)?)
    }

    pub fn keyname(&self, c: KeyBinding) -> result!(String) {
        Ok(ncursesw::keyname_sp(self.handle, c)?)
    }

    pub fn keyok(&self, keycode: KeyBinding, enable: bool) -> result!(()) {
        Ok(ncursesw::keyok_sp(self.handle, keycode, enable)?)
    }

    pub fn killchar(&self) -> result!(char) {
        Ok(ncursesw::killchar_sp(self.handle)?)
    }

    pub fn longname(&self) -> result!(String) {
        Ok(ncursesw::longname_sp(self.handle)?)
    }

    pub fn mcprint(&self, data: &[i8], len: i32) -> result!(i32) {
        Ok(ncursesw::mcprint_sp(self.handle, data, len)?)
    }

    pub fn mvcur(&self, old: Origin, new: Origin) -> result!(()) {
        Ok(ncursesw::mvcur_sp(self.handle, old.try_into()?, new.try_into()?)?)
    }

    #[deprecated(since = "0.5.0", note = "ncurses library call superseeded by native rust call. Use std::thread::sleep(dur: std::time::Duration) instead")]
    pub fn napms(&self, ms: time::Duration) -> result!(()) {
        Ok(ncursesw::napms_sp(self.handle, ms)?)
    }

    #[deprecated(since = "0.5.0", note = "use with caution as this routine will reset all color pairs potentially before they go out of scope and the color pairs will default to terminal default foreground and backgound colors.")]
    /// Reset all defined color pairs.
    pub fn reset_color_pairs(&self) {
        ncursesw::reset_color_pairs_sp(self.handle)
    }

    pub fn reset_prog_mode(&self) -> result!(()) {
        Ok(ncursesw::reset_prog_mode_sp(self.handle)?)
    }

    pub fn reset_shell_mode(&self) -> result!(()) {
        Ok(ncursesw::reset_shell_mode_sp(self.handle)?)
    }

    pub fn resetty(&self) -> result!(()) {
        Ok(ncursesw::resetty_sp(self.handle)?)
    }

    pub fn resize_term(&self, size: Size) -> result!(()) {
        Ok(ncursesw::resize_term_sp(self.handle, size.try_into()?)?)
    }

    pub fn resizeterm(&self, size: Size) -> result!(()) {
        Ok(ncursesw::resizeterm_sp(self.handle, size.try_into()?)?)
    }

    pub fn savetty(&self) -> result!(()) {
        Ok(ncursesw::savetty_sp(self.handle)?)
    }

    pub fn scr_init<P: AsRef<Path>>(&self, path: P) -> result!(()) {
        Ok(ncursesw::scr_init_sp(self.handle, path)?)
    }

    pub fn scr_restore<P: AsRef<Path>>(&self, path: P) -> result!(()) {
        Ok(ncursesw::scr_restore_sp(self.handle, path)?)
    }

    pub fn scr_set<P: AsRef<Path>>(&self, path: P) -> result!(()) {
        Ok(ncursesw::scr_set_sp(self.handle, path)?)
    }

    pub fn set_escdelay(&self, ms: time::Duration) -> result!(()) {
        Ok(ncursesw::set_escdelay_sp(self.handle, ms)?)
    }

    pub fn set_tabsize(&self, size: i32) -> result!(()) {
        Ok(ncursesw::set_tabsize_sp(self.handle, size)?)
    }

    pub fn set_term(&self) -> result!(Screen) {
        Ok(Screen::_from(ncursesw::set_term(self.handle)?, false))
    }

    pub fn start_color(&self) -> result!(()) {
        Ok(ncursesw::start_color_sp(self.handle)?)
    }

    pub fn termname(&self) -> result!(String) {
        Ok(ncursesw::termname_sp(self.handle)?)
    }

    pub fn typeahead<FD: AsRawFd + Read>(&self, file: Option<FD>) -> result!(()) {
        Ok(ncursesw::typeahead_sp(self.handle, file)?)
    }

    pub fn unctrl(&self, c: ChtypeChar) -> result!(String) {
        Ok(ncursesw::unctrl_sp(self.handle, c)?)
    }

    pub fn ungetch(&self, ch: char) -> result!(()) {
        Ok(ncursesw::ungetch_sp(self.handle, ch)?)
    }

    pub fn unget_wch(&self, ch: WideChar) -> result!(()) {
        Ok(ncursesw::unget_wch_sp(self.handle, ch)?)
    }

    pub fn use_default_colors(&self) -> result!(()) {
        Ok(ncursesw::use_default_colors_sp(self.handle)?)
    }

    pub fn use_env(&self, flag: bool) {
        ncursesw::use_env_sp(self.handle, flag)
    }

    pub fn use_tioctl(&self, flag: bool) {
        ncursesw::use_tioctl_sp(self.handle, flag)
    }

    pub fn use_legacy_coding(&self, level: Legacy) -> result!(Legacy) {
        Ok(ncursesw::use_legacy_coding_sp(self.handle, level)?)
    }

    pub fn wunctrl(&self, ch: ComplexChar) -> result!(WideChar) {
        Ok(ncursesw::wunctrl_sp(self.handle, ch)?)
    }

    pub fn erasewchar(&self) -> result!(WideChar) {
        Ok(ncursesw::erasewchar_sp(self.handle)?)
    }

    pub fn killwchar(&self) -> result!(WideChar) {
        Ok(ncursesw::killwchar_sp(self.handle)?)
    }

    /// Returns the topmost panel in the given screen.
    pub fn ceiling_panel(&self) -> result!(Panel) {
        Ok(Panel::_from(Some(self.handle), panels::ceiling_panel(self.handle)?, false))
    }

    /// Returns the lowest panel in the given screen.
    pub fn ground_panel(&self) -> result!(Panel) {
        Ok(Panel::_from(Some(self.handle), panels::ground_panel(self.handle)?, false))
    }

    pub fn update_panels(&self) {
        panels::update_panels_sp(self.handle)
    }
}

impl Drop for Screen {
    fn drop(&mut self) {
        if self.free_on_drop {
            if let Err(source) = ncursesw::endwin_sp(self.handle) {
                panic!("{} @ {:?}", source, self)
            }

            ncursesw::delscreen(self.handle);
        }
    }
}

unsafe impl Send for Screen { } // too make thread safe
unsafe impl Sync for Screen { } // too make thread safe

impl PartialEq for Screen {
    fn eq(&self, rhs: &Self) -> bool {
        ptr::eq(self.handle, rhs.handle)
    }
}

impl Eq for Screen { }

impl Hash for Screen {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.handle.hash(state);
    }
}

impl AsRef<Screen> for Screen {
    fn as_ref(&self) -> &Self {
        self
    }
}

impl Clone for Screen {
    fn clone(&self) -> Self {
        Self::_from(self.handle.clone(), false)
    }
}

impl fmt::Debug for Screen {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Screen {{ handle: {:p}, free_on_drop: {} }}", self.handle, self.free_on_drop)
    }
}