Skip to content

Commit

Permalink
Clean up picker rendering
Browse files Browse the repository at this point in the history
Make use of the serde support in ratatui to parse colors.
Have the default colors in one place instead of spread out all over the
place.
Move out selection update from the render function.
Move out the matcher update from the render function.
Simplify the selection if statement in render_preview function.
  • Loading branch information
petersimonsson committed Sep 2, 2024
1 parent 33faa01 commit 0b6b4d5
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 139 deletions.
5 changes: 5 additions & 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 @@ -29,7 +29,7 @@ config = { version = "0.14", default-features = false, features = ["toml"] }
toml = "0.8"
dirs = "5.0.1"
nucleo = "0.5.0"
ratatui = "0.28"
ratatui = { version = "0.28", features = ["serde"] }
crossterm = "0.28"
clap_complete = "4.5"

Expand Down
21 changes: 11 additions & 10 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use clap::{Args, Command, CommandFactory, Parser, Subcommand};
use clap_complete::{generate, Generator, Shell};
use error_stack::ResultExt;
use git2::{build::RepoBuilder, FetchOptions, RemoteCallbacks, Repository};
use ratatui::style::Color;

#[derive(Debug, Parser)]
#[command(author, version)]
Expand Down Expand Up @@ -89,19 +90,19 @@ pub struct ConfigCommand {
max_depths: Option<Vec<usize>>,
#[arg(long, value_name = "#rrggbb")]
/// Background color of the highlighted item in the picker
picker_highlight_color: Option<String>,
picker_highlight_color: Option<Color>,
#[arg(long, value_name = "#rrggbb")]
/// Text color of the hightlighted item in the picker
picker_highlight_text_color: Option<String>,
picker_highlight_text_color: Option<Color>,
#[arg(long, value_name = "#rrggbb")]
/// Color of the borders between widgets in the picker
picker_border_color: Option<String>,
picker_border_color: Option<Color>,
#[arg(long, value_name = "#rrggbb")]
/// Color of the item count in the picker
picker_info_color: Option<String>,
picker_info_color: Option<Color>,
#[arg(long, value_name = "#rrggbb")]
/// Color of the prompt in the picker
picker_prompt_color: Option<String>,
picker_prompt_color: Option<Color>,
#[arg(long, value_name = "Alphabetical | LastAttached")]
/// Set the sort order of the sessions in the switch command
session_sort_order: Option<SessionSortOrderConfig>,
Expand Down Expand Up @@ -392,27 +393,27 @@ fn config_command(args: &ConfigCommand, mut config: Config) -> Result<()> {

if let Some(color) = &args.picker_highlight_color {
let mut picker_colors = config.picker_colors.unwrap_or_default();
picker_colors.highlight_color = Some(color.to_string());
picker_colors.highlight_color = Some(*color);
config.picker_colors = Some(picker_colors);
}
if let Some(color) = &args.picker_highlight_text_color {
let mut picker_colors = config.picker_colors.unwrap_or_default();
picker_colors.highlight_text_color = Some(color.to_string());
picker_colors.highlight_text_color = Some(*color);
config.picker_colors = Some(picker_colors);
}
if let Some(color) = &args.picker_border_color {
let mut picker_colors = config.picker_colors.unwrap_or_default();
picker_colors.border_color = Some(color.to_string());
picker_colors.border_color = Some(*color);
config.picker_colors = Some(picker_colors);
}
if let Some(color) = &args.picker_info_color {
let mut picker_colors = config.picker_colors.unwrap_or_default();
picker_colors.info_color = Some(color.to_string());
picker_colors.info_color = Some(*color);
config.picker_colors = Some(picker_colors);
}
if let Some(color) = &args.picker_prompt_color {
let mut picker_colors = config.picker_colors.unwrap_or_default();
picker_colors.prompt_color = Some(color.to_string());
picker_colors.prompt_color = Some(*color);
config.picker_colors = Some(picker_colors);
}

Expand Down
85 changes: 44 additions & 41 deletions src/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use error_stack::ResultExt;
use serde_derive::{Deserialize, Serialize};
use std::{collections::HashMap, env, fmt::Display, fs::canonicalize, io::Write, path::PathBuf};

use ratatui::style::{Color, Style};
use ratatui::style::{Color, Style, Stylize};

use crate::{error::Suggestion, keymap::Keymap};

Expand Down Expand Up @@ -142,8 +142,7 @@ impl Config {
.change_context(ConfigError::IoError)?
.to_string();

let path = canonicalize(expanded_path)
.change_context(ConfigError::IoError)?;
let path = canonicalize(expanded_path).change_context(ConfigError::IoError)?;

Ok(SearchDirectory::new(path, search_dir.depth))
})
Expand Down Expand Up @@ -255,70 +254,74 @@ pub struct Window {
#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct Pane {}

#[derive(Default, Debug, Serialize, Deserialize, PartialEq, Eq)]
#[derive(Clone, Default, Debug, Serialize, Deserialize, PartialEq, Eq)]
pub struct PickerColorConfig {
pub highlight_color: Option<String>,
pub highlight_text_color: Option<String>,
pub border_color: Option<String>,
pub info_color: Option<String>,
pub prompt_color: Option<String>,
pub highlight_color: Option<Color>,
pub highlight_text_color: Option<Color>,
pub border_color: Option<Color>,
pub info_color: Option<Color>,
pub prompt_color: Option<Color>,
}

const HIGHLIGHT_COLOR_DEFAULT: Color = Color::LightBlue;
const HIGHLIGHT_TEXT_COLOR_DEFAULT: Color = Color::Black;
const BORDER_COLOR_DEFAULT: Color = Color::DarkGray;
const INFO_COLOR_DEFAULT: Color = Color::LightYellow;
const PROMPT_COLOR_DEFAULT: Color = Color::LightGreen;

impl PickerColorConfig {
pub fn default_colors() -> Self {
PickerColorConfig {
highlight_color: Some(HIGHLIGHT_COLOR_DEFAULT),
highlight_text_color: Some(HIGHLIGHT_TEXT_COLOR_DEFAULT),
border_color: Some(BORDER_COLOR_DEFAULT),
info_color: Some(INFO_COLOR_DEFAULT),
prompt_color: Some(PROMPT_COLOR_DEFAULT),
}
}

pub fn highlight_style(&self) -> Style {
let mut style = Style::default().bg(Color::LightBlue).fg(Color::Black);
let mut style = Style::default()
.bg(HIGHLIGHT_COLOR_DEFAULT)
.fg(HIGHLIGHT_TEXT_COLOR_DEFAULT)
.bold();

if let Some(color) = &self.highlight_color {
if let Some(color) = rgb_to_color(color) {
style = style.bg(color);
}
if let Some(color) = self.highlight_color {
style = style.bg(color);
}

if let Some(color) = &self.highlight_text_color {
if let Some(color) = rgb_to_color(color) {
style = style.fg(color);
}
if let Some(color) = self.highlight_text_color {
style = style.fg(color);
}

style
}

pub fn border_color(&self) -> Option<Color> {
if let Some(color) = &self.border_color {
rgb_to_color(color)
pub fn border_color(&self) -> Color {
if let Some(color) = self.border_color {
color
} else {
None
BORDER_COLOR_DEFAULT
}
}

pub fn info_color(&self) -> Option<Color> {
if let Some(color) = &self.info_color {
rgb_to_color(color)
pub fn info_color(&self) -> Color {
if let Some(color) = self.info_color {
color
} else {
None
INFO_COLOR_DEFAULT
}
}

pub fn prompt_color(&self) -> Option<Color> {
if let Some(color) = &self.prompt_color {
rgb_to_color(color)
pub fn prompt_color(&self) -> Color {
if let Some(color) = self.prompt_color {
color
} else {
None
PROMPT_COLOR_DEFAULT
}
}
}

fn rgb_to_color(color: &str) -> Option<Color> {
if color.len() == 7 && color.starts_with('#') {
let red = u8::from_str_radix(&color[1..3], 16).ok()?;
let green = u8::from_str_radix(&color[3..5], 16).ok()?;
let blue = u8::from_str_radix(&color[5..7], 16).ok()?;
Some(Color::Rgb(red, green, blue))
} else {
None
}
}

#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
pub enum SessionSortOrderConfig {
Alphabetical,
Expand Down
1 change: 0 additions & 1 deletion src/keymap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use serde::de::Error as DeError;
use serde::ser::Error as SerError;
use serde::{Deserialize, Serialize};
use serde_derive::{Deserialize, Serialize};

use crate::error::TmsError;

Expand Down
Loading

0 comments on commit 0b6b4d5

Please sign in to comment.