Skip to content

Commit

Permalink
32 theme (#33)
Browse files Browse the repository at this point in the history
feat: configurable theme

- assign theme in ~/.config/ratisui/config.ron: theme: Some("theme name") without .ron extension.
- read theme from ~/.config/ratisui/theme/*.ron if exists.
- color fallback to build in Light/Dark.
- clap add -T --theme argument.
  • Loading branch information
honhimW authored Dec 12, 2024
1 parent 1294260 commit d5e8066
Show file tree
Hide file tree
Showing 24 changed files with 677 additions and 263 deletions.
3 changes: 1 addition & 2 deletions examples/redis_cli_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,8 @@ fn main() -> Result<()> {

fn get_table(rows: Vec<Row>) -> Table {
let table = Table::new(rows, [Min(1), Length(7), Length(0)])
// .block(Block::bordered().border_type(BorderType::Rounded))
.style(Style::default().bg(tailwind::NEUTRAL.c800))
.highlight_style(Style::default().bg(tailwind::ZINC.c900).bold());
.row_highlight_style(Style::default().bg(tailwind::ZINC.c900).bold());
table
}

Expand Down
21 changes: 21 additions & 0 deletions examples/theme.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use anyhow::Result;
use ratatui::style::palette::tailwind;
use ron::ser::PrettyConfig;
use ratisui::theme::{get_color, Color, Tab, TailwindColor, TailwindPalette, Theme};
use ratisui::theme::TailwindPalette::C100;

fn main() -> Result<()> {
let mut theme = Theme::default();
let mut tab = Tab::default();
theme.tab = tab;

let result = ron::ser::to_string_pretty(&theme, PrettyConfig::default())?;
println!("{}", result);

let color = get_color(|t| &t.tab.explorer.accent);
println!("{:?}", color);

println!("{:?}", Color::hex("ffffff").to_color());

Ok(())
}
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use crate::input::Input;
use anyhow::{Result};
use ratatui::crossterm::event::KeyEvent;
use ratatui::layout::{Constraint, Direction, Layout, Rect};
use ratatui::style::palette::tailwind;
use ratatui::text::Line;
use ratatui::Frame;
use ratatui::style::Color;
use crate::bus::GlobalEvent;
use crate::configuration::{Configuration, Databases};

Expand Down Expand Up @@ -33,7 +33,7 @@ pub enum AppEvent {
}

pub trait TabImplementation: Renderable + Listenable {
fn palette(&self) -> tailwind::Palette;
fn highlight(&self) -> Color;
fn title(&self) -> Line<'static>;
}

Expand Down
10 changes: 8 additions & 2 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
use std::collections::BTreeMap;
use anyhow::Result;
use clap::{arg, ArgMatches, Command};
use std::collections::BTreeMap;

pub fn cli() -> Result<Command> {
let command = Command::new("ratisui")
.about("Redis TUI build on Ratatui")
.args([
arg!(-t --target <TARGET> "named redis target, default read from config file if exist"),
arg!(-T --theme <THEME> "theme configuration file name, under ~/.config/ratisui/theme/<THEME>.ron"),
]);

Ok(command)
}

pub struct AppArguments {
pub target: Option<String>,
pub theme: Option<String>,
}

impl AppArguments {
pub fn from_matches(arg_matches: &ArgMatches) -> Self {
let values = Value::from_matches(arg_matches);
let mut args = Self { target: None };
let mut args = Self { target: None, theme: None };
for (id, value) in values {
if id == "target" {
if let Value::String(s) = value {
args.target = Some(s);
}
} else if id == "theme" {
if let Value::String(s) = value {
args.theme = Some(s);
}
}
}
args
Expand Down
9 changes: 5 additions & 4 deletions src/components/console_output.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::components::console_output::OutputKind::{ERR, STD};
use ratatui::layout::{Position, Rect};
use ratatui::prelude::Text;
use ratatui::style::palette::tailwind;
use ratatui::style::{Style};
use ratatui::widgets::{Paragraph, Wrap};
use ratatui_macros::{line, span};
use std::cmp;
use strum::Display;
use OutputKind::{Else, CMD};
use crate::theme::get_color;

pub struct ConsoleData<'a> {
pub lines: Vec<(OutputKind, String)>,
Expand Down Expand Up @@ -44,9 +44,10 @@ impl ConsoleData<'_> {
let mut text = Text::default();
for (kind, l) in self.lines.iter() {
let new_line = match kind {
CMD => line![span!(Style::default().fg(tailwind::EMERALD.c700); l.clone())],
STD => line![span!(l.clone())],
ERR => line![span!(Style::default().fg(tailwind::ROSE.c700); l.clone())],

CMD => line![span!(Style::default().fg(get_color(|t| &t.tab.cli.console.cmd)); l.clone())],
STD => line![span!(Style::default().fg(get_color(|t| &t.tab.cli.console.out)); l.clone())],
ERR => line![span!(Style::default().fg(get_color(|t| &t.tab.cli.console.err)); l.clone())],
Else(style) => line![span!(*style; l.clone())],
};
text.push_line(new_line);
Expand Down
6 changes: 3 additions & 3 deletions src/components/create_key_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::utils::clean_text_area;
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ratatui::layout::Constraint::{Fill, Length, Percentage};
use ratatui::layout::{Layout, Rect};
use ratatui::style::palette::tailwind;
use ratatui::style::{Style, Stylize};
use ratatui::text::Span;
use ratatui::widgets::{Block, BorderType, Clear};
Expand All @@ -12,6 +11,7 @@ use serde::{Deserialize, Serialize};
use std::cmp;
use strum::{Display, EnumCount, EnumIter, IntoEnumIterator};
use tui_textarea::TextArea;
use ratisui::theme::get_color;

pub struct Form {
title: String,
Expand Down Expand Up @@ -53,14 +53,14 @@ impl Default for Form {
fn default() -> Self {
let mut form = Self {
title: String::from(""),
editing_style: Style::default().fg(tailwind::SKY.c700),
editing_style: Style::default().fg(get_color(|t| &t.editor.editing)),
editing: 0,
key_type: 0,
key_name_text_area: TextArea::default(),
ttl_text_area: TextArea::default(),
};
form.key_name_text_area.set_placeholder_text("must not be blank");
form.key_name_text_area.set_placeholder_style(Style::default().fg(tailwind::RED.c700).dim());
form.key_name_text_area.set_placeholder_style(Style::default().fg(get_color(|t| &t.editor.warning)).dim());
form.key_name_text_area.set_cursor_style(Style::default());
form.ttl_text_area.set_cursor_style(Style::default());
form
Expand Down
6 changes: 3 additions & 3 deletions src/components/database_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use crate::configuration::{Database, Protocol, SshTunnel};
use ratatui::crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use ratatui::layout::Constraint::{Fill, Length, Percentage};
use ratatui::layout::{Layout, Rect};
use ratatui::style::palette::tailwind;
use ratatui::style::{Style, Stylize};
use ratatui::text::Span;
use ratatui::widgets::{Block, BorderType, Clear};
use ratatui::Frame;
use strum::{Display, EnumCount, EnumIter, IntoEnumIterator};
use tui_textarea::TextArea;
use uuid::Uuid;
use crate::theme::get_color;

pub struct Form {
title: String,
Expand Down Expand Up @@ -74,7 +74,7 @@ impl Default for Form {
fn default() -> Self {
let mut form = Self {
title: String::from(""),
editing_style: Style::default().fg(tailwind::SKY.c700),
editing_style: Style::default().fg(get_color(|t| &t.editor.editing)),
editing: 0,
name_text_area: TextArea::default(),
host_text_area: TextArea::default(),
Expand All @@ -92,7 +92,7 @@ impl Default for Form {
ssh_password_text_area: TextArea::default(),
};
form.name_text_area.set_placeholder_text("must not be blank");
form.name_text_area.set_placeholder_style(Style::default().fg(tailwind::RED.c700).dim());
form.name_text_area.set_placeholder_style(Style::default().fg(get_color(|t| &t.editor.warning)).dim());
form.host_text_area.set_placeholder_text("127.0.0.1");
form.port_text_area.set_placeholder_text("6379");
form.username_text_area.set_placeholder_text("");
Expand Down
28 changes: 3 additions & 25 deletions src/components/hash_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
use crate::app::{Listenable, Renderable};
use crate::components::raw_value::raw_value_to_highlight_text;
use crate::components::TableColors;
use anyhow::Result;
use itertools::Itertools;
use ratatui::crossterm::event::KeyEvent;
use ratatui::layout::Constraint::{Length, Min};
use ratatui::{
crossterm::event::{KeyCode, KeyEventKind},
layout::{Margin, Rect},
style::{self, Color, Style, Stylize},
style::{Style, Stylize},
text::{Line, Text},
widgets::{
Cell, HighlightSpacing, Row, Scrollbar, ScrollbarOrientation, ScrollbarState
Expand All @@ -33,33 +34,10 @@ use ratatui::{
use std::borrow::Cow;
use std::cmp;
use std::collections::HashMap;
use style::palette::tailwind;
use unicode_width::UnicodeWidthStr;

const ITEM_HEIGHT: usize = 4;

struct TableColors {
buffer_bg: Color,
header_bg: Color,
header_fg: Color,
row_fg: Color,
normal_row_color: Color,
alt_row_color: Color,
}

impl TableColors {
fn new(color: &tailwind::Palette) -> Self {
Self {
buffer_bg: Color::default(),
header_bg: color.c900,
header_fg: color.c200,
row_fg: color.c200,
normal_row_color: Color::default(),
alt_row_color: color.c950,
}
}
}

pub struct Data {
pub index: String,
pub key: String,
Expand Down Expand Up @@ -106,7 +84,7 @@ impl HashValue {
state: TableState::default().with_selected(0),
longest_item_lens: constraint_len_calculator(&vec),
scroll_state: ScrollbarState::new((vec.len() - 1) * ITEM_HEIGHT),
colors: TableColors::new(&tailwind::GRAY),
colors: TableColors::new(),
items: vec,
}
}
Expand Down
26 changes: 3 additions & 23 deletions src/components/list_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
use crate::app::{Listenable, Renderable};
use crate::components::raw_value::raw_value_to_highlight_text;
use crate::components::TableColors;
use anyhow::Result;
use itertools::Itertools;
use ratatui::crossterm::event::KeyEvent;
use ratatui::layout::Constraint::{Length, Min};
use ratatui::{
crossterm::event::{KeyCode, KeyEventKind},
layout::{Margin, Rect},
style::{self, Color, Style, Stylize},
style::{Style, Stylize},
text::{Line, Text},
widgets::{
Cell, HighlightSpacing, Row, Scrollbar, ScrollbarOrientation, ScrollbarState
Expand All @@ -32,32 +33,11 @@ use ratatui::{
};
use std::borrow::Cow;
use std::cmp;
use style::palette::tailwind;
use unicode_width::UnicodeWidthStr;

const ITEM_HEIGHT: usize = 4;

struct TableColors {
buffer_bg: Color,
header_bg: Color,
header_fg: Color,
row_fg: Color,
normal_row_color: Color,
alt_row_color: Color,
}

impl TableColors {
fn new(color: &tailwind::Palette) -> Self {
Self {
buffer_bg: Color::default(),
header_bg: color.c900,
header_fg: color.c200,
row_fg: color.c200,
normal_row_color: Color::default(),
alt_row_color: color.c950,
}
}
}

pub struct Data {
pub index: String,
Expand Down Expand Up @@ -99,7 +79,7 @@ impl ListValue {
state: TableState::default().with_selected(0),
longest_item_lens: constraint_len_calculator(&vec),
scroll_state: ScrollbarState::new((vec.len().saturating_sub(1)) * ITEM_HEIGHT),
colors: TableColors::new(&tailwind::GRAY),
colors: TableColors::new(),
items: vec,
}
}
Expand Down
27 changes: 26 additions & 1 deletion src/components/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
use ratatui::prelude::Color;
use crate::theme::get_color;

pub mod highlight_value;
pub mod list_table;
pub mod set_table;
Expand All @@ -12,4 +15,26 @@ pub mod create_key_editor;
pub mod console_output;
pub mod redis_cli;
pub mod raw_paragraph;
pub mod stream_view;
pub mod stream_view;

struct TableColors {
buffer_bg: Color,
header_bg: Color,
header_fg: Color,
row_fg: Color,
normal_row_color: Color,
alt_row_color: Color,
}

impl TableColors {
fn new() -> Self {
Self {
buffer_bg: get_color(|t| &t.table.buffer_bg),
header_bg: get_color(|t| &t.table.header_bg),
header_fg: get_color(|t| &t.table.header_fg),
row_fg: get_color(|t| &t.table.row_fg),
normal_row_color: get_color(|t| &t.table.normal_row_color),
alt_row_color: get_color(|t| &t.table.alt_row_color),
}
}
}
18 changes: 9 additions & 9 deletions src/components/raw_value.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::borrow::Cow;
use ratatui::prelude::Text;
use ratatui::style::palette::tailwind;
use ratatui::style::Style;
use ratatui::text::{Line, Span};
use ratisui::theme::get_color;
use crate::utils::ContentType;
use crate::components::highlight_value::{HighlightKind, HighlightProcessor, HighlightText};

Expand All @@ -29,14 +29,14 @@ pub fn raw_value_to_highlight_text_with_content_type(value: Cow<str>, content_ty
for highlight_text in fragments {
let fragment = highlight_text.text.clone();
let style= match highlight_text.kind {
HighlightKind::String => Style::default().fg(tailwind::AMBER.c400),
HighlightKind::Boolean |
HighlightKind::Keyword |
HighlightKind::Constant |
HighlightKind::Null => Style::default().fg(tailwind::ROSE.c600),
HighlightKind::Property => Style::default().fg(tailwind::FUCHSIA.c700),
HighlightKind::Comment => Style::default().fg(tailwind::CYAN.c500),
HighlightKind::Number => Style::default().fg(tailwind::BLUE.c600),
HighlightKind::String => Style::default().fg(get_color(|t| &t.raw.string)),
HighlightKind::Boolean => Style::default().fg(get_color(|t| &t.raw.boolean)),
HighlightKind::Keyword => Style::default().fg(get_color(|t| &t.raw.keyword)),
HighlightKind::Constant => Style::default().fg(get_color(|t| &t.raw.constant)),
HighlightKind::Null => Style::default().fg(get_color(|t| &t.raw.null)),
HighlightKind::Property => Style::default().fg(get_color(|t| &t.raw.property)),
HighlightKind::Comment => Style::default().fg(get_color(|t| &t.raw.comment)),
HighlightKind::Number => Style::default().fg(get_color(|t| &t.raw.number)),
_ => Style::default(),
};

Expand Down
Loading

0 comments on commit d5e8066

Please sign in to comment.