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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
mod filter_popup;
mod image_info;
mod layer_info;
mod layer_inspector;
mod layer_selector;
mod style;
mod util;
use std::borrow::Cow;
use anyhow::Context;
use image_info::ImageInfoField;
pub use image_info::ImageInfoPane;
use layer_info::LayerInfoField;
pub use layer_info::LayerInfoPane;
pub use layer_inspector::LayerInspectorPane;
pub use layer_selector::LayerSelectorPane;
use ratatui::layout::Rect;
use ratatui::style::{Modifier, Style, Stylize};
use ratatui::text::{Line, Text};
use ratatui::widgets::block::Title;
use ratatui::widgets::{Block, BorderType, Paragraph, Widget, Wrap};
use style::{ACTIVE_FIELD_STYLE, text_style};
pub(super) use style::{
FIELD_KEY_STYLE, FIELD_VALUE_STYLE, LayerInspectorNodeStyles,
};
use util::fields_into_lines;
use super::widgets::PaneWithPopup;
use super::{ActivePane, SideEffect};
use crate::parser::{Image, LayerChangeSet};
use crate::tui::action::Direction;
use crate::tui::store::AppState;
use crate::tui::util::encode_hex;
/// All panes that exist in the app.
///
/// Each variant also holds all the state that a particular pane needs, as
/// these variants are created once during the app initialization and are then reused.
pub enum Pane {
/// Contains all image-related information from [crate::parser::Image].
ImageInfo(ImageInfoPane),
/// Displays infromation about the [LayerSelectorPane::selected_layer].
LayerInfo(LayerInfoPane),
/// Allows switching between [Layers](crate::parser::Layer) of the [crate::parser::Image].
LayerSelector(LayerSelectorPane),
/// Displays the aggregated changeset from the currently selected [Layers](crate::parser::Layer).
LayerInspector(LayerInspectorPane),
}
impl Pane {
/// Returns a [Widget] that can be used to render the current pane in the terminal.
pub fn render<'a>(
&'a self,
state: &'a AppState,
pane_rows: u16,
pane_cols: u16,
) -> anyhow::Result<impl Widget + 'a> {
let pane_is_active =
state.active_pane == self.into() && !state.show_help_popup;
let pane_text_style = text_style(pane_is_active);
let field_key_style = FIELD_KEY_STYLE.patch(pane_text_style);
let field_value_style = FIELD_VALUE_STYLE.patch(pane_text_style);
let active_field_style = ACTIVE_FIELD_STYLE.patch(pane_text_style);
// Two rows are taken by the block borders
let remaining_rows = pane_rows - 2;
// Two cols are taken by the block borders
let remaining_cols = pane_cols - 2;
let mut widget = PaneWithPopup::<Paragraph, Paragraph>::new(None, None);
let block = self.get_styled_block(pane_is_active);
let pane_widget = match self {
Pane::ImageInfo(pane_state) => {
let lines = fields_into_lines(
pane_state.get_fields(),
field_key_style,
field_value_style,
|field_key| {
if pane_state.active_field == field_key
&& pane_is_active
{
active_field_style
} else {
Style::default()
}
},
);
Paragraph::new(Text::from(lines)).block(block)
}
Pane::LayerSelector(pane_state) => {
let lines = pane_state.lines(
state.layers.iter(),
field_value_style,
remaining_rows,
remaining_cols,
);
Paragraph::new(Text::from(lines)).block(block)
}
Pane::LayerInfo(pane_state) => {
let (selected_layer_digest, selected_layer, _) = state
.get_selected_layer()
.context("failed to get the currently selected layer")?;
let lines = fields_into_lines(
LayerInfoPane::get_fields(
selected_layer_digest,
selected_layer,
),
field_key_style,
field_value_style,
|field_key| {
if pane_state.active_field == field_key
&& pane_is_active
{
active_field_style
} else {
Style::default()
}
},
);
// FIXME: add a vertical scroll
// - this requires doing wrap manually, as there is no way to know how many lines will the wrapped text take
Paragraph::new(Text::from(lines))
.wrap(Wrap { trim: true })
.block(block)
}
Pane::LayerInspector(pane_state) => {
let (layer_changeset, _) =
state.get_aggregated_layers_changeset()?;
let (_, _, current_layer_idx) = state.get_selected_layer()?;
let lines = pane_state
.changeset_to_lines(
layer_changeset,
|node_is_selected,
node_updated_in,
node_is_deleted,
node_is_modified| {
if pane_is_active && node_is_selected {
LayerInspectorNodeStyles::get_selected_node_style()
} else if node_updated_in == current_layer_idx as u8
&& current_layer_idx != 0
{
if node_is_deleted {
LayerInspectorNodeStyles::get_deleted_node_style(pane_is_active)
} else if node_is_modified {
LayerInspectorNodeStyles::get_modified_node_style(pane_is_active)
} else {
LayerInspectorNodeStyles::get_added_node_style(pane_is_active)
}
} else {
field_value_style
}
},
remaining_rows,
)
.context("layer inspector: failed to render a changeset")?;
if let Some(filter_popup) = pane_state.filter_popup() {
let (popup, v_constraint, h_constraint) =
filter_popup.render_with_layout_constraints();
widget.set_popup((
popup,
Some(v_constraint),
Some(h_constraint),
));
}
// FIXME: add a horizontal scroll
Paragraph::new(Text::from(lines)).block(block)
}
};
widget.set_pane(pane_widget);
Ok(widget)
}
/// Moves (vertically) to the next entry in the specified [Direction] inside the [Pane].
pub fn move_within_pane(
&mut self,
direction: Direction,
state: &AppState,
) -> anyhow::Result<Option<SideEffect>> {
match self {
Pane::ImageInfo(pane_state) => {
pane_state.toggle_active_field(direction);
Ok(None)
}
Pane::LayerSelector(pane_state) => {
pane_state.move_within_pane(direction, state)
}
Pane::LayerInfo(pane_state) => {
pane_state.toggle_active_field(direction);
Ok(None)
}
Pane::LayerInspector(pane_state) => {
pane_state.move_within_pane(direction, state).map(|_| None)
}
}
}
/// Returns the currently selected value within a [Pane].
pub fn get_selected_field<'a>(
&'a self,
state: &'a AppState,
) -> Option<Cow<'a, str>> {
match self {
Pane::ImageInfo(ImageInfoPane {
active_field,
image_name,
tag,
size,
architecture,
os,
}) => Some(match active_field {
ImageInfoField::Repository => image_name.as_ref().into(),
ImageInfoField::Tag => tag.as_ref().into(),
ImageInfoField::Size => size.as_ref().into(),
ImageInfoField::Architecture => architecture.into(),
ImageInfoField::Os => os.into(),
}),
Pane::LayerInfo(LayerInfoPane { active_field }) => {
let Ok((selected_layer_digest, selected_layer, _)) =
state.get_selected_layer()
else {
// Add a log here for debugging purposes in case this happens somehow
tracing::debug!(
"Failed to get the currently selected layer when getting the selected field from the LayerInfo pane"
);
return None;
};
Some(match active_field {
LayerInfoField::Digest => {
encode_hex(selected_layer_digest).into()
}
LayerInfoField::Command => {
selected_layer.created_by.as_str().into()
}
LayerInfoField::Comment => {
selected_layer.comment.as_ref()?.into()
}
})
}
Pane::LayerInspector(pane) => {
match pane.get_current_node_full_path(state) {
Ok(path) => Some(path),
Err(e) => {
tracing::debug!(
"Failed to get the path of the currently selected node: {}",
e
);
None
}
}
}
_ => None,
}
}
/// Interacts with the currently active element inside the [Pane].
///
/// The actual action depends on the currently active [Pane] and its state.
pub fn interact_within_pane(
&mut self,
state: &AppState,
) -> anyhow::Result<()> {
if let Pane::LayerInspector(pane_state) = self {
// Only the inspector pane supports this action for now.
pane_state
.toggle_active_node(state)
.context("layer inspector: failed to toggle the active node")?;
}
Ok(())
}
/// Toggles the input mode and allows the [Pane] to accept user's input without processing any special keys.
///
/// What exactly user inputs depends on the [Pane] itself.
pub fn toggle_input_mode(&mut self) -> (bool, Option<SideEffect>) {
// Only the inspector pane supports this action for now.
if let Pane::LayerInspector(pane_state) = self {
let input_is_active = pane_state.toggle_filter_popup();
// Apply the filter only when user exits the input screen to avoid using a lot of resources for nothing
let side_effect =
(!input_is_active).then_some(SideEffect::FiltersUpdated);
return (input_is_active, side_effect);
};
(false, None)
}
/// Handles user's input when in "insert" mode.
///
/// How user's input is handled depends on the [Pane] itself.
///
/// # Safety
///
/// Should be called only in insert mode.
pub fn on_input_character(&mut self, input: char) {
// Only the inspector pane supports this action for now.
if let Pane::LayerInspector(pane_state) = self {
pane_state.append_to_filter(input);
};
}
/// Handles a backspace when in "insert" mode.
///
/// How it's handled depends on the [Pane] itself.
///
/// # Safety
///
/// Should be called only in insert mode.
pub fn on_backspace(&mut self) {
// Only the inspector pane supports this action for now.
if let Pane::LayerInspector(pane_state) = self {
pane_state.pop_from_filter();
};
}
/// Scroll horizontally within the [Pane].
///
/// Vertical scroll is done in [Self::move_within_pane].
pub fn scroll_within_pane(
&mut self,
direction: Direction,
state: &AppState,
) -> anyhow::Result<()> {
let pane_area = state
.panes
.get(Into::<usize>::into(Into::<ActivePane>::into(&*self)))
.map(|(_, rect)| (rect.width, rect.height))
.context("bug: ghost pane?")?;
// Only the inspector pane supports this action for now.
if let Pane::LayerSelector(pane_state) = self {
pane_state
.scroll(direction, pane_area, state)
.context("error while scrolling the layer selector pane")?
};
Ok(())
}
/// Handles a subaction.
///
/// How it's handled depends on the [Pane] itself.
pub fn on_subaction(&mut self) -> Option<SideEffect> {
// Only the inspector pane supports this action for now.
if let Pane::LayerInspector(pane_state) = self {
pane_state.toggle_show_only_changed_files();
return Some(SideEffect::FiltersUpdated);
};
None
}
/// Returns a styled [Block] for the pane.
fn get_styled_block(&self, is_active: bool) -> Block<'_> {
let (border_type, border_style) = if is_active {
(BorderType::Thick, Style::new().add_modifier(Modifier::BOLD))
} else {
(BorderType::Plain, Style::new().add_modifier(Modifier::DIM))
};
Block::bordered()
.border_type(border_type)
.border_style(border_style)
.title(self.get_styled_title(is_active))
.title(Line::from(
Into::<ActivePane>::into(self).to_formatted_index(),
))
}
/// Returns a styled [Title] for the pane.
fn get_styled_title(&self, is_active: bool) -> impl Into<Title<'static>> {
let title = match self {
Pane::ImageInfo(..) => "Image Information",
Pane::LayerSelector(..) => "Layers",
Pane::LayerInfo(..) => "Layer Information",
Pane::LayerInspector(..) => "Layer Changes",
};
let title = if is_active {
title.bold()
} else {
title.not_bold().dim()
};
title.into_centered_line()
}
}
/// Initializes all panes from the provided [Image].
pub fn init_panes(
image: &mut Image,
) -> anyhow::Result<[(Option<Pane>, Rect); 4]> {
let image_info_pane = Pane::ImageInfo(ImageInfoPane::new(
std::mem::take(&mut image.image_name),
std::mem::take(&mut image.tag),
std::mem::take(&mut image.size),
std::mem::take(&mut image.architecture),
std::mem::take(&mut image.os),
));
let (_, layer) = image
.layers
.get_index(0)
.context("got an image with 0 layers")?;
let layer_selector_pane = Pane::LayerSelector(LayerSelectorPane::new(
0,
layer.changeset.clone().unwrap_or(LayerChangeSet::new(0)),
));
let layer_info_pane = Pane::LayerInfo(LayerInfoPane::default());
let layer_inspector_pane =
Pane::LayerInspector(LayerInspectorPane::default());
// Note that we assign zeroed rects here. This means that we won't be able to render anything before dispatching at least one
// [AppAction::Empty] event with the correct terminal size.
let mut panes = [
(Some(image_info_pane), Rect::ZERO),
(Some(layer_info_pane), Rect::ZERO),
(Some(layer_selector_pane), Rect::ZERO),
(Some(layer_inspector_pane), Rect::ZERO),
];
// Ensure that panes are always sorted by the render order, determined
// by the order of enum's variants declaration.
panes.sort_by_key(|(a, _)| {
Into::<usize>::into(Into::<ActivePane>::into(a.as_ref().unwrap()))
});
Ok(panes)
}