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
//! Widget abstraction and some basic Widgets useful for creating basic building blocks of text
//! user interfaces.
//!
//! # Example:
//! ```no_run //tests do not provide a fully functional terminal
//! use unsegen::base::*;
//! use unsegen::widget::*;
//! use unsegen::widget::builtin::*;
//! use std::io::stdout;
//!
//! struct MyWidget {
//!     layout: VerticalLayout,
//!     prompt: PromptLine,
//!     buffer: LogViewer,
//! }
//!
//! impl Widget for MyWidget {
//!    fn space_demand(&self) -> Demand2D {
//!        let widgets: Vec<&Widget> = vec![&self.prompt, &self.buffer];
//!        self.layout.space_demand(widgets.as_slice())
//!    }
//!    fn draw(&self, window: Window, hints: RenderingHints) {
//!        let widgets: Vec<(&Widget, RenderingHints)> =
//!            vec![(&self.prompt, hints), (&self.buffer, hints.active(false))];
//!        self.layout.draw(window, widgets.as_slice());
//!    }
//! }
//!
//!
//! fn main() {
//!     let stdout = stdout();
//!     let mut term = Terminal::new(stdout.lock()).unwrap();
//!     let mut widget = MyWidget {
//!         layout: VerticalLayout::new(
//!             SeparatingStyle::AlternatingStyle(StyleModifier::new().invert(true))
//!         ),
//!         prompt: PromptLine::with_prompt(" > ".to_owned()),
//!         buffer: LogViewer::new(),
//!     };
//!
//!     loop {
//!         // Put application logic here: read input, chain behavior, react to other stuff
//!         {
//!             let win = term.create_root_window();
//!             widget.draw(win, RenderingHints::new().active(true));
//!         }
//!         term.present();
//!     }
//! }
//! ```
pub mod builtin;
pub mod layouts;
pub mod widget;

pub use self::layouts::*;
pub use self::widget::*;
use super::base::*;

/// Count the number of grapheme clusters in the given string.
///
/// A thin convenience wrapper around unicode_segmentation.
pub fn count_grapheme_clusters(text: &str) -> usize {
    use unicode_segmentation::UnicodeSegmentation;
    text.graphemes(true).count()
}

/// Calculate the (monospace) width of the given string.
///
/// A thin convenience wrapper around unicode_width.
pub fn text_width(text: &str) -> Width {
    use unicode_width::UnicodeWidthStr;
    Width::new(UnicodeWidthStr::width(text) as _).unwrap()
}