Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add settings dialog to Tui #520

Merged
merged 5 commits into from
Apr 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Added support for showing and navigating host detail ([#70](https://github.com/fujiapple852/trippy/issues/70))
- Added `--geoip-mmdb-file` and `--tui-geoip-mode` flags for looking up and displaying GeoIp information from `mmdb`
files ([#503](https://github.com/fujiapple852/trippy/issues/503))
- Added settings dialog and simplified Tui header display ([#521](https://github.com/fujiapple852/trippy/issues/521))

### Changed

Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ Options:
- name: Display the AS name

--tui-geoip-mode <TUI_GEOIP_MODE>
How to render GeoIp information [default: compact]
How to render GeoIp information [default: short]

Possible values:
- off: Do not display GeoIp data
Expand Down Expand Up @@ -537,6 +537,11 @@ line option.
| `samples-chart-color` | The color of the samples chart | `Yellow` |
| `help-dialog-bg-color` | The background color of the help dialog | `Blue` |
| `help-dialog-text-color` | The color of the text in the help dialog | `Gray` |
| `settings-dialog-bg-color` | The background color of the settings dialog | `blue` |
| `settings-tab-text-color` | The color of the text in settings dialog tabs | `green` |
| `settings-table-header-text-color` | The color of text in the settings table header | `black` |
| `settings-table-header-bg-color` | The background color of the settings table header | `white` |
| `settings-table-row-text-color` | The color of text of rows in the settings table | `gray` |

The supported colors are:

Expand All @@ -552,6 +557,7 @@ command line option.
| Command | Description | Default |
|------------------------|-------------------------------------------------|----------|
| `toggle-help` | Toggle help | `h` |
| `toggle-settings` | Toggle settings | `s` |
| `next-hop` | Select next hop | `down` |
| `previous-hop` | Select previous hop | `up` |
| `next-trace` | Select next trace | `right` |
Expand Down
56 changes: 55 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::config::TuiCommandItem::{
AddressModeBoth, AddressModeHost, AddressModeIp, ChartZoomIn, ChartZoomOut, ClearDnsCache,
ClearSelection, ClearTraceData, ContractHosts, ContractHostsMin, ExpandHosts, ExpandHostsMax,
NextHop, NextHopAddress, NextTrace, PreviousHop, PreviousHopAddress, PreviousTrace, Quit,
ToggleASInfo, ToggleChart, ToggleFreeze, ToggleHelp, ToggleHopDetails,
ToggleASInfo, ToggleChart, ToggleFreeze, ToggleHelp, ToggleHopDetails, ToggleSettings,
};
use anyhow::anyhow;
use clap::{Command, CommandFactory, Parser, ValueEnum};
Expand Down Expand Up @@ -521,6 +521,16 @@ pub struct TuiTheme {
pub help_dialog_bg_color: TuiColor,
/// The color of the text in the help dialog.
pub help_dialog_text_color: TuiColor,
/// The background color of the settings dialog.
pub settings_dialog_bg_color: TuiColor,
/// The color of the text in settings dialog tabs.
pub settings_tab_text_color: TuiColor,
/// The color of text in the settings table header.
pub settings_table_header_text_color: TuiColor,
/// The background color of the settings table header.
pub settings_table_header_bg_color: TuiColor,
/// The color of text of rows in the settings table.
pub settings_table_row_text_color: TuiColor,
}

impl From<(HashMap<TuiThemeItem, TuiColor>, ConfigThemeColors)> for TuiTheme {
Expand Down Expand Up @@ -591,6 +601,26 @@ impl From<(HashMap<TuiThemeItem, TuiColor>, ConfigThemeColors)> for TuiTheme {
.get(&TuiThemeItem::HelpDialogTextColor)
.or(cfg.help_dialog_text_color.as_ref())
.unwrap_or(&TuiColor::Gray),
settings_dialog_bg_color: *color_map
.get(&TuiThemeItem::SettingsDialogBgColor)
.or(cfg.settings_dialog_bg_color.as_ref())
.unwrap_or(&TuiColor::Blue),
settings_tab_text_color: *color_map
.get(&TuiThemeItem::SettingsTabTextColor)
.or(cfg.settings_tab_text_color.as_ref())
.unwrap_or(&TuiColor::Green),
settings_table_header_text_color: *color_map
.get(&TuiThemeItem::SettingsTableHeaderTextColor)
.or(cfg.settings_table_header_text_color.as_ref())
.unwrap_or(&TuiColor::Black),
settings_table_header_bg_color: *color_map
.get(&TuiThemeItem::SettingsTableHeaderBgColor)
.or(cfg.settings_table_header_bg_color.as_ref())
.unwrap_or(&TuiColor::White),
settings_table_row_text_color: *color_map
.get(&TuiThemeItem::SettingsTableRowTextColor)
.or(cfg.settings_table_row_text_color.as_ref())
.unwrap_or(&TuiColor::Gray),
}
}
}
Expand Down Expand Up @@ -632,6 +662,16 @@ pub enum TuiThemeItem {
HelpDialogBgColor,
/// The color of the text in the help dialog.
HelpDialogTextColor,
/// The color of the text in settings tabs.
SettingsTabTextColor,
/// The background color of the settings dialog.
SettingsDialogBgColor,
/// The color of text in the settings table header.
SettingsTableHeaderTextColor,
/// The background color of the settings table header.
SettingsTableHeaderBgColor,
/// The color of text of rows in the settings table.
SettingsTableRowTextColor,
}

/// A TUI color.
Expand Down Expand Up @@ -701,6 +741,7 @@ impl TryFrom<&str> for TuiColor {
#[derive(Debug, Clone, Copy)]
pub struct TuiBindings {
pub toggle_help: TuiKeyBinding,
pub toggle_settings: TuiKeyBinding,
pub previous_hop: TuiKeyBinding,
pub next_hop: TuiKeyBinding,
pub previous_trace: TuiKeyBinding,
Expand Down Expand Up @@ -733,6 +774,7 @@ impl TuiBindings {
pub fn find_duplicates(&self) -> Vec<String> {
let (_, duplicates) = [
(self.toggle_help, ToggleHelp),
(self.toggle_settings, ToggleSettings),
(self.previous_hop, PreviousHop),
(self.next_hop, NextHop),
(self.previous_trace, PreviousTrace),
Expand Down Expand Up @@ -787,6 +829,10 @@ impl From<(HashMap<TuiCommandItem, TuiKeyBinding>, ConfigBindings)> for TuiBindi
.get(&ToggleHelp)
.or(cfg.toggle_help.as_ref())
.unwrap_or(&TuiKeyBinding::new(KeyCode::Char('h'))),
toggle_settings: *cmd_items
.get(&ToggleSettings)
.or(cfg.toggle_settings.as_ref())
.unwrap_or(&TuiKeyBinding::new(KeyCode::Char('s'))),
previous_hop: *cmd_items
.get(&PreviousHop)
.or(cfg.previous_hop.as_ref())
Expand Down Expand Up @@ -1112,6 +1158,8 @@ impl Display for TuiKeyBinding {
pub enum TuiCommandItem {
/// Toggle the help dialog.
ToggleHelp,
/// Toggle the settings dialog.
ToggleSettings,
/// Move down to the next hop.
NextHop,
/// Move up to the previous hop.
Expand Down Expand Up @@ -1326,12 +1374,18 @@ pub mod config_file {
pub samples_chart_color: Option<TuiColor>,
pub help_dialog_bg_color: Option<TuiColor>,
pub help_dialog_text_color: Option<TuiColor>,
pub settings_dialog_bg_color: Option<TuiColor>,
pub settings_tab_text_color: Option<TuiColor>,
pub settings_table_header_text_color: Option<TuiColor>,
pub settings_table_header_bg_color: Option<TuiColor>,
pub settings_table_row_text_color: Option<TuiColor>,
}

#[derive(Debug, Default, Deserialize)]
#[serde(rename_all = "kebab-case", deny_unknown_fields)]
pub struct ConfigBindings {
pub toggle_help: Option<TuiKeyBinding>,
pub toggle_settings: Option<TuiKeyBinding>,
pub previous_hop: Option<TuiKeyBinding>,
pub next_hop: Option<TuiKeyBinding>,
pub previous_trace: Option<TuiKeyBinding>,
Expand Down
Loading