mirror of
https://github.com/andwati/ratatui-json-editor.git
synced 2026-11-13 04:05:15 +00:00
completed app.rs
This commit is contained in:
+55
@@ -0,0 +1,55 @@
|
||||
pub enum CurrentScreen {
|
||||
Main,
|
||||
Editing,
|
||||
Exiting,
|
||||
}
|
||||
|
||||
pub enum CurrentlyEditing {
|
||||
Key,
|
||||
Value,
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
pub key_input: String,
|
||||
pub value_input: String,
|
||||
pub pairs: HashMap<String, String>,
|
||||
pub current_screen: CurrentScreen,
|
||||
pub currently_editing: Option<CurrentlyEditing>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new() -> App {
|
||||
App {
|
||||
key_input: String::new(),
|
||||
value_input: String::new(),
|
||||
pairs: HashMap::new(),
|
||||
current_screen: CurrentScreen::Main,
|
||||
currently_editing: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save_key_value(&mut self) {
|
||||
self.pairs
|
||||
.insert(self.key_input.clone(), self.value_input.clone());
|
||||
self.key_input = String::new();
|
||||
self.value_input = String::new();
|
||||
self.currently_editing = None;
|
||||
}
|
||||
|
||||
pub fn toggle_editing(&mut self) {
|
||||
if let Some(edit_mode) = &self.currently_editing {
|
||||
match edit_mode {
|
||||
CurrentlyEditing::Key => self.currently_editing = Some(CurrentlyEditing::Value),
|
||||
CurrentlyEditing::Value => self.currently_editing = Some(CurrentlyEditing::Key),
|
||||
};
|
||||
} else {
|
||||
self.currently_editing = Some(CurrentlyEditing::Key)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print_json(&self) -> serde::Result<()> {
|
||||
let output = serde_json::to_string(&self.pairs)?;
|
||||
println!("{}", output);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user