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
//! Exclusions section
use crate::action::{Action, ActionResult};
use crate::color::SectionColors;
use crate::section::{Section, SectionId, SectionTrait};
use crate::strings;
use crate::ui::dialog::DialogType;
use crate::ui::section_layout::SectionHeights;
/// Height configuration for Exclusions section
/// Defined within this module as per design requirement
pub const HEIGHTS: SectionHeights = SectionHeights {
note_min: 1,
note_max: 1,
content_min: 3, // List display (min 3 lines)
content_max: 15, // List display (max 15 lines, scrollable if more)
actions_min: 1,
actions_max: 1,
border_overhead: 2,
};
use ratatui::layout::Rect;
use ratatui::{
style::{Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, List, ListItem, Paragraph, Scrollbar, ScrollbarOrientation, ScrollbarState},
};
pub fn create_exclusions_section() -> Section {
Section::new(
SectionId::Exclusions,
strings::exclusions::TITLE,
|_| strings::exclusions::HINT.to_string(),
|app| {
if app.state.exclude.is_empty() {
strings::exclusions::NONE.to_string()
} else {
app.state.exclude.join(", ")
}
},
|_app| {
vec![
Action::new(
strings::exclusions::actions::ENTER_EXCLUSION_PATTERN,
|_| true,
|app| {
app.input_dialog = Some(crate::ui::dialog::Dialog::new(
DialogType::ExclusionPatternInput,
strings::exclusions::dialog::EXCLUSION_PATTERN_TITLE,
));
ActionResult::DialogOpened
},
),
Action::new(
strings::exclusions::actions::CHOOSE_TEMPLATE,
|_| true,
|app| {
app.input_dialog = Some(crate::ui::dialog::Dialog::new(
DialogType::TemplateSelection {
field: crate::ui::dialog::TemplateField::ExclusionPattern,
},
strings::dialog::template_selection::TITLE,
));
ActionResult::DialogOpened
},
),
Action::new(
strings::exclusions::actions::SPECIFY_FILES,
|app| !app.state.list.is_empty(),
|app| {
app.input_dialog = Some(crate::ui::dialog::Dialog::new(
DialogType::FileSelection,
strings::exclusions::dialog::FILE_SELECTION_TITLE,
));
ActionResult::DialogOpened
},
),
Action::new(
strings::exclusions::actions::CLEAR,
|app| !app.state.exclude.is_empty(),
|app| {
app.state.exclude.clear();
ActionResult::StateUpdated
},
),
]
},
|f, app, area, is_focused| {
use ratatui::layout::{Constraint, Layout};
let border_style = if is_focused {
Style::default()
.fg(SectionColors::FOCUSED_BORDER)
.add_modifier(Modifier::BOLD)
} else {
Style::default().fg(SectionColors::BORDER)
};
let inner_area = Rect {
x: area.x + 1,
y: area.y + 1,
width: area.width.saturating_sub(2),
height: area.height.saturating_sub(2),
};
// Use Length(1) when empty or single item to avoid extra blank lines
// Use Fill(1) when multiple items to allow scrolling
let list_constraint = if app.state.exclude.is_empty() || app.state.exclude.len() == 1 {
Constraint::Length(1) // Exactly 1 line for "None" or single item
} else {
Constraint::Fill(1) // Fill remaining space for multiple items (scrollable)
};
let chunks = Layout::default()
.constraints([
Constraint::Length(1), // Blank line
Constraint::Length(1), // Note
Constraint::Length(1), // Blank line
list_constraint, // List (1 line if empty, variable if not)
Constraint::Length(1), // Blank line
Constraint::Length(1), // Actions
])
.split(inner_area);
let _blank1 = chunks[0]; // Blank line - not rendered
let note_area = chunks[1];
let _blank2 = chunks[2]; // Blank line - not rendered
let list_area = chunks[3];
let _blank3 = chunks[4]; // Blank line - not rendered
let actions_area = chunks[5];
// Render note (hint)
let hint = strings::exclusions::HINT;
let note_paragraph = Paragraph::new(Line::from(vec![
Span::styled(hint, Style::default().fg(SectionColors::HINT)),
]));
f.render_widget(note_paragraph, note_area);
// Render exclusions list
if app.state.exclude.is_empty() {
let empty_paragraph = Paragraph::new(Line::from(vec![
Span::styled(strings::exclusions::NONE, Style::default().fg(SectionColors::VALUE)),
]));
f.render_widget(empty_paragraph, list_area);
} else if app.state.exclude.len() == 1 {
// Single item - render as Paragraph to avoid List widget padding
let workdirs_canonical: Vec<_> = app.state.workdirs.iter()
.filter_map(|w| w.canonicalize().ok())
.collect();
let exclusion = &app.state.exclude[0];
let display_text = if let Ok(path) = std::path::PathBuf::from(exclusion).canonicalize() {
// It's a file path - check if it's within any workdir
let is_in_workdir = workdirs_canonical.iter()
.any(|workdir| path.starts_with(workdir));
if is_in_workdir {
// Within a workdir - show filename only
path.file_name()
.and_then(|n| n.to_str())
.map(|s| s.to_string())
.unwrap_or_else(|| exclusion.clone())
} else {
// Outside all workdirs - show full path
path.display().to_string()
}
} else {
// It's a pattern, not a file path - show as-is
exclusion.clone()
};
let single_paragraph = Paragraph::new(Line::from(vec![
Span::styled(display_text, Style::default().fg(SectionColors::VALUE)),
]));
f.render_widget(single_paragraph, list_area);
} else {
// Format paths: show full path if not in any workdir, filename only if in a workdir
let workdirs_canonical: Vec<_> = app.state.workdirs.iter()
.filter_map(|w| w.canonicalize().ok())
.collect();
let items: Vec<ListItem> = app.state.exclude.iter()
.map(|exclusion| {
// Check if it's a file path (try to parse as PathBuf and canonicalize)
let display_text = if let Ok(path) = std::path::PathBuf::from(exclusion).canonicalize() {
// It's a file path - check if it's within any workdir
let is_in_workdir = workdirs_canonical.iter()
.any(|workdir| path.starts_with(workdir));
if is_in_workdir {
// Within a workdir - show filename only
path.file_name()
.and_then(|n| n.to_str())
.map(|s| s.to_string())
.unwrap_or_else(|| exclusion.clone())
} else {
// Outside all workdirs - show full path
path.display().to_string()
}
} else {
// It's a pattern, not a file path - show as-is
exclusion.clone()
};
ListItem::new(Line::from(vec![
Span::styled(display_text, Style::default().fg(SectionColors::VALUE)),
]))
})
.collect();
let list = List::new(items);
// Update list state selection if needed
let items_len = app.state.exclude.len();
{
let mut list_state = app.exclusions_list_state.borrow_mut();
if let Some(sel) = list_state.selected() {
if sel >= items_len && items_len > 0 {
list_state.select(Some(0));
}
} else if items_len > 0 {
list_state.select(Some(0));
}
}
// Render list
{
let mut list_state = app.exclusions_list_state.borrow_mut();
f.render_stateful_widget(list, list_area, &mut *list_state);
}
// Update and render scrollbar if content exceeds visible area
let content_length = items_len;
let visible_height = list_area.height as usize;
if content_length > visible_height {
// Update scrollbar state
let offset = app.exclusions_list_state.borrow().offset();
let mut scrollbar_state = app.exclusions_scrollbar_state.borrow_mut();
*scrollbar_state = ScrollbarState::new(content_length)
.position(offset)
.viewport_content_length(visible_height);
// Render scrollbar on the right side
let scrollbar = Scrollbar::default()
.orientation(ScrollbarOrientation::VerticalRight);
f.render_stateful_widget(scrollbar, list_area, &mut *scrollbar_state);
}
}
let actions = app.sections.iter()
.find(|s| s.id() == SectionId::Exclusions)
.map(|s| s.actions(app))
.unwrap_or_default();
crate::ui::action_menu::render_action_menu(
f,
actions_area,
&actions,
if is_focused { app.action_selection } else { None },
app,
);
let block = Block::default()
.title(strings::exclusions::TITLE)
.borders(Borders::ALL)
.border_style(border_style);
f.render_widget(block, area);
},
|_app, _key| false,
HEIGHTS,
)
}