Skip to content

Commit

Permalink
feat: save databases configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
honhimW committed Sep 14, 2024
1 parent 60b1b68 commit f0692a5
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 13 deletions.
67 changes: 56 additions & 11 deletions src/components/servers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,32 @@
//! [examples]: https://github.com/ratatui/ratatui/blob/main/examples
//! [examples readme]: https://github.com/ratatui/ratatui/blob/main/examples/README.md

use crate::app::{centered_rect, Listenable, Renderable};
use crate::app::{centered_rect, AppEvent, Listenable, Renderable};
use crate::bus::{publish_msg, Message};
use crate::components::database_editor::Form;
use crate::components::popup::Popup;
use crate::components::raw_value::raw_value_to_highlight_text;
use crate::configuration::{Database, Databases, Protocol, save_database_configuration};
use crate::redis_opt::{redis_operations, switch_client};
use anyhow::{anyhow, Error, Result};
use itertools::Itertools;
use log::info;
use ratatui::buffer::Buffer;
use ratatui::crossterm::event::{KeyEvent, KeyModifiers};
use ratatui::layout::Alignment;
use ratatui::layout::Constraint::{Length, Min};
use ratatui::widgets::block::Position;
use ratatui::widgets::{Block, BorderType, Borders, Clear, Paragraph, Widget};
use ratatui::{crossterm::event::{KeyCode, KeyEventKind}, layout::{Margin, Rect}, style::{self, Color, Style, Stylize}, symbols, text::{Line, Text}, widgets::{
Cell, HighlightSpacing, Row, Scrollbar, ScrollbarOrientation, ScrollbarState
, Table, TableState,
}, Frame};
use std::borrow::Cow;
use std::cmp;
use std::string::ToString;
use log::info;
use ratatui::buffer::Buffer;
use ratatui::layout::Alignment;
use ratatui::widgets::{Block, BorderType, Borders, Clear, Paragraph, Widget};
use ratatui::widgets::block::Position;
use style::palette::tailwind;
use tui_textarea::TextArea;
use unicode_width::UnicodeWidthStr;
use crate::bus::{publish_msg, Message};
use crate::components::database_editor::Form;
use crate::components::popup::Popup;
use crate::configuration::{Database, Databases, Protocol};
use crate::redis_opt::{redis_operations, switch_client};

const PALETTES: [tailwind::Palette; 4] = [
tailwind::BLUE,
Expand Down Expand Up @@ -69,6 +69,7 @@ impl TableColors {
}
}

#[derive(Clone)]
pub struct Data {
pub selected: String,
pub name: String,
Expand Down Expand Up @@ -115,6 +116,8 @@ impl Data {
}

pub struct ServerList {
init_database_name: Option<String>,
have_changed: bool,
show_delete_popup: bool,
show_create_popup: bool,
show_edit_popup: bool,
Expand Down Expand Up @@ -154,7 +157,10 @@ impl ServerList {
x.name.cmp(&x1.name)
});
let default_selected = vec.iter().position(|data| data.selected == "*").unwrap_or(0);
let init_database_name = vec.get(default_selected).map(|data| { data.name.clone() });
Self {
init_database_name,
have_changed: false,
show_delete_popup: false,
show_create_popup: false,
show_edit_popup: false,
Expand Down Expand Up @@ -359,6 +365,7 @@ impl ServerList {
if data.selected == "*" {
let _ = publish_msg(Message::warning("Cannot delete selected server"));
} else {
self.have_changed = true;
self.items.remove(selected);
}
}
Expand Down Expand Up @@ -388,6 +395,7 @@ impl ServerList {
database,
};
self.valid_create(&data)?;
self.have_changed = true;
self.items.push(data);
self.create_form = Form::default().title("New");
self.show_create_popup = false;
Expand Down Expand Up @@ -419,6 +427,7 @@ impl ServerList {
database,
};
self.valid_edit(&data)?;
self.have_changed = true;
if data.selected == "*" {
switch_client(data.name.clone(), &data.database)?;
}
Expand Down Expand Up @@ -453,6 +462,32 @@ impl ServerList {
}
Ok(())
}

fn selected(&self) -> Option<Data> {
if let Some(selected) = self.state.selected() {
let item = self.items.get(selected).clone();
if let Some(data) = item {
let data = data.clone();
return Some(data);
}
}
None
}

fn save(&self) -> Result<()> {
let selected_data = self.selected();
let selected_name = selected_data.map(|data| { data.name });
let default_database_changed = selected_name != self.init_database_name;
if self.have_changed || default_database_changed {
let mut databases = Databases::empty();
databases.default_database = selected_name;
for data_ref in self.items.iter() {
databases.databases.insert(data_ref.name.clone(), data_ref.database.clone());
}
save_database_configuration(&databases)?;
}
Ok(())
}
}

impl Renderable for ServerList {
Expand Down Expand Up @@ -562,6 +597,16 @@ impl Listenable for ServerList {
}
Ok(false)
}

fn on_app_event(&mut self, app_event: AppEvent) -> Result<()> {
match app_event {
AppEvent::Destroy => {
self.save()?;
}
_ => {}
}
Ok(())
}
}

fn constraint_len_calculator(items: &[Data]) -> (u16, u16, u16, u16, u16, u16, u16) {
Expand Down
4 changes: 2 additions & 2 deletions src/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub fn save_configuration(config: &Configuration) -> Result<()> {

pub fn save_database_configuration(databases: &Databases) -> Result<()> {
let db_config_path = get_file_path("databases.toml")?;
let toml_content = toml::to_string(&databases)?;
let toml_content = toml::to_string_pretty(&databases)?;
debug!("{}", &toml_content);
if let Ok(mut file) = File::create(&db_config_path) {
file.write_all(toml_content.as_ref())?;
Expand Down Expand Up @@ -97,7 +97,7 @@ impl Configuration {
}

impl Databases {
fn empty() -> Self {
pub fn empty() -> Self {
Self {
default_database: None,
databases: HashMap::new(),
Expand Down
1 change: 1 addition & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,7 @@ impl Listenable for Context {
self.explorer_tab.on_app_event(app_event.clone())?;
self.profiler_tab.on_app_event(app_event.clone())?;
self.logger_tab.on_app_event(app_event.clone())?;
self.server_list.on_app_event(app_event.clone())?;
Ok(())
}
}

0 comments on commit f0692a5

Please sign in to comment.