oxker 0.13.2

A simple tui to view & control docker containers
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
use ratatui::style::Color;

static COLOR_RX: Color = Color::Rgb(255, 233, 193);
static COLOR_TX: Color = Color::Rgb(205, 140, 140);

/// The macro accepts a list of struct names with key names
/// Returns a struct where every key name is an Option<String>, with the correct derived attributes
macro_rules! optional_config_struct {
    ($($struct_name:ident, $($key_name:ident),*);*) => {
        $(
            #[derive(Debug, serde::Deserialize, Clone, PartialEq, Eq)]
            struct $struct_name {
                $(
                    $key_name: Option<String>,
                )*
            }
        )*
    };
}

/// The macro accepts a list of struct names with key names
macro_rules! config_struct {
    ($($struct_name:ident, $($key_name:ident),*);*) => {
        $(
            #[derive(Debug, Clone, PartialEq, Eq, Copy)]
            pub struct $struct_name {
                $(
                    pub $key_name: Color,
                )*
            }
        )*
    };
}

impl AppColors {
    fn map_color(color_str: Option<&str>, setter: &mut Color) {
        color_str.map(|i| i.parse::<Color>().map(|i| *setter = i));
    }
}

impl From<Option<ConfigColors>> for AppColors {
    #[allow(clippy::too_many_lines)]
    fn from(value: Option<ConfigColors>) -> Self {
        let mut app_colors = Self::new();

        if let Some(config_colors) = value {
            // Heading bar
            if let Some(hb) = config_colors.headers_bar {
                Self::map_color(
                    hb.background.as_deref(),
                    &mut app_colors.headers_bar.background,
                );
                Self::map_color(
                    hb.loading_spinner.as_deref(),
                    &mut app_colors.headers_bar.loading_spinner,
                );
                Self::map_color(hb.text.as_deref(), &mut app_colors.headers_bar.text);
                Self::map_color(
                    hb.text_selected.as_deref(),
                    &mut app_colors.headers_bar.text_selected,
                );
            }

            // Selectable panel borders
            if let Some(b) = config_colors.borders {
                Self::map_color(b.selected.as_deref(), &mut app_colors.borders.selected);
                Self::map_color(b.unselected.as_deref(), &mut app_colors.borders.unselected);
            }

            // Error Popup
            if let Some(ep) = config_colors.popup_error {
                Self::map_color(
                    ep.background.as_deref(),
                    &mut app_colors.popup_error.background,
                );
                Self::map_color(ep.text.as_deref(), &mut app_colors.popup_error.text);
            }

            // Filter panel
            if let Some(fc) = config_colors.filter {
                Self::map_color(fc.background.as_deref(), &mut app_colors.filter.background);
                Self::map_color(fc.highlight.as_deref(), &mut app_colors.filter.highlight);

                Self::map_color(
                    fc.selected_filter_background.as_deref(),
                    &mut app_colors.filter.selected_filter_background,
                );
                Self::map_color(
                    fc.selected_filter_text.as_deref(),
                    &mut app_colors.filter.selected_filter_text,
                );
                Self::map_color(fc.text.as_deref(), &mut app_colors.filter.text);
            }

            // Log search
            if let Some(ls) = config_colors.log_search {
                Self::map_color(
                    ls.background.as_deref(),
                    &mut app_colors.log_search.background,
                );
                Self::map_color(
                    ls.highlight.as_deref(),
                    &mut app_colors.log_search.highlight,
                );

                Self::map_color(
                    ls.button_text.as_deref(),
                    &mut app_colors.log_search.button_text,
                );
                Self::map_color(ls.text.as_deref(), &mut app_colors.log_search.text);
            }

            // Help Popup
            if let Some(hp) = config_colors.popup_help {
                Self::map_color(
                    hp.background.as_deref(),
                    &mut app_colors.popup_help.background,
                );
                Self::map_color(hp.text.as_deref(), &mut app_colors.popup_help.text);
                Self::map_color(
                    hp.text_highlight.as_deref(),
                    &mut app_colors.popup_help.text_highlight,
                );
            }

            // Info Popup
            if let Some(ip) = config_colors.popup_info {
                Self::map_color(
                    ip.background.as_deref(),
                    &mut app_colors.popup_info.background,
                );
                Self::map_color(ip.text.as_deref(), &mut app_colors.popup_info.text);
            }

            // Delete Popup
            if let Some(dp) = config_colors.popup_delete {
                Self::map_color(
                    dp.background.as_deref(),
                    &mut app_colors.popup_delete.background,
                );
                Self::map_color(dp.text.as_deref(), &mut app_colors.popup_delete.text);
                Self::map_color(
                    dp.text_highlight.as_deref(),
                    &mut app_colors.popup_delete.text_highlight,
                );
            }

            // Chart Cpu
            if let Some(cc) = config_colors.chart_cpu {
                Self::map_color(
                    cc.background.as_deref(),
                    &mut app_colors.chart_cpu.background,
                );
                Self::map_color(cc.border.as_deref(), &mut app_colors.chart_cpu.border);
                Self::map_color(cc.max.as_deref(), &mut app_colors.chart_cpu.max);
                Self::map_color(cc.points.as_deref(), &mut app_colors.chart_cpu.points);
                Self::map_color(cc.title.as_deref(), &mut app_colors.chart_cpu.title);
                Self::map_color(cc.y_axis.as_deref(), &mut app_colors.chart_cpu.y_axis);
            }

            // Chart Memory
            if let Some(cm) = config_colors.chart_memory {
                Self::map_color(
                    cm.background.as_deref(),
                    &mut app_colors.chart_memory.background,
                );
                Self::map_color(cm.border.as_deref(), &mut app_colors.chart_memory.border);
                Self::map_color(cm.max.as_deref(), &mut app_colors.chart_memory.max);
                Self::map_color(cm.points.as_deref(), &mut app_colors.chart_memory.points);
                Self::map_color(cm.title.as_deref(), &mut app_colors.chart_memory.title);
                Self::map_color(cm.y_axis.as_deref(), &mut app_colors.chart_memory.y_axis);
            }

            // Chart ports
            if let Some(cp) = config_colors.chart_ports {
                Self::map_color(
                    cp.background.as_deref(),
                    &mut app_colors.chart_ports.background,
                );
                Self::map_color(cp.border.as_deref(), &mut app_colors.chart_ports.border);
                Self::map_color(cp.headings.as_deref(), &mut app_colors.chart_ports.headings);
                Self::map_color(cp.text.as_deref(), &mut app_colors.chart_ports.text);
                Self::map_color(cp.title.as_deref(), &mut app_colors.chart_ports.title);
            }

            // Containers
            if let Some(c) = config_colors.containers {
                Self::map_color(
                    c.background.as_deref(),
                    &mut app_colors.containers.background,
                );
                Self::map_color(c.icon.as_deref(), &mut app_colors.containers.icon);
                Self::map_color(c.text.as_deref(), &mut app_colors.containers.text);
                Self::map_color(c.text_rx.as_deref(), &mut app_colors.containers.text_rx);
                Self::map_color(c.text_tx.as_deref(), &mut app_colors.containers.text_tx);
            }

            // Commands
            if let Some(cc) = config_colors.commands {
                Self::map_color(
                    cc.background.as_deref(),
                    &mut app_colors.commands.background,
                );
                Self::map_color(cc.pause.as_deref(), &mut app_colors.commands.pause);
                Self::map_color(cc.restart.as_deref(), &mut app_colors.commands.restart);
                Self::map_color(cc.stop.as_deref(), &mut app_colors.commands.stop);
                Self::map_color(cc.delete.as_deref(), &mut app_colors.commands.start);
                Self::map_color(cc.resume.as_deref(), &mut app_colors.commands.resume);
                Self::map_color(cc.start.as_deref(), &mut app_colors.commands.start);
            }

            // Logs panel
            if let Some(cl) = config_colors.logs {
                Self::map_color(cl.background.as_deref(), &mut app_colors.logs.background);
                Self::map_color(cl.text.as_deref(), &mut app_colors.logs.text);
            }

            // Container State
            if let Some(cs) = config_colors.container_state {
                Self::map_color(cs.dead.as_deref(), &mut app_colors.container_state.dead);
                Self::map_color(cs.exited.as_deref(), &mut app_colors.container_state.exited);
                Self::map_color(cs.paused.as_deref(), &mut app_colors.container_state.paused);
                Self::map_color(
                    cs.removing.as_deref(),
                    &mut app_colors.container_state.removing,
                );
                Self::map_color(
                    cs.restarting.as_deref(),
                    &mut app_colors.container_state.restarting,
                );
                Self::map_color(
                    cs.running_healthy.as_deref(),
                    &mut app_colors.container_state.running_healthy,
                );
                Self::map_color(
                    cs.running_unhealthy.as_deref(),
                    &mut app_colors.container_state.running_unhealthy,
                );
                Self::map_color(
                    cs.unknown.as_deref(),
                    &mut app_colors.container_state.unknown,
                );
            }
        }
        app_colors
    }
}

const ORANGE: Color = Color::Rgb(255, 178, 36);

optional_config_struct!(
    ConfigBackgroundText, background, text;
    ConfigBackgroundTextHighlight, background, text, text_highlight;
    ConfigBorders, selected, unselected;
    ConfigChartBandwidth, background, border, max_rx, max_tx, title_tx, title_rx, points_rx, points_tx, y_axis;

    ConfigChartCpu, background, border, order, title, max, points,y_axis;
    ConfigChartMemory, background, border, title, max, points, y_axis;
    ConfigChartPorts, background, border, title, headings, text;
    ConfigCommands, background, pause, restart, stop, delete, resume, start;
    ConfigContainers, background, icon, text, text_rx, text_tx;
    ConfigContainerState, background, dead, exited, paused, removing, restarting, running_healthy, running_unhealthy, unknown;
    ConfigFilter, background, text, selected_filter_background, selected_filter_text, highlight;
     ConfigLogSearch, background, text, button_text, highlight;
    ConfigHeadersBar, background, loading_spinner, text, text_selected;
    ConfigLogs, background, text
);

config_struct!(
    Borders, selected, unselected;
    ChartCpu, background, border, title, max, points, y_axis;
    ChartMemory, background, border, title, max, points, y_axis;
    ChartBandwidth, background, border, max_rx, max_tx, title_rx, title_tx, points_rx, points_tx, y_axis;

    ChartPorts, background, border, title, headings, text;
    Commands, background, pause, restart, stop, delete, resume, start;
    Containers, background, icon, text, text_rx, text_tx;
    ContainerState, dead, exited, paused, removing, restarting, running_healthy, running_unhealthy, unknown;
    Filter, background, text, selected_filter_background, selected_filter_text, highlight;
    LogSearch, background, text, button_text, highlight;
    HeadersBar, background, text_selected, loading_spinner, text;
    Logs, background, text;
    PopupDelete, background, text, text_highlight;
    PopupError, background, text;
    PopupHelp, background, text, text_highlight;
    PopupInfo, background, text
);

#[derive(Debug, serde::Deserialize, Clone, PartialEq, Eq)]
pub struct ConfigColors {
    borders: Option<ConfigBorders>,
    chart_cpu: Option<ConfigChartCpu>,
    chart_memory: Option<ConfigChartMemory>,
    chart_bandwidth: Option<ConfigChartBandwidth>,
    chart_ports: Option<ConfigChartPorts>,
    commands: Option<ConfigCommands>,
    container_state: Option<ConfigContainerState>,
    containers: Option<ConfigContainers>,
    filter: Option<ConfigFilter>,
    log_search: Option<ConfigLogSearch>,
    headers_bar: Option<ConfigHeadersBar>,
    logs: Option<ConfigLogs>,
    popup_delete: Option<ConfigBackgroundTextHighlight>,
    popup_error: Option<ConfigBackgroundText>,
    popup_help: Option<ConfigBackgroundTextHighlight>,
    popup_info: Option<ConfigBackgroundText>,
}

/// Default colours for the header bar
impl HeadersBar {
    const fn new() -> Self {
        Self {
            background: Color::Magenta,
            loading_spinner: Color::White,
            text: Color::Black,
            text_selected: Color::Gray,
        }
    }
}

/// Default colours for the borders
impl Borders {
    const fn new() -> Self {
        Self {
            selected: Color::LightCyan,
            unselected: Color::Gray,
        }
    }
}

/// Default colours for the delete popup
impl Commands {
    const fn new() -> Self {
        Self {
            background: Color::Reset,
            pause: Color::Yellow,
            restart: Color::Magenta,
            stop: Color::Red,
            delete: Color::Gray,
            resume: Color::Blue,
            start: Color::Green,
        }
    }
}

/// Default colours for the Bandwidth chart
impl ChartBandwidth {
    const fn new() -> Self {
        Self {
            background: Color::Reset,
            border: Color::White,
            max_rx: COLOR_RX,
            title_rx: COLOR_RX,
            title_tx: COLOR_TX,
            max_tx: COLOR_TX,
            points_rx: COLOR_RX,
            points_tx: COLOR_TX,
            y_axis: Color::White,
        }
    }
}

/// Default colours for the CPU chart
impl ChartCpu {
    const fn new() -> Self {
        Self {
            background: Color::Reset,
            border: Color::White,
            title: Color::Green,
            max: ORANGE,
            points: Color::Magenta,
            y_axis: Color::White,
        }
    }
}

/// Default colours for the help popup
impl ChartMemory {
    const fn new() -> Self {
        Self {
            background: Color::Reset,
            border: Color::White,
            title: Color::Green,
            max: ORANGE,
            points: Color::Cyan,
            y_axis: Color::White,
        }
    }
}

/// Default colours for the help popup
impl ChartPorts {
    const fn new() -> Self {
        Self {
            background: Color::Reset,
            border: Color::White,
            title: Color::Green,
            headings: Color::Yellow,
            text: Color::White,
        }
    }
}

/// Default colours for the help popup
impl Containers {
    const fn new() -> Self {
        Self {
            background: Color::Reset,
            icon: Color::White,
            text: Color::Blue,
            text_rx: COLOR_RX,
            text_tx: COLOR_TX,
        }
    }
}

/// Default colours for the help popup
impl ContainerState {
    const fn new() -> Self {
        Self {
            paused: Color::Yellow,
            removing: Color::LightRed,
            restarting: Color::LightGreen,
            running_healthy: Color::Green,
            running_unhealthy: ORANGE,
            dead: Color::Red,
            exited: Color::Red,
            unknown: Color::Red,
        }
    }
}

/// Default colours for the filter panel
impl Filter {
    const fn new() -> Self {
        Self {
            background: Color::Reset,
            highlight: Color::Magenta,
            selected_filter_background: Color::Gray,
            selected_filter_text: Color::Black,
            text: Color::Gray,
        }
    }
}

/// Default colours for the log search
impl LogSearch {
    const fn new() -> Self {
        Self {
            background: Color::Reset,
            highlight: Color::Magenta,
            button_text: Color::Black,
            text: Color::Gray,
        }
    }
}

/// Default colours for the logs panel, only applied if color_logs is false
impl Logs {
    const fn new() -> Self {
        Self {
            background: Color::Reset,
            text: Color::Reset,
        }
    }
}

/// Default colours for the Error popup
impl PopupError {
    const fn new() -> Self {
        Self {
            background: Color::Red,
            text: Color::White,
        }
    }
}

/// Default colours for the info popup
impl PopupInfo {
    const fn new() -> Self {
        Self {
            background: Color::Blue,
            text: Color::White,
        }
    }
}

/// Default colours for the help popup
impl PopupHelp {
    const fn new() -> Self {
        Self {
            background: Color::Magenta,
            text: Color::Black,
            text_highlight: Color::White,
        }
    }
}

/// Default colours for the delete popup
impl PopupDelete {
    const fn new() -> Self {
        Self {
            background: Color::White,
            text: Color::Black,
            text_highlight: Color::Red,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Copy)]
pub struct AppColors {
    pub borders: Borders,
    pub chart_cpu: ChartCpu,
    pub chart_memory: ChartMemory,
    pub chart_bandwidth: ChartBandwidth,
    pub chart_ports: ChartPorts,
    pub commands: Commands,
    pub container_state: ContainerState,
    pub containers: Containers,
    pub log_search: LogSearch,
    pub filter: Filter,
    pub headers_bar: HeadersBar,
    pub logs: Logs,
    pub popup_delete: PopupDelete,
    pub popup_error: PopupError,
    pub popup_help: PopupHelp,
    pub popup_info: PopupInfo,
}

impl AppColors {
    pub const fn new() -> Self {
        Self {
            borders: Borders::new(),
            chart_cpu: ChartCpu::new(),
            chart_memory: ChartMemory::new(),
            chart_bandwidth: ChartBandwidth::new(),
            chart_ports: ChartPorts::new(),
            commands: Commands::new(),
            container_state: ContainerState::new(),
            containers: Containers::new(),
            log_search: LogSearch::new(),
            filter: Filter::new(),
            headers_bar: HeadersBar::new(),
            logs: Logs::new(),
            popup_delete: PopupDelete::new(),
            popup_error: PopupError::new(),
            popup_help: PopupHelp::new(),
            popup_info: PopupInfo::new(),
        }
    }
}