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
// (C) 2025 - Enzo Lombardi
//! HistoryWindow view - popup window for selecting from input history.
// HistoryWindow - Popup window displaying history items
//
// Matches Borland: THistoryWindow (modal window with THistoryViewer)
//
// A modal popup window that displays history items and allows selection.
// Returns the selected history item when dismissed with Enter.
//
// Usage:
// let mut window = HistoryWindow::new(Point::new(10, 5), history_id, 15);
// if let Some(selected) = window.execute(terminal) {
// // User selected an item
// }
use super::history_viewer::HistoryViewer;
use super::view::View;
use super::window::Window;
use crate::core::event::{EventType, KB_ENTER, KB_ESC};
use crate::core::geometry::{Point, Rect};
use crate::terminal::Terminal;
/// HistoryWindow - Modal popup for selecting from history
///
/// Matches Borland: THistoryWindow
pub struct HistoryWindow {
window: Window,
viewer: HistoryViewer,
}
impl HistoryWindow {
/// Create a new history window at the given position
///
/// # Arguments
/// * `pos` - Top-left position of the window
/// * `history_id` - History list ID to display
/// * `width` - Width of the window (height auto-calculated based on items, max 10)
pub fn new(pos: Point, history_id: u16, width: i16) -> Self {
use crate::core::history::HistoryManager;
// Calculate height based on number of items (max 10, min 3)
let item_count = HistoryManager::count(history_id);
let viewer_height = item_count.min(10).max(1) as i16;
let window_height = viewer_height + 2; // +2 for frame
let window_bounds = Rect::new(pos.x, pos.y, pos.x + width, pos.y + window_height);
// Viewer bounds are in absolute screen coordinates, inset one cell inside
// the window frame (the viewer draws directly to the terminal).
let viewer_bounds = Rect::new(
pos.x + 1,
pos.y + 1,
pos.x + width - 1,
pos.y + 1 + viewer_height,
);
let window = Window::new(window_bounds, "History");
let mut viewer = HistoryViewer::new(viewer_bounds, history_id);
// Focus the viewer
viewer.set_focus(true);
Self { window, viewer }
}
/// Execute the history window modally
///
/// Returns the selected history item, or None if cancelled.
pub fn execute(&mut self, terminal: &mut Terminal) -> Option<String> {
loop {
// Create fresh token per frame for QCell safety
// Draw window and viewer
self.window.draw(terminal);
self.viewer.draw(terminal);
let _ = terminal.flush();
// Handle events
if let Ok(Some(mut event)) = terminal.poll_event(std::time::Duration::from_millis(50)) {
// Let viewer handle navigation first
self.viewer.handle_event(&mut event);
// Handle Enter and Esc
match event.what {
EventType::Keyboard => {
if event.key_code == KB_ENTER {
// Return selected item
return self.viewer.get_selected_item().map(|s| s.to_string());
} else if event.key_code == KB_ESC {
// Cancel
return None;
}
}
EventType::MouseDown => {
// Check if double-click on viewer
if event.mouse.double_click
&& self.viewer.bounds().contains(event.mouse.pos)
{
return self.viewer.get_selected_item().map(|s| s.to_string());
}
}
_ => {}
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::core::history::HistoryManager;
#[test]
fn test_history_window_creation() {
let _guard = crate::core::history::test_lock();
HistoryManager::clear(10);
HistoryManager::add(10, "test1".to_string());
HistoryManager::add(10, "test2".to_string());
HistoryManager::add(10, "test3".to_string());
let window = HistoryWindow::new(Point::new(10, 5), 10, 30);
// Window should be sized based on items
assert_eq!(window.viewer.item_count(), 3);
}
#[test]
fn test_history_viewer_bounds_relative_to_window() {
let _guard = crate::core::history::test_lock();
HistoryManager::clear(11);
HistoryManager::add(11, "a".to_string());
HistoryManager::add(11, "b".to_string());
HistoryManager::add(11, "c".to_string());
let window = HistoryWindow::new(Point::new(10, 5), 11, 30);
// Viewer is inset one cell inside the window frame at the window's
// screen position (regression: it used to be at absolute (1,1)).
assert_eq!(window.viewer.bounds(), Rect::new(11, 6, 39, 9));
}
#[test]
fn test_history_window_empty() {
let _guard = crate::core::history::test_lock();
HistoryManager::clear(99);
let window = HistoryWindow::new(Point::new(10, 5), 99, 30);
assert_eq!(window.viewer.item_count(), 0);
}
#[test]
fn test_history_window_many_items() {
let _guard = crate::core::history::test_lock();
HistoryManager::clear(20);
// Add 15 items
for i in 1..=15 {
HistoryManager::add(20, format!("item{}", i));
}
let window = HistoryWindow::new(Point::new(10, 5), 20, 30);
// Should have all 15 items but viewer height capped at 10
assert_eq!(window.viewer.item_count(), 15);
// Viewer must sit inside the window frame at its screen position
assert_eq!(window.viewer.bounds().a, Point::new(11, 6));
// Viewer bounds height should be at most 10
let viewer_height = window.viewer.bounds().height();
assert!(
viewer_height >= 1 && viewer_height <= 11,
"viewer height was {}",
viewer_height
);
}
}
/// Builder for creating history windows with a fluent API.
pub struct HistoryWindowBuilder {
pos: Option<Point>,
history_id: Option<u16>,
width: i16,
}
impl HistoryWindowBuilder {
pub fn new() -> Self {
Self {
pos: None,
history_id: None,
width: 30,
}
}
#[must_use]
pub fn pos(mut self, pos: Point) -> Self {
self.pos = Some(pos);
self
}
#[must_use]
pub fn history_id(mut self, history_id: u16) -> Self {
self.history_id = Some(history_id);
self
}
#[must_use]
pub fn width(mut self, width: i16) -> Self {
self.width = width;
self
}
pub fn build(self) -> HistoryWindow {
let pos = self.pos.expect("HistoryWindow pos must be set");
let history_id = self
.history_id
.expect("HistoryWindow history_id must be set");
HistoryWindow::new(pos, history_id, self.width)
}
pub fn build_boxed(self) -> Box<HistoryWindow> {
Box::new(self.build())
}
}
impl Default for HistoryWindowBuilder {
fn default() -> Self {
Self::new()
}
}