Skip to content

Commit

Permalink
Merge pull request #5 from honhimW/develop
Browse files Browse the repository at this point in the history
Nightly
  • Loading branch information
honhimW authored Sep 14, 2024
2 parents 60b1b68 + da73afc commit 8bfccdc
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 24 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
> Please note that the current project is still in its very early stages of development.
> Since this is my first Rust project, it might be shit👻👻
![gif](./assets/ratisui.gif)

## Quick Start

> build from source
Expand Down
Binary file added assets/ratisui.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
7 changes: 4 additions & 3 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::utils::none_match;
use crate::redis_opt::redis_operations;
use crate::tabs::explorer::ExplorerTab;
use crate::tabs::logger::LoggerTab;
use crate::tabs::profiler::ProfilerTab;
use crate::tabs::cli::CliTab;
use anyhow::Result;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::layout::Constraint::{Fill, Length, Max, Min};
Expand All @@ -27,7 +27,7 @@ pub struct Context {
current_tab: CurrentTab,
current_tab_index: usize,
explorer_tab: ExplorerTab,
profiler_tab: ProfilerTab,
profiler_tab: CliTab,
logger_tab: LoggerTab,
databases: Databases,
server_list: ServerList,
Expand All @@ -49,7 +49,7 @@ impl Context {
current_tab: CurrentTab::Explorer,
current_tab_index: 0,
explorer_tab: ExplorerTab::new(),
profiler_tab: ProfilerTab::default(),
profiler_tab: CliTab::default(),
logger_tab: LoggerTab::new(),
server_list: ServerList::new(&databases),
databases,
Expand Down 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(())
}
}
12 changes: 6 additions & 6 deletions src/tabs/profiler.rs → src/tabs/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,29 @@ use ratatui::widgets::{Block, Padding, Paragraph};
use ratatui::{symbols, Frame};

#[derive(Default, Clone, Copy)]
pub struct ProfilerTab {
pub struct CliTab {

}

impl TabImplementation for ProfilerTab {
impl TabImplementation for CliTab {
fn palette(&self) -> tailwind::Palette {
tailwind::GREEN
}

fn title(&self) -> Line<'static> {
" Profiler "
" CLI "
.fg(tailwind::SLATE.c200)
.bg(self.palette().c900)
.into()
}
}

impl Renderable for ProfilerTab {
impl Renderable for CliTab {
fn render_frame(&mut self, frame: &mut Frame, rect: Rect) -> anyhow::Result<()>
where
Self: Sized,
{
let paragraph = Paragraph::new("This is the profiler tab")
let paragraph = Paragraph::new("This is the CLI tab, TODO")
.block(Block::bordered()
.border_set(symbols::border::PROPORTIONAL_TALL)
.padding(Padding::horizontal(1))
Expand All @@ -39,6 +39,6 @@ impl Renderable for ProfilerTab {
}
}

impl Listenable for ProfilerTab {
impl Listenable for CliTab {

}
2 changes: 1 addition & 1 deletion src/tabs/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl TabImplementation for LoggerTab {
}

fn title(&self) -> Line<'static> {
" Logger "
" Logger "
.fg(tailwind::SLATE.c200)
.bg(self.palette().c900)
.into()
Expand Down
2 changes: 1 addition & 1 deletion src/tabs/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod explorer;
pub mod profiler;
pub mod cli;
pub mod logger;

0 comments on commit 8bfccdc

Please sign in to comment.