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
//! use_window_size hook for terminal dimensions
//!
//! Provides reactive terminal window size tracking.
//!
//! # Example
//!
//! ```rust,ignore
//! use rnk::prelude::*;
//!
//! fn app() -> Element {
//! let (width, height) = use_window_size();
//!
//! Text::new(format!("Terminal: {}x{}", width, height)).into_element()
//! }
//! ```
use crossterm::terminal;
/// Get the current terminal size
pub fn get_terminal_size() -> (u16, u16) {
terminal::size().unwrap_or((80, 24))
}
/// Hook to get reactive terminal window size
///
/// Returns (width, height) tuple that updates on resize.
pub fn use_window_size() -> (u16, u16) {
get_terminal_size()
}
/// Hook to get only the terminal width
pub fn use_window_width() -> u16 {
let (width, _) = use_window_size();
width
}
/// Hook to get only the terminal height
pub fn use_window_height() -> u16 {
let (_, height) = use_window_size();
height
}
/// Check if terminal is wide enough for a given width
pub fn use_is_wide_enough(min_width: u16) -> bool {
let (width, _) = use_window_size();
width >= min_width
}
/// Check if terminal is tall enough for a given height
pub fn use_is_tall_enough(min_height: u16) -> bool {
let (_, height) = use_window_size();
height >= min_height
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_terminal_size() {
let (w, h) = get_terminal_size();
assert!(w > 0);
assert!(h > 0);
}
#[test]
fn test_use_window_size_compiles() {
fn _test() {
let (w, h) = use_window_size();
let _ = w + h;
}
}
#[test]
fn test_use_window_width_compiles() {
fn _test() {
let w = use_window_width();
let _ = w;
}
}
#[test]
fn test_use_is_wide_enough_compiles() {
fn _test() {
let _ = use_is_wide_enough(80);
}
}
}