Compare commits
2 Commits
51625ed296
...
50278821ca
| Author | SHA1 | Date | |
|---|---|---|---|
| 50278821ca | |||
| e3510a96fb |
163
src/app.rs
163
src/app.rs
@@ -8,6 +8,8 @@ pub enum Mode {
|
|||||||
Normal,
|
Normal,
|
||||||
/// Active text entry mode for modifying values.
|
/// Active text entry mode for modifying values.
|
||||||
Insert,
|
Insert,
|
||||||
|
/// Active text entry mode for modifying keys.
|
||||||
|
InsertKey,
|
||||||
/// Active search mode for filtering keys.
|
/// Active search mode for filtering keys.
|
||||||
Search,
|
Search,
|
||||||
}
|
}
|
||||||
@@ -145,18 +147,97 @@ impl App {
|
|||||||
/// Updates the input buffer to reflect the value of the currently selected variable.
|
/// Updates the input buffer to reflect the value of the currently selected variable.
|
||||||
pub fn sync_input_with_selected(&mut self) {
|
pub fn sync_input_with_selected(&mut self) {
|
||||||
if let Some(var) = self.vars.get(self.selected) {
|
if let Some(var) = self.vars.get(self.selected) {
|
||||||
let val = var.value.clone().unwrap_or_default();
|
let val = match self.mode {
|
||||||
|
Mode::InsertKey => var.key.clone(),
|
||||||
|
_ => var.value.clone().unwrap_or_default(),
|
||||||
|
};
|
||||||
self.input = Input::new(val);
|
self.input = Input::new(val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Commits the current text in the input buffer back to the selected variable's value.
|
/// Commits the current text in the input buffer back to the selected variable's value or key.
|
||||||
pub fn commit_input(&mut self) {
|
/// Returns true if commit was successful, false if there was an error (e.g. collision).
|
||||||
if let Some(var) = self.vars.get_mut(self.selected)
|
pub fn commit_input(&mut self) -> bool {
|
||||||
&& !var.is_group {
|
match self.mode {
|
||||||
var.value = Some(self.input.value().to_string());
|
Mode::Insert => {
|
||||||
var.status = crate::format::ItemStatus::Modified;
|
if let Some(var) = self.vars.get_mut(self.selected)
|
||||||
|
&& !var.is_group {
|
||||||
|
var.value = Some(self.input.value().to_string());
|
||||||
|
var.status = crate::format::ItemStatus::Modified;
|
||||||
|
}
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
Mode::InsertKey => {
|
||||||
|
let new_key = self.input.value().trim().to_string();
|
||||||
|
if new_key.is_empty() {
|
||||||
|
self.status_message = Some("Key cannot be empty".to_string());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
let selected_var = self.vars[self.selected].clone();
|
||||||
|
if selected_var.key == new_key {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collision check: siblings share the same parent path
|
||||||
|
let parent_path = if selected_var.path.len() > 1 {
|
||||||
|
&selected_var.path[..selected_var.path.len() - 1]
|
||||||
|
} else {
|
||||||
|
&[]
|
||||||
|
};
|
||||||
|
|
||||||
|
let exists = self.vars.iter().enumerate().any(|(i, v)| {
|
||||||
|
i != self.selected
|
||||||
|
&& v.path.len() == selected_var.path.len()
|
||||||
|
&& v.path.starts_with(parent_path)
|
||||||
|
&& v.key == new_key
|
||||||
|
});
|
||||||
|
|
||||||
|
if exists {
|
||||||
|
self.status_message = Some(format!("Key already exists: {}", new_key));
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update selected item's key and path
|
||||||
|
let old_path = selected_var.path.clone();
|
||||||
|
let mut new_path = parent_path.to_vec();
|
||||||
|
new_path.push(PathSegment::Key(new_key.clone()));
|
||||||
|
|
||||||
|
{
|
||||||
|
let var = self.vars.get_mut(self.selected).unwrap();
|
||||||
|
var.key = new_key;
|
||||||
|
var.path = new_path.clone();
|
||||||
|
var.status = crate::format::ItemStatus::Modified;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update paths of all children if it's a group
|
||||||
|
if selected_var.is_group {
|
||||||
|
for var in self.vars.iter_mut() {
|
||||||
|
if var.path.starts_with(&old_path) && var.path.len() > old_path.len() {
|
||||||
|
let mut p = new_path.clone();
|
||||||
|
p.extend(var.path[old_path.len()..].iter().cloned());
|
||||||
|
var.path = p;
|
||||||
|
var.status = crate::format::ItemStatus::Modified;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
true
|
||||||
|
}
|
||||||
|
_ => true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Transitions the application into Insert Mode for keys.
|
||||||
|
pub fn enter_insert_key(&mut self) {
|
||||||
|
if !self.vars.is_empty() {
|
||||||
|
if let Some(var) = self.vars.get(self.selected)
|
||||||
|
&& matches!(var.path.last(), Some(PathSegment::Index(_))) {
|
||||||
|
self.status_message = Some("Cannot rename array indices".to_string());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
self.mode = Mode::InsertKey;
|
||||||
|
self.sync_input_with_selected();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Transitions the application into Insert Mode with a specific variant.
|
/// Transitions the application into Insert Mode with a specific variant.
|
||||||
@@ -182,9 +263,17 @@ impl App {
|
|||||||
|
|
||||||
/// Commits the current input and transitions the application into Normal Mode.
|
/// Commits the current input and transitions the application into Normal Mode.
|
||||||
pub fn enter_normal(&mut self) {
|
pub fn enter_normal(&mut self) {
|
||||||
self.commit_input();
|
if self.commit_input() {
|
||||||
self.save_undo_state();
|
self.save_undo_state();
|
||||||
|
self.mode = Mode::Normal;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cancels the current input and transitions the application into Normal Mode.
|
||||||
|
pub fn cancel_insert(&mut self) {
|
||||||
self.mode = Mode::Normal;
|
self.mode = Mode::Normal;
|
||||||
|
self.sync_input_with_selected();
|
||||||
|
self.status_message = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Deletes the currently selected item. If it's a group, deletes all children.
|
/// Deletes the currently selected item. If it's a group, deletes all children.
|
||||||
@@ -248,24 +337,28 @@ impl App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Adds a new item relative to the selected item.
|
/// Adds a new item relative to the selected item.
|
||||||
pub fn add_item(&mut self, after: bool) {
|
pub fn add_item(&mut self, after: bool, is_group: bool) {
|
||||||
if self.vars.is_empty() {
|
if self.vars.is_empty() {
|
||||||
let new_key = "NEW_VAR".to_string();
|
let new_key = "NEW_VAR".to_string();
|
||||||
self.vars.push(ConfigItem {
|
self.vars.push(ConfigItem {
|
||||||
key: new_key.clone(),
|
key: new_key.clone(),
|
||||||
path: vec![PathSegment::Key(new_key)],
|
path: vec![PathSegment::Key(new_key)],
|
||||||
value: Some("".to_string()),
|
value: if is_group { None } else { Some("".to_string()) },
|
||||||
template_value: None,
|
template_value: None,
|
||||||
default_value: None,
|
default_value: None,
|
||||||
depth: 0,
|
depth: 0,
|
||||||
is_group: false,
|
is_group,
|
||||||
status: crate::format::ItemStatus::Modified,
|
status: crate::format::ItemStatus::Modified,
|
||||||
value_type: crate::format::ValueType::String,
|
value_type: if is_group { crate::format::ValueType::Null } else { crate::format::ValueType::String },
|
||||||
});
|
});
|
||||||
self.selected = 0;
|
self.selected = 0;
|
||||||
self.sync_input_with_selected();
|
self.sync_input_with_selected();
|
||||||
self.save_undo_state();
|
self.save_undo_state();
|
||||||
self.enter_insert(InsertVariant::Start);
|
if is_group {
|
||||||
|
self.enter_insert_key();
|
||||||
|
} else {
|
||||||
|
self.enter_insert(InsertVariant::Start);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -335,7 +428,7 @@ impl App {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
let mut count = 1;
|
let mut count = 1;
|
||||||
let mut candidate = "NEW_VAR".to_string();
|
let mut candidate = if is_group { "NEW_GROUP".to_string() } else { "NEW_VAR".to_string() };
|
||||||
let parent_path_slice = new_path.as_slice();
|
let parent_path_slice = new_path.as_slice();
|
||||||
|
|
||||||
while self.vars.iter().any(|v| {
|
while self.vars.iter().any(|v| {
|
||||||
@@ -343,7 +436,7 @@ impl App {
|
|||||||
&& v.path.len() == parent_path_slice.len() + 1
|
&& v.path.len() == parent_path_slice.len() + 1
|
||||||
&& v.key == candidate
|
&& v.key == candidate
|
||||||
}) {
|
}) {
|
||||||
candidate = format!("NEW_VAR_{}", count);
|
candidate = if is_group { format!("NEW_GROUP_{}", count) } else { format!("NEW_VAR_{}", count) };
|
||||||
count += 1;
|
count += 1;
|
||||||
}
|
}
|
||||||
new_path.push(PathSegment::Key(candidate.clone()));
|
new_path.push(PathSegment::Key(candidate.clone()));
|
||||||
@@ -354,23 +447,49 @@ impl App {
|
|||||||
let new_item = ConfigItem {
|
let new_item = ConfigItem {
|
||||||
key: final_key,
|
key: final_key,
|
||||||
path: new_path,
|
path: new_path,
|
||||||
value: Some("".to_string()),
|
value: if is_group { None } else { Some("".to_string()) },
|
||||||
template_value: None,
|
template_value: None,
|
||||||
default_value: None,
|
default_value: None,
|
||||||
depth: new_depth,
|
depth: new_depth,
|
||||||
is_group: false,
|
is_group,
|
||||||
status: crate::format::ItemStatus::Modified,
|
status: crate::format::ItemStatus::Modified,
|
||||||
value_type: crate::format::ValueType::String,
|
value_type: if is_group { crate::format::ValueType::Null } else { crate::format::ValueType::String },
|
||||||
};
|
};
|
||||||
|
|
||||||
self.vars.insert(insert_pos, new_item);
|
self.vars.insert(insert_pos, new_item);
|
||||||
self.selected = insert_pos;
|
self.selected = insert_pos;
|
||||||
self.sync_input_with_selected();
|
if is_array_item {
|
||||||
self.save_undo_state();
|
self.sync_input_with_selected();
|
||||||
self.enter_insert(InsertVariant::Start);
|
self.save_undo_state();
|
||||||
|
self.enter_insert(InsertVariant::Start);
|
||||||
|
} else {
|
||||||
|
self.enter_insert_key();
|
||||||
|
}
|
||||||
self.status_message = None;
|
self.status_message = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Toggles the group status of the currently selected item.
|
||||||
|
pub fn toggle_group_selected(&mut self) {
|
||||||
|
if let Some(var) = self.vars.get_mut(self.selected) {
|
||||||
|
// Cannot toggle array items (always vars)
|
||||||
|
if matches!(var.path.last(), Some(PathSegment::Index(_))) {
|
||||||
|
self.status_message = Some("Cannot toggle array items".to_string());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var.is_group = !var.is_group;
|
||||||
|
if var.is_group {
|
||||||
|
var.value = None;
|
||||||
|
var.value_type = crate::format::ValueType::Null;
|
||||||
|
} else {
|
||||||
|
var.value = Some("".to_string());
|
||||||
|
var.value_type = crate::format::ValueType::String;
|
||||||
|
}
|
||||||
|
var.status = crate::format::ItemStatus::Modified;
|
||||||
|
self.sync_input_with_selected();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Status bar helpers
|
/// Status bar helpers
|
||||||
pub fn selected_is_group(&self) -> bool {
|
pub fn selected_is_group(&self) -> bool {
|
||||||
self.vars.get(self.selected).map(|v| v.is_group).unwrap_or(false)
|
self.vars.get(self.selected).map(|v| v.is_group).unwrap_or(false)
|
||||||
|
|||||||
@@ -121,6 +121,10 @@ pub struct KeybindsConfig {
|
|||||||
pub delete_item: String,
|
pub delete_item: String,
|
||||||
pub undo: String,
|
pub undo: String,
|
||||||
pub redo: String,
|
pub redo: String,
|
||||||
|
pub rename: String,
|
||||||
|
pub append_group: String,
|
||||||
|
pub prepend_group: String,
|
||||||
|
pub toggle_group: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for KeybindsConfig {
|
impl Default for KeybindsConfig {
|
||||||
@@ -144,6 +148,10 @@ pub struct KeybindsConfig {
|
|||||||
delete_item: "dd".to_string(),
|
delete_item: "dd".to_string(),
|
||||||
undo: "u".to_string(),
|
undo: "u".to_string(),
|
||||||
redo: "U".to_string(),
|
redo: "U".to_string(),
|
||||||
|
rename: "r".to_string(),
|
||||||
|
append_group: "alt+o".to_string(),
|
||||||
|
prepend_group: "alt+O".to_string(),
|
||||||
|
toggle_group: "t".to_string(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -576,25 +576,43 @@ enabled = true
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_xml_flatten_unflatten() {
|
fn test_group_rename_write() {
|
||||||
let xml_str = "<config><server><port>8080</port><enabled>true</enabled></server></config>";
|
|
||||||
|
|
||||||
let json_val = xml_to_json(xml_str).unwrap();
|
|
||||||
|
|
||||||
let mut vars = Vec::new();
|
let mut vars = Vec::new();
|
||||||
flatten(&json_val, Vec::new(), Some("".to_string()), 0, &mut vars);
|
let json = serde_json::json!({
|
||||||
|
"old_group": {
|
||||||
|
"key": "val"
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
flatten(&json, Vec::new(), Some("".to_string()), 0, &mut vars);
|
||||||
|
assert_eq!(vars.len(), 2);
|
||||||
|
assert_eq!(vars[0].key, "old_group");
|
||||||
|
assert_eq!(vars[0].is_group, true);
|
||||||
|
assert_eq!(vars[1].key, "key");
|
||||||
|
assert_eq!(vars[1].path_string(), "old_group.key");
|
||||||
|
|
||||||
|
// Manually simulate a rename of "old_group" to "new_group"
|
||||||
|
let old_path = vars[0].path.clone();
|
||||||
|
let new_key = "new_group".to_string();
|
||||||
|
let mut new_path = vec![PathSegment::Key(new_key.clone())];
|
||||||
|
|
||||||
|
vars[0].key = new_key;
|
||||||
|
vars[0].path = new_path.clone();
|
||||||
|
|
||||||
|
// Update child path
|
||||||
|
vars[1].path = vec![PathSegment::Key("new_group".to_string()), PathSegment::Key("key".to_string())];
|
||||||
|
|
||||||
|
let handler = HierarchicalHandler::new(FormatType::Json);
|
||||||
let mut root = Value::Object(Map::new());
|
let mut root = Value::Object(Map::new());
|
||||||
for var in vars {
|
for var in &vars {
|
||||||
if !var.is_group {
|
if !var.is_group {
|
||||||
insert_into_value(&mut root, &var.path, var.value.as_deref().unwrap_or(""), var.value_type);
|
insert_into_value(&mut root, &var.path, var.value.as_deref().unwrap_or(""), var.value_type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let unflattened_xml = json_to_xml(&root);
|
let out = serde_json::to_string(&root).unwrap();
|
||||||
|
assert!(out.contains("\"new_group\""));
|
||||||
assert!(unflattened_xml.contains("<port>8080</port>"));
|
assert!(out.contains("\"key\":\"val\""));
|
||||||
assert!(unflattened_xml.contains("<enabled>true</enabled>"));
|
assert!(!out.contains("\"old_group\""));
|
||||||
assert!(unflattened_xml.contains("<config>") && unflattened_xml.contains("</config>"));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -80,15 +80,46 @@ mod tests {
|
|||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_ini() {
|
fn test_section_rename_write() {
|
||||||
let mut file = NamedTempFile::new().unwrap();
|
|
||||||
writeln!(file, "[server]\nport=8080\n[database]\nhost=localhost").unwrap();
|
|
||||||
|
|
||||||
let handler = IniHandler;
|
let handler = IniHandler;
|
||||||
let vars = handler.parse(file.path()).unwrap();
|
let mut vars = vec![
|
||||||
|
ConfigItem {
|
||||||
|
key: "server".to_string(),
|
||||||
|
path: vec![PathSegment::Key("server".to_string())],
|
||||||
|
value: None,
|
||||||
|
template_value: None,
|
||||||
|
default_value: None,
|
||||||
|
depth: 0,
|
||||||
|
is_group: true,
|
||||||
|
status: ItemStatus::Present,
|
||||||
|
value_type: ValueType::Null,
|
||||||
|
},
|
||||||
|
ConfigItem {
|
||||||
|
key: "port".to_string(),
|
||||||
|
path: vec![PathSegment::Key("server".to_string()), PathSegment::Key("port".to_string())],
|
||||||
|
value: Some("8080".to_string()),
|
||||||
|
template_value: Some("8080".to_string()),
|
||||||
|
default_value: Some("8080".to_string()),
|
||||||
|
depth: 1,
|
||||||
|
is_group: false,
|
||||||
|
status: ItemStatus::Present,
|
||||||
|
value_type: ValueType::String,
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
assert!(vars.iter().any(|v| v.path_string() == "server" && v.is_group));
|
// Rename "server" to "srv"
|
||||||
assert!(vars.iter().any(|v| v.path_string() == "server.port" && v.value.as_deref() == Some("8080")));
|
vars[0].key = "srv".to_string();
|
||||||
assert!(vars.iter().any(|v| v.path_string() == "database.host" && v.value.as_deref() == Some("localhost")));
|
vars[0].path = vec![PathSegment::Key("srv".to_string())];
|
||||||
|
|
||||||
|
// Update child path
|
||||||
|
vars[1].path = vec![PathSegment::Key("srv".to_string()), PathSegment::Key("port".to_string())];
|
||||||
|
|
||||||
|
let file = NamedTempFile::new().unwrap();
|
||||||
|
handler.write(file.path(), &vars).unwrap();
|
||||||
|
|
||||||
|
let content = std::fs::read_to_string(file.path()).unwrap();
|
||||||
|
assert!(content.contains("[srv]"));
|
||||||
|
assert!(content.contains("port=8080"));
|
||||||
|
assert!(!content.contains("[server]"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -89,14 +89,45 @@ mod tests {
|
|||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_properties() {
|
fn test_group_rename_write() {
|
||||||
let mut file = NamedTempFile::new().unwrap();
|
|
||||||
writeln!(file, "server.port=8080\ndatabase.host=localhost").unwrap();
|
|
||||||
|
|
||||||
let handler = PropertiesHandler;
|
let handler = PropertiesHandler;
|
||||||
let vars = handler.parse(file.path()).unwrap();
|
let mut vars = vec![
|
||||||
|
ConfigItem {
|
||||||
|
key: "server".to_string(),
|
||||||
|
path: vec![PathSegment::Key("server".to_string())],
|
||||||
|
value: None,
|
||||||
|
template_value: None,
|
||||||
|
default_value: None,
|
||||||
|
depth: 0,
|
||||||
|
is_group: true,
|
||||||
|
status: ItemStatus::Present,
|
||||||
|
value_type: ValueType::Null,
|
||||||
|
},
|
||||||
|
ConfigItem {
|
||||||
|
key: "port".to_string(),
|
||||||
|
path: vec![PathSegment::Key("server".to_string()), PathSegment::Key("port".to_string())],
|
||||||
|
value: Some("8080".to_string()),
|
||||||
|
template_value: Some("8080".to_string()),
|
||||||
|
default_value: Some("8080".to_string()),
|
||||||
|
depth: 1,
|
||||||
|
is_group: false,
|
||||||
|
status: ItemStatus::Present,
|
||||||
|
value_type: ValueType::String,
|
||||||
|
}
|
||||||
|
];
|
||||||
|
|
||||||
assert!(vars.iter().any(|v| v.path_string() == "server" && v.is_group));
|
// Rename "server" to "srv"
|
||||||
assert!(vars.iter().any(|v| v.path_string() == "server.port" && v.value.as_deref() == Some("8080")));
|
vars[0].key = "srv".to_string();
|
||||||
|
vars[0].path = vec![PathSegment::Key("srv".to_string())];
|
||||||
|
|
||||||
|
// Update child path
|
||||||
|
vars[1].path = vec![PathSegment::Key("srv".to_string()), PathSegment::Key("port".to_string())];
|
||||||
|
|
||||||
|
let file = NamedTempFile::new().unwrap();
|
||||||
|
handler.write(file.path(), &vars).unwrap();
|
||||||
|
|
||||||
|
let content = std::fs::read_to_string(file.path()).unwrap();
|
||||||
|
assert!(content.contains("srv.port=8080"));
|
||||||
|
assert!(!content.contains("server.port=8080"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ where
|
|||||||
match self.app.mode {
|
match self.app.mode {
|
||||||
Mode::Normal => self.handle_normal_mode(key),
|
Mode::Normal => self.handle_normal_mode(key),
|
||||||
Mode::Insert => self.handle_insert_mode(key),
|
Mode::Insert => self.handle_insert_mode(key),
|
||||||
|
Mode::InsertKey => self.handle_insert_key_mode(key),
|
||||||
Mode::Search => self.handle_search_mode(key),
|
Mode::Search => self.handle_search_mode(key),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -109,8 +110,19 @@ where
|
|||||||
|
|
||||||
/// Handles primary navigation (j/k) and transitions to insert or command modes.
|
/// Handles primary navigation (j/k) and transitions to insert or command modes.
|
||||||
fn handle_navigation_mode(&mut self, key: KeyEvent) -> io::Result<()> {
|
fn handle_navigation_mode(&mut self, key: KeyEvent) -> io::Result<()> {
|
||||||
if let KeyCode::Char(c) = key.code {
|
let key_str = if let KeyCode::Char(c) = key.code {
|
||||||
self.key_sequence.push(c);
|
let mut s = String::new();
|
||||||
|
if key.modifiers.contains(event::KeyModifiers::ALT) {
|
||||||
|
s.push_str("alt+");
|
||||||
|
}
|
||||||
|
s.push(c);
|
||||||
|
s
|
||||||
|
} else {
|
||||||
|
String::new()
|
||||||
|
};
|
||||||
|
|
||||||
|
if !key_str.is_empty() {
|
||||||
|
self.key_sequence.push_str(&key_str);
|
||||||
|
|
||||||
// Collect all configured keybinds
|
// Collect all configured keybinds
|
||||||
let binds = [
|
let binds = [
|
||||||
@@ -129,6 +141,10 @@ where
|
|||||||
(&self.config.keybinds.delete_item, "delete_item"),
|
(&self.config.keybinds.delete_item, "delete_item"),
|
||||||
(&self.config.keybinds.undo, "undo"),
|
(&self.config.keybinds.undo, "undo"),
|
||||||
(&self.config.keybinds.redo, "redo"),
|
(&self.config.keybinds.redo, "redo"),
|
||||||
|
(&self.config.keybinds.rename, "rename"),
|
||||||
|
(&self.config.keybinds.append_group, "append_group"),
|
||||||
|
(&self.config.keybinds.prepend_group, "prepend_group"),
|
||||||
|
(&self.config.keybinds.toggle_group, "toggle_group"),
|
||||||
(&"a".to_string(), "add_missing"),
|
(&"a".to_string(), "add_missing"),
|
||||||
(&":".to_string(), "command"),
|
(&":".to_string(), "command"),
|
||||||
(&"q".to_string(), "quit"),
|
(&"q".to_string(), "quit"),
|
||||||
@@ -163,11 +179,18 @@ where
|
|||||||
"previous_match" => self.app.jump_previous_match(),
|
"previous_match" => self.app.jump_previous_match(),
|
||||||
"jump_top" => self.app.jump_top(),
|
"jump_top" => self.app.jump_top(),
|
||||||
"jump_bottom" => self.app.jump_bottom(),
|
"jump_bottom" => self.app.jump_bottom(),
|
||||||
"append_item" => self.app.add_item(true),
|
"append_item" => self.app.add_item(true, false),
|
||||||
"prepend_item" => self.app.add_item(false),
|
"prepend_item" => self.app.add_item(false, false),
|
||||||
"delete_item" => self.app.delete_selected(),
|
"delete_item" => self.app.delete_selected(),
|
||||||
"undo" => self.app.undo(),
|
"undo" => self.app.undo(),
|
||||||
"redo" => self.app.redo(),
|
"redo" => self.app.redo(),
|
||||||
|
"rename" => self.app.enter_insert_key(),
|
||||||
|
"append_group" => self.app.add_item(true, true),
|
||||||
|
"prepend_group" => self.app.add_item(false, true),
|
||||||
|
"toggle_group" => {
|
||||||
|
self.app.toggle_group_selected();
|
||||||
|
self.app.save_undo_state();
|
||||||
|
}
|
||||||
"add_missing" => {
|
"add_missing" => {
|
||||||
self.add_missing_item();
|
self.add_missing_item();
|
||||||
}
|
}
|
||||||
@@ -179,9 +202,8 @@ where
|
|||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
} else if !prefix_match {
|
} else if !prefix_match {
|
||||||
// Not an exact match and not a prefix for any bind, clear and restart seq
|
|
||||||
self.key_sequence.clear();
|
self.key_sequence.clear();
|
||||||
self.key_sequence.push(c);
|
self.key_sequence.push_str(&key_str);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Non-character keys reset the sequence buffer
|
// Non-character keys reset the sequence buffer
|
||||||
@@ -213,7 +235,26 @@ where
|
|||||||
/// Delegates key events to the `tui_input` handler during active editing.
|
/// Delegates key events to the `tui_input` handler during active editing.
|
||||||
fn handle_insert_mode(&mut self, key: KeyEvent) -> io::Result<()> {
|
fn handle_insert_mode(&mut self, key: KeyEvent) -> io::Result<()> {
|
||||||
match key.code {
|
match key.code {
|
||||||
KeyCode::Esc | KeyCode::Enter => {
|
KeyCode::Esc => {
|
||||||
|
self.app.cancel_insert();
|
||||||
|
}
|
||||||
|
KeyCode::Enter => {
|
||||||
|
self.app.enter_normal();
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
self.app.input.handle_event(&Event::Key(key));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handles keys in InsertKey mode.
|
||||||
|
fn handle_insert_key_mode(&mut self, key: KeyEvent) -> io::Result<()> {
|
||||||
|
match key.code {
|
||||||
|
KeyCode::Esc => {
|
||||||
|
self.app.cancel_insert();
|
||||||
|
}
|
||||||
|
KeyCode::Enter => {
|
||||||
self.app.enter_normal();
|
self.app.enter_normal();
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
|
|||||||
23
src/ui.rs
23
src/ui.rs
@@ -188,7 +188,9 @@ pub fn draw(f: &mut Frame, app: &mut App, config: &Config) {
|
|||||||
let mut extra_info = String::new();
|
let mut extra_info = String::new();
|
||||||
|
|
||||||
if let Some(var) = current_var {
|
if let Some(var) = current_var {
|
||||||
if var.is_group {
|
if matches!(app.mode, Mode::InsertKey) {
|
||||||
|
input_title = format!(" Rename Key: {} ", var.path_string());
|
||||||
|
} else if var.is_group {
|
||||||
input_title = format!(" Group: {} ", var.path_string());
|
input_title = format!(" Group: {} ", var.path_string());
|
||||||
} else {
|
} else {
|
||||||
input_title = format!(" Editing: {} ", var.path_string());
|
input_title = format!(" Editing: {} ", var.path_string());
|
||||||
@@ -199,7 +201,7 @@ pub fn draw(f: &mut Frame, app: &mut App, config: &Config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let input_border_color = match app.mode {
|
let input_border_color = match app.mode {
|
||||||
Mode::Insert => theme.border_active(),
|
Mode::Insert | Mode::InsertKey => theme.border_active(),
|
||||||
Mode::Normal | Mode::Search => theme.border_normal(),
|
Mode::Normal | Mode::Search => theme.border_normal(),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -236,7 +238,7 @@ pub fn draw(f: &mut Frame, app: &mut App, config: &Config) {
|
|||||||
f.render_widget(input, chunks[2]);
|
f.render_widget(input, chunks[2]);
|
||||||
|
|
||||||
// Position the terminal cursor correctly when in Insert mode.
|
// Position the terminal cursor correctly when in Insert mode.
|
||||||
if let Mode::Insert = app.mode {
|
if matches!(app.mode, Mode::Insert) || matches!(app.mode, Mode::InsertKey) {
|
||||||
f.set_cursor_position(ratatui::layout::Position::new(
|
f.set_cursor_position(ratatui::layout::Position::new(
|
||||||
chunks[2].x + 1 + cursor_pos as u16,
|
chunks[2].x + 1 + cursor_pos as u16,
|
||||||
chunks[2].y + 1,
|
chunks[2].y + 1,
|
||||||
@@ -259,6 +261,13 @@ pub fn draw(f: &mut Frame, app: &mut App, config: &Config) {
|
|||||||
.fg(theme.bg_normal())
|
.fg(theme.bg_normal())
|
||||||
.add_modifier(Modifier::BOLD),
|
.add_modifier(Modifier::BOLD),
|
||||||
),
|
),
|
||||||
|
Mode::InsertKey => (
|
||||||
|
" RENAME ",
|
||||||
|
Style::default()
|
||||||
|
.bg(theme.bg_active())
|
||||||
|
.fg(theme.bg_normal())
|
||||||
|
.add_modifier(Modifier::BOLD),
|
||||||
|
),
|
||||||
Mode::Search => (
|
Mode::Search => (
|
||||||
" SEARCH ",
|
" SEARCH ",
|
||||||
Style::default()
|
Style::default()
|
||||||
@@ -282,11 +291,16 @@ pub fn draw(f: &mut Frame, app: &mut App, config: &Config) {
|
|||||||
if !app.selected_is_group() {
|
if !app.selected_is_group() {
|
||||||
parts.push(format!("{}/{}/{} edit", kb.edit, kb.edit_append, kb.edit_substitute));
|
parts.push(format!("{}/{}/{} edit", kb.edit, kb.edit_append, kb.edit_substitute));
|
||||||
}
|
}
|
||||||
|
parts.push(format!("{} rename", kb.rename));
|
||||||
|
parts.push(format!("{} toggle", kb.toggle_group));
|
||||||
if app.selected_is_missing() {
|
if app.selected_is_missing() {
|
||||||
parts.push(format!("{} add", "a")); // 'a' is currently hardcoded in runner
|
parts.push(format!("{} add", "a")); // 'a' is currently hardcoded in runner
|
||||||
}
|
}
|
||||||
if app.selected_is_array() {
|
if app.selected_is_array() {
|
||||||
parts.push(format!("{}/{} array", kb.append_item, kb.prepend_item));
|
parts.push(format!("{}/{} array", kb.append_item, kb.prepend_item));
|
||||||
|
} else {
|
||||||
|
parts.push(format!("{}/{} add", kb.append_item, kb.prepend_item));
|
||||||
|
parts.push(format!("{}/{} group", kb.append_group, kb.prepend_group));
|
||||||
}
|
}
|
||||||
parts.push(format!("{} del", kb.delete_item));
|
parts.push(format!("{} del", kb.delete_item));
|
||||||
parts.push(format!("{} undo", kb.undo));
|
parts.push(format!("{} undo", kb.undo));
|
||||||
@@ -294,7 +308,8 @@ pub fn draw(f: &mut Frame, app: &mut App, config: &Config) {
|
|||||||
parts.push(format!("{} quit", kb.quit));
|
parts.push(format!("{} quit", kb.quit));
|
||||||
parts.join(" · ")
|
parts.join(" · ")
|
||||||
}
|
}
|
||||||
Mode::Insert => "Esc normal · Enter commit".to_string(),
|
Mode::Insert => "Esc cancel · Enter commit".to_string(),
|
||||||
|
Mode::InsertKey => "Esc cancel · Enter rename".to_string(),
|
||||||
Mode::Search => "Esc normal · type to filter".to_string(),
|
Mode::Search => "Esc normal · type to filter".to_string(),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user