Skip to content

Commit

Permalink
ensure read-only keys are styled darkgray, use capital C or D to clea…
Browse files Browse the repository at this point in the history
…r or delete a key
  • Loading branch information
aprxi committed Sep 22, 2024
1 parent bef30c9 commit a957f0e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use async_trait::async_trait;
use ratatui::prelude::*;
use serde_json::{json, Value as JsonValue};

use super::profile::{ProfileCreationStep, SubPartCreationState};
use super::provider::ProviderCreationStep;
use super::*;

Expand Down Expand Up @@ -483,9 +482,7 @@ impl ConfigItemManager {
current_tab: ConfigTab,
) -> Result<WindowMode, ApplicationError> {
if let Some(creator) = &mut self.creator {
eprintln!("Handling creation input");
let action = creator.handle_input(key_event).await?;
eprintln!("Action: {:?}", action);
match action {
CreatorAction::Continue => {
Ok(WindowMode::Modal(ModalEvent::UpdateUI))
Expand Down Expand Up @@ -858,8 +855,25 @@ impl ConfigItemManager {

if let Some(item) = item {
let flattened_settings = settings_editor.get_flattened_settings();
let mut items: Vec<ListItem> = flattened_settings
.iter()
let mut visible_items: Vec<(&String, &JsonValue, &usize)> =
Vec::new();
let mut selected_index = 0;
let mut found_selected = false;

for (key, value, depth) in flattened_settings.iter() {
let last_part = key.split('.').last().unwrap_or(key);
if !last_part.starts_with("__") {
if !found_selected && key == &settings_editor.current_field
{
selected_index = visible_items.len();
found_selected = true;
}
visible_items.push((key, value, depth));
}
}

let mut items: Vec<ListItem> = visible_items
.into_iter()
.map(|(key, value, depth)| {
let indent = " ".repeat(*depth);
let is_editable = !key.starts_with("__");
Expand All @@ -879,10 +893,8 @@ impl ConfigItemManager {
Style::default()
.bg(Color::Rgb(40, 40, 40))
.fg(Color::White)
} else if is_editable {
Style::default().fg(Color::Cyan)
} else {
Style::default().fg(Color::DarkGray)
Style::default().fg(Color::Cyan)
};

let key_span = Span::styled(
Expand All @@ -904,7 +916,13 @@ impl ConfigItemManager {
} else {
let mut span =
settings_editor.get_display_value_span(value);
if key == &settings_editor.current_field {
if span.style.fg == Some(Color::DarkGray) {
// dont override style if already set to DarkGray (non-editable)
} else if !is_editable {
// override style for non-editable fields
span.style = Style::default().fg(Color::DarkGray);
} else if key == &settings_editor.current_field {
// override style for (editable) selected field
span.style = Style::default()
.bg(Color::Rgb(40, 40, 40))
.fg(Color::White);
Expand All @@ -915,6 +933,8 @@ impl ConfigItemManager {
ListItem::new(Line::from(vec![key_span, value_span]))
})
.collect();

// Add new key input field if in AddingNewKey mode
if settings_editor.edit_mode == EditMode::AddingNewKey {
let secure_indicator = if settings_editor.is_new_value_secure()
{
Expand All @@ -932,6 +952,7 @@ impl ConfigItemManager {
.bg(Color::Rgb(40, 40, 40))
.fg(Color::White),
)])));
selected_index = items.len() - 1;
}

// Add new value input field if in AddingNewValue mode
Expand All @@ -953,6 +974,7 @@ impl ConfigItemManager {
.bg(Color::Rgb(40, 40, 40))
.fg(Color::White),
)])));
selected_index = items.len() - 1;
}

let title = format!(
Expand All @@ -966,14 +988,7 @@ impl ConfigItemManager {
.highlight_symbol(">> ");

let mut state = ListState::default();
state.select(Some(
flattened_settings
.iter()
.position(|(key, _, _)| {
key == &settings_editor.current_field
})
.unwrap_or(0),
));
state.select(Some(selected_index));

f.render_stateful_widget(list, area, &mut state);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,27 +226,6 @@ impl ProviderCreator {
&mut self,
input: KeyEvent,
) -> Result<CreatorAction<ConfigItem>, ApplicationError> {
// match input.code {
// KeyCode::Esc => {
// if self.current_step == ProviderCreationStep::ConfirmCreate {
// self.text_area = None;
// }
// return self.go_to_previous_step();
// }
// KeyCode::Backspace => match self.current_step {
// ProviderCreationStep::EnterName if !self.name.is_empty() => {
// self.name.pop();
// return Ok(CreatorAction::Continue);
// }
// ProviderCreationStep::ConfirmCreate => {
// self.text_area = None;
// return self.go_to_previous_step();
// }
// _ => return self.go_to_previous_step(),
// },
// _ => {}
// }

match self.current_step {
ProviderCreationStep::EnterName => self.handle_enter_name(input),
ProviderCreationStep::SelectProviderType => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,26 +110,29 @@ impl SettingsEditor {
self.get_nested_value(&self.current_field)
{
match current_value {
JsonValue::Object(obj)
if obj.contains_key("__content") =>
{
let is_encrypted =
obj.contains_key("__encryption_key");
if is_encrypted && !self.show_secure {
// Encrypted value but masked, don't allow editing
(EditMode::NotEditing, true, None)
} else {
// Allow editing for non-encrypted or unmasked encrypted values
self.start_editing();
(EditMode::EditingValue, true, None)
}
}
JsonValue::Object(obj) => {
if obj.is_empty() {
// Empty object, don't expand
(EditMode::NotEditing, false, None)
if obj.contains_key("__content") {
// Check if the field is editable
let is_editable =
!self.current_field.starts_with("__");
let is_encrypted =
obj.contains_key("__encryption_key");

if is_editable {
if is_encrypted && !self.show_secure {
// Encrypted value but masked, don't allow editing
(EditMode::NotEditing, true, None)
} else {
// Allow editing for non-encrypted or unmasked encrypted values
self.start_editing();
(EditMode::EditingValue, true, None)
}
} else {
// Non-editable field, don't allow editing
(EditMode::NotEditing, false, None)
}
} else {
// Non-empty object, expand it
// Regular object, expand it
self.toggle_section_expansion();
(
EditMode::NotEditing,
Expand All @@ -138,24 +141,24 @@ impl SettingsEditor {
)
}
}
JsonValue::Array(arr) => {
if arr.is_empty() {
// Empty array, don't expand
(EditMode::NotEditing, false, None)
} else {
// Non-empty array, expand it
self.toggle_section_expansion();
(
EditMode::NotEditing,
true,
Some(SettingsAction::ToggleSection),
)
}
JsonValue::Array(_) => {
// Array, expand it
self.toggle_section_expansion();
(
EditMode::NotEditing,
true,
Some(SettingsAction::ToggleSection),
)
}
_ => {
// Regular value, start editing
self.start_editing();
(EditMode::EditingValue, true, None)
// Regular value, check if editable
if !self.current_field.starts_with("__") {
self.start_editing();
(EditMode::EditingValue, true, None)
} else {
// Non-editable field, don't allow editing
(EditMode::NotEditing, false, None)
}
}
}
} else {
Expand All @@ -177,12 +180,12 @@ impl SettingsEditor {
true,
Some(SettingsAction::ToggleSecureVisibility),
),
KeyCode::Char('d') | KeyCode::Char('D') => (
KeyCode::Char('D') => (
EditMode::NotEditing,
true,
Some(SettingsAction::DeleteCurrentKey),
),
KeyCode::Char('c') | KeyCode::Char('C') => (
KeyCode::Char('C') => (
EditMode::NotEditing,
true,
Some(SettingsAction::ClearCurrentKey),
Expand Down Expand Up @@ -461,15 +464,27 @@ impl SettingsEditor {
}

pub fn move_selection(&mut self, delta: i32) {
let flattened = self.get_flattened_settings();
let current_index = flattened
let flattened_settings = self.get_flattened_settings();
let current_index = flattened_settings
.iter()
.position(|(key, _, _)| key == &self.current_field)
.unwrap_or(0);
let new_index = (current_index as i32 + delta)
.rem_euclid(flattened.len() as i32)
as usize;
self.current_field = flattened[new_index].0.clone();

let mut new_index = current_index;
let total_items = flattened_settings.len();

for _ in 0..total_items {
new_index = (new_index as i32 + delta)
.rem_euclid(total_items as i32)
as usize;
let (key, _, _) = &flattened_settings[new_index];
let last_part = key.split('.').last().unwrap_or(key);
if !last_part.starts_with("__") {
break;
}
}

self.current_field = flattened_settings[new_index].0.clone();
}

fn start_editing(&mut self) {
Expand Down

0 comments on commit a957f0e

Please sign in to comment.