Skip to content

Commit

Permalink
refactored tui to a module, refactored colors
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethlove committed Aug 20, 2024
1 parent 671744a commit a605ec3
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 387 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ categories = ["command-line-utilities"]
ansi_term = "0.12.1"
assert_cmd = "2.0.15"
assert_fs = "1.1.2"
catppuccin = { version = "2.4.0", features = ["ratatui"] }
catppuccin = { version = "2.4.0", features = ["ansi-term", "ratatui"] }
chrono = { version = "0.4.38", features = ["serde"] }
clap = { version = "4.5.11", features = ["derive"] }
console = "0.15.8"
Expand Down
180 changes: 0 additions & 180 deletions assets/fileexplorer.css

This file was deleted.

12 changes: 4 additions & 8 deletions src/cli/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::path::Path;

use ansi_term::{Color, Style};
use catppuccin::PALETTE;
use ansi_term::Style;
use clap::{Parser, Subcommand};
use console::Emoji;
use dirs;
Expand Down Expand Up @@ -136,16 +135,13 @@ pub fn get_database_url() -> String {
path.to_string_lossy().to_string()
}

const fn ansi(color: &catppuccin::Color) -> ansi_term::Colour {
ansi_term::Colour::RGB(color.rgb.r, color.rgb.g, color.rgb.b)
}

/// Parses command line options
pub fn parse() {
let app_styles = crate::color::AppStyles::new();
let cli = Cli::parse();
let db_url = get_database_url();
let mut db = Database::new(&db_url).expect("Could not load database");
let response_style = Style::new().bold().fg(ansi(&PALETTE.mocha.colors.mauve));
let response_style = Style::new().bold().fg(app_styles.response_fg.into());
match &cli.command {
Commands::Add { task, frequency } => match frequency {
Frequency::Daily => {
Expand Down Expand Up @@ -223,7 +219,7 @@ pub fn parse() {
Err(e) => {
let response = Style::new()
.bold()
.fg(Color::Red)
.fg(app_styles.response_error_fg)
.paint("Error checking in:");
eprintln!("{response} {}", e)
}
Expand Down
7 changes: 4 additions & 3 deletions src/cli/table.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::color::AppStyles;
use crate::streak::Streak;
use ansi_term::Style;
use tabled::{builder::Builder, settings::Style as TabledStyle};
use term_size::dimensions;

use crate::streak::Streak;

/// Builds table of streaks from list
pub fn build_table(streaks: Vec<Streak>) -> String {
let app_styles = AppStyles::new();
let mut builder = Builder::new();
let header_style = Style::new().italic();
let header_style = Style::new().italic().fg(app_styles.table_header_fg);
builder.push_record([
header_style.paint("\nIdent").to_string(),
header_style.paint("\nTask").to_string(),
Expand Down
66 changes: 66 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use catppuccin::{self, Flavor};
use ratatui::prelude::*;
static PALETTE: Flavor = catppuccin::PALETTE.mocha;

const fn ansi(color: &catppuccin::Color) -> ansi_term::Color {
ansi_term::Colour::RGB(color.rgb.r, color.rgb.g, color.rgb.b)
}

const fn rgb(color: &catppuccin::Color) -> Color {
Color::Rgb(color.rgb.r, color.rgb.g, color.rgb.b)
}

#[derive(Clone, Debug)]
pub struct AppStyles {
pub background: Color,
pub foreground: Color,
pub danger: Color,
pub row_bg: Color,
pub alt_row_bg: Color,
pub row_fg: Color,
pub alt_row_fg: Color,
pub highlight_bg: Color,
pub highlight_fg: Color,
pub tab_fg: Color,
pub selected_tab_fg: Color,
pub response_fg: ansi_term::Color,
pub response_error_fg: ansi_term::Color,
pub table_header_fg: ansi_term::Color,
}

impl AppStyles {
pub fn new() -> Self {
let peach = rgb(&PALETTE.colors.peach);
let text = rgb(&PALETTE.colors.text);
let base = rgb(&PALETTE.colors.base);

AppStyles {
background: base,
foreground: text,
danger: Color::Rgb(
PALETTE.colors.red.rgb.r,
PALETTE.colors.red.rgb.g,
PALETTE.colors.red.rgb.b,
),
row_bg: Color::Rgb(
PALETTE.colors.surface0.rgb.r,
PALETTE.colors.surface0.rgb.g,
PALETTE.colors.surface0.rgb.b,
),
alt_row_bg: Color::Rgb(
PALETTE.colors.surface1.rgb.r,
PALETTE.colors.surface1.rgb.g,
PALETTE.colors.surface1.rgb.b,
),
row_fg: text,
alt_row_fg: text,
highlight_bg: peach,
highlight_fg: base,
tab_fg: text,
selected_tab_fg: peach,
response_fg: ansi(&PALETTE.colors.peach),
response_error_fg: ansi(&PALETTE.colors.red),
table_header_fg: ansi(&PALETTE.colors.peach),
}
}
}
Loading

0 comments on commit a605ec3

Please sign in to comment.