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
//! Resize event hook for detecting terminal size changes
//!
//! This module provides the `use_on_resize` hook for responding to terminal resize events.
use crate::;
use Event;
/// A hook that triggers a callback when the terminal is resized.
///
/// This hook monitors resize events and calls the provided callback with the new
/// terminal dimensions (width, height) whenever a resize occurs.
///
/// # Type Parameters
///
/// * `F` - The callback function type that takes `(u16, u16)` as parameters
///
/// # Arguments
///
/// * `callback` - A callback function that will be invoked with `(width, height)` when resize occurs
///
/// # Examples
///
/// ## Basic Usage
///
/// ```rust,no_run
/// use reratui_hooks::resize::use_on_resize;
///
/// // In a component context:
/// use_on_resize(|(width, height)| {
/// println!("Terminal resized to: {}x{}", width, height);
/// });
/// ```
///
/// ## With State Updates
///
/// ```rust,no_run
/// use reratui_hooks::{resize::use_on_resize, state::use_state};
///
/// // Track terminal size in state
/// let (size, set_size) = use_state(|| (80u16, 24u16));
///
/// use_on_resize({
/// let set_size = set_size.clone();
/// move |(width, height)| {
/// set_size.set((width, height));
/// }
/// });
/// ```
///
/// ## With Closure
///
/// ```rust,no_run
/// use reratui_hooks::{
/// resize::use_on_resize,
/// state::use_state,
/// };
///
/// let (size, set_size) = use_state(|| (80u16, 24u16));
///
/// use_on_resize({
/// let set_size = set_size.clone();
/// move |(width, height): (u16, u16)| {
/// set_size.set((width, height));
/// println!("Resized to: {}x{}", width, height);
/// }
/// });
/// ```
///
/// # Implementation Details
///
/// - Monitors `Event::Resize` events from the event system
/// - Only triggers callback when actual resize events occur
/// - Callback receives `(width, height)` as a tuple
/// - Works seamlessly with other hooks like `use_state` and `use_callback`
/// - No performance overhead when no resize occurs
///
/// # Notes
///
/// - The callback is called immediately when a resize event is detected
/// - Multiple components can use this hook independently
/// - Each component will receive resize events separately
/// - Consider using `use_callback` to memoize the callback for better performance
///
/// # Note
///
/// - The callback always sees the latest state values (via effect event pattern)
/// - Each resize event is only processed once per component
/// - The callback has a stable identity across renders
/// - Only resize events trigger the callback (keyboard, mouse, etc. are ignored)
///
/// # Performance
///
/// This hook is lightweight and only processes events when they occur. It does not
/// poll or create additional overhead when the terminal is not being resized.
/// A hook that triggers a memoized callback when the terminal is resized.
///
/// This is a variant of `use_on_resize` that accepts a `MemoizedCallback` instead
/// of a raw closure. This is useful when you want to use callbacks created with
/// `use_callback` or `use_effect_event`.
///
/// # Type Parameters
///
/// * `IN` - The input type for the callback (must be `(u16, u16)`)
/// * `OUT` - The output type of the callback
///
/// # Arguments
///
/// * `callback` - A memoized callback that will be invoked with `(width, height)`
///
/// # Examples
///
/// ```rust,no_run
/// use reratui_hooks::{
/// resize::use_on_resize_callback,
/// callback::use_callback,
/// state::use_state,
/// };
///
/// let (size, set_size) = use_state(|| (80u16, 24u16));
///
/// let handle_resize = use_callback(
/// {
/// let set_size = set_size.clone();
/// move |(width, height): (u16, u16)| {
/// set_size.set((width, height));
/// }
/// },
/// set_size.version(),
/// );
///
/// use_on_resize_callback(handle_resize);
/// ```
/// A hook that returns the current terminal dimensions as a tuple.
///
/// This is a convenience hook that automatically tracks terminal size and returns
/// the current dimensions directly as a tuple.
///
/// # Returns
///
/// A tuple `(u16, u16)` containing the current terminal dimensions (width, height)
///
/// # Examples
///
/// ```rust,no_run
/// use reratui_hooks::resize::use_terminal_dimensions;
///
/// // In a component context:
/// let (width, height) = use_terminal_dimensions();
/// println!("Terminal: {}x{}", width, height);
/// ```
///
/// ## Responsive Layout Example
///
/// ```rust,no_run
/// use reratui_hooks::resize::use_terminal_dimensions;
///
/// let (width, height) = use_terminal_dimensions();
///
/// // Adjust UI based on terminal size
/// if width < 80 {
/// // Render compact layout
/// } else {
/// // Render full layout
/// }
/// ```
///
/// # Notes
///
/// - Returns (0, 0) until the first resize event occurs
/// - Automatically updates when the terminal is resized
/// - Re-renders the component when dimensions change
/// - Most convenient API for read-only dimension access