From 59cf71f857bf5573fbe82f77e147f2645cc59b0a Mon Sep 17 00:00:00 2001 From: Brad Culwell Date: Tue, 25 Jun 2024 11:24:39 +0000 Subject: [PATCH] chore: bump ratatui to v0.27.0 --- Cargo.lock | 48 ++++++++++----- Cargo.toml | 7 +-- src/results.rs | 4 +- src/source/nyaa_html.rs | 2 +- src/source/sukebei_nyaa.rs | 2 +- src/source/torrent_galaxy.rs | 2 +- src/theme.rs | 4 +- src/util.rs | 1 + src/util/conv.rs | 112 +++++++++++++++++++++++++++++++++++ src/widget/batch.rs | 2 +- src/widget/category.rs | 2 +- src/widget/help.rs | 2 +- src/widget/page.rs | 2 +- src/widget/results.rs | 2 +- src/widget/search.rs | 2 +- src/widget/themes.rs | 2 +- src/widget/user.rs | 2 +- 17 files changed, 164 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8e072bb..ceaa4f8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -211,16 +211,6 @@ dependencies = [ "error-code", ] -[[package]] -name = "color-to-tui" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbb7fe3c2fe4e88669ff503b2502dffc2f69125f276ae8ecb79439c3dd22f292" -dependencies = [ - "ratatui", - "serde", -] - [[package]] name = "compact_str" version = "0.7.1" @@ -959,6 +949,15 @@ dependencies = [ "either", ] +[[package]] +name = "itertools" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.11" @@ -1137,7 +1136,6 @@ dependencies = [ "arboard", "base64 0.22.1", "chrono", - "color-to-tui", "crossterm", "directories", "dirs", @@ -1147,7 +1145,7 @@ dependencies = [ "lexopt", "nix", "open", - "ratatui", + "ratatui 0.27.0", "ratatui-image", "reqwest", "rss", @@ -1537,16 +1535,36 @@ name = "ratatui" version = "0.26.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f44c9e68fd46eda15c646fbb85e1040b657a58cdc8c98db1d97a55930d991eef" +dependencies = [ + "bitflags 2.5.0", + "cassowary", + "compact_str", + "itertools 0.12.1", + "lru", + "paste", + "stability", + "strum", + "unicode-segmentation", + "unicode-truncate", + "unicode-width", +] + +[[package]] +name = "ratatui" +version = "0.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d16546c5b5962abf8ce6e2881e722b4e0ae3b6f1a08a26ae3573c55853ca68d3" dependencies = [ "bitflags 2.5.0", "cassowary", "compact_str", "crossterm", - "itertools", + "itertools 0.13.0", "lru", "paste", "stability", "strum", + "strum_macros", "unicode-segmentation", "unicode-truncate", "unicode-width", @@ -1563,7 +1581,7 @@ dependencies = [ "icy_sixel", "image", "rand", - "ratatui", + "ratatui 0.26.3", ] [[package]] @@ -2306,7 +2324,7 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5fbabedabe362c618c714dbefda9927b5afc8e2a8102f47f081089a9019226" dependencies = [ - "itertools", + "itertools 0.12.1", "unicode-width", ] diff --git a/Cargo.toml b/Cargo.toml index cb125db..001df0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ lto = true reqwest = { version = "0.11.27", features = ["cookies", "gzip"], default-features = false } tokio = { version = "1.38.0", features = ["macros", "rt-multi-thread"], default-features = false } urlencoding = "2.1.3" -ratatui = { version = "0.26.3", features = ["crossterm"], default-features = false } +ratatui = { version = "0.27.0", features = ["crossterm"], default-features = false } textwrap = { version = "0.16.1", default-features = false } crossterm = { version = "0.27.0", default-features = false } unicode-width = "0.1.13" @@ -41,13 +41,12 @@ arboard = { version = "3.4", default-features = false } dirs = "5.0.1" shellexpand = "3.1.0" indexmap = { version = "2.2.6", default-features = false } -color-to-tui = { version = "0.3.0", default-features = false } human_bytes = { version = "0.4.3", default-features = false } strum = { version = "0.26.2", default-features = false } -ratatui-image = { version = "1.0.1", optional = true , default-features = false } -image = { version = "0.25.1", optional = true, features = ["png"], default-features = false } base64 = { version = "0.22.1", features = ["alloc"], default-features = false } lexopt = "0.3.0" +ratatui-image = { version = "1.0.1", optional = true , default-features = false } +image = { version = "0.25.1", optional = true, features = ["png"], default-features = false } [lib] name = "nyaa" diff --git a/src/results.rs b/src/results.rs index 1fe24e2..4d93fbf 100644 --- a/src/results.rs +++ b/src/results.rs @@ -156,9 +156,9 @@ pub struct ResultRow { } impl<'a> Stylize<'a, ResultRow> for ResultRow { - fn bg(self, color: ratatui::prelude::Color) -> Self { + fn bg>(self, color: S) -> Self { let mut newself = self; - newself.style = newself.style.bg(color); + newself.style = newself.style.bg(color.into()); newself } diff --git a/src/source/nyaa_html.rs b/src/source/nyaa_html.rs index 978600d..4abd73a 100644 --- a/src/source/nyaa_html.rs +++ b/src/source/nyaa_html.rs @@ -18,7 +18,7 @@ use crate::{ sync::SearchQuery, theme::Theme, util::{ - conv::{shorten_number, to_bytes}, + conv::{color_to_tui, shorten_number, to_bytes}, html::{as_type, attr, inner}, }, widget::sort::{SelectedSort, SortDir}, diff --git a/src/source/sukebei_nyaa.rs b/src/source/sukebei_nyaa.rs index d7df9b0..30b8835 100644 --- a/src/source/sukebei_nyaa.rs +++ b/src/source/sukebei_nyaa.rs @@ -15,7 +15,7 @@ use crate::{ sync::SearchQuery, theme::Theme, util::{ - conv::to_bytes, + conv::{color_to_tui, to_bytes}, html::{attr, inner}, }, widget::sort::{SelectedSort, SortDir}, diff --git a/src/source/torrent_galaxy.rs b/src/source/torrent_galaxy.rs index a9d0555..33858d1 100644 --- a/src/source/torrent_galaxy.rs +++ b/src/source/torrent_galaxy.rs @@ -22,7 +22,7 @@ use crate::{ sync::SearchQuery, theme::Theme, util::{ - conv::{shorten_number, to_bytes}, + conv::{color_to_tui, shorten_number, to_bytes}, html::{as_type, attr, inner}, }, widget::sort::{SelectedSort, SortDir}, diff --git a/src/theme.rs b/src/theme.rs index 1771d3c..e437712 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -5,10 +5,10 @@ use std::{ }; use indexmap::IndexMap; -use ratatui::{style::Color, widgets::BorderType}; +use ratatui::{prelude::Color, widgets::BorderType}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use crate::{app::Context, collection, config, source::SourceTheme}; +use crate::{app::Context, collection, config, source::SourceTheme, util::conv::color_to_tui}; pub static THEMES_PATH: &str = "themes"; diff --git a/src/util.rs b/src/util.rs index 3440b5c..9d78eb0 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,6 @@ pub mod cmd; pub mod conv; pub mod html; +pub mod strings; pub mod term; pub mod types; diff --git a/src/util/conv.rs b/src/util/conv.rs index 0a63b14..9de9cf1 100644 --- a/src/util/conv.rs +++ b/src/util/conv.rs @@ -125,3 +125,115 @@ pub fn key_to_string(key: KeyCode, modifier: KeyModifiers) -> String { }; format!("<{}{}>", modifier, key) } + +// From https://github.com/uttarayan21/color-to-tui +pub mod color_to_tui { + use ratatui::style::Color; + use serde::{Deserialize as _, Deserializer, Serializer}; + + pub fn serialize(color: &Color, serializer: S) -> Result { + serializer.serialize_str(&match color { + Color::Reset => "Reset".to_string(), + Color::Red => "Red".to_string(), + Color::Green => "Green".to_string(), + Color::Black => "Black".to_string(), + Color::Yellow => "Yellow".to_string(), + Color::Blue => "Blue".to_string(), + Color::Magenta => "Magenta".to_string(), + Color::Cyan => "Cyan".to_string(), + Color::Gray => "Gray".to_string(), + Color::White => "White".to_string(), + + Color::DarkGray => "DarkGray".to_string(), + Color::LightBlue => "LightBlue".to_string(), + Color::LightCyan => "LightCyan".to_string(), + Color::LightGreen => "LightGreen".to_string(), + Color::LightMagenta => "LightMagenta".to_string(), + Color::LightRed => "LightRed".to_string(), + Color::LightYellow => "LightYellow".to_string(), + Color::Indexed(index) => format!("{:03}", index), + Color::Rgb(r, g, b) => format!("#{:02X}{:02X}{:02X}", r, g, b), + }) + } + + pub fn deserialize<'de, D: Deserializer<'de>>(deserializer: D) -> Result { + use serde::de::{Error, Unexpected}; + + let color_string = String::deserialize(deserializer)?; + Ok(match color_string.to_lowercase().as_str() { + "reset" => Color::Reset, + "red" => Color::Red, + "green" => Color::Green, + "black" => Color::Black, + "yellow" => Color::Yellow, + "blue" => Color::Blue, + "magenta" => Color::Magenta, + "cyan" => Color::Cyan, + "gray" => Color::Gray, + "white" => Color::White, + + "darkgray" => Color::DarkGray, + "lightblue" => Color::LightBlue, + "lightcyan" => Color::LightCyan, + "lightgreen" => Color::LightGreen, + "lightmagenta" => Color::LightMagenta, + "lightred" => Color::LightRed, + "lightyellow" => Color::LightYellow, + _ => match color_string.len() { + 3 => { + let index = color_string.parse::(); + if let Ok(index) = index { + Color::Indexed(index) + } else { + return Err(Error::invalid_type( + Unexpected::Bytes(color_string.as_bytes()), + &"u8 index color", + )); + } + } + 4 | 7 => { + if !color_string.starts_with('#') { + return Err(Error::invalid_value( + Unexpected::Char(color_string.chars().next().unwrap()), + &"# at the start", + )); + } + + let color_string = color_string.trim_start_matches('#'); + + let (r, g, b); + + match color_string.len() { + 6 => { + r = u8::from_str_radix(&color_string[0..2], 16); + g = u8::from_str_radix(&color_string[2..4], 16); + b = u8::from_str_radix(&color_string[4..6], 16); + } + 3 => { + r = u8::from_str_radix(&color_string[0..1], 16).map(|r| r * 17); + g = u8::from_str_radix(&color_string[1..2], 16).map(|g| g * 17); + b = u8::from_str_radix(&color_string[2..3], 16).map(|b| b * 17); + } + _ => unreachable!("Can't be reached since already checked"), + } + + match (r, g, b) { + (Ok(r), Ok(g), Ok(b)) => Color::Rgb(r, g, b), + (_, _, _) => { + return Err(Error::invalid_value( + Unexpected::Bytes(color_string.as_bytes()), + &"hex color string", + )); + } + } + } + _ => { + return Err(serde::de::Error::invalid_length( + color_string.len(), + &"color string with length 4 or 7", + )) + } + }, + }) + } +} diff --git a/src/widget/batch.rs b/src/widget/batch.rs index 6c79003..82959e0 100644 --- a/src/widget/batch.rs +++ b/src/widget/batch.rs @@ -83,7 +83,7 @@ impl super::Widget for BatchWidget { StatefulWidget::render(table, area, buf, &mut self.table.state); if ctx.batch.len() + 2 > area.height as usize { let sb = super::scrollbar(ctx, ScrollbarOrientation::VerticalRight); - let sb_area = area.inner(&Margin { + let sb_area = area.inner(Margin { vertical: 1, horizontal: 0, }); diff --git a/src/widget/category.rs b/src/widget/category.rs index 73938b9..efe3d25 100644 --- a/src/widget/category.rs +++ b/src/widget/category.rs @@ -144,7 +144,7 @@ impl Widget for CategoryPopup { StatefulWidget::render(table, center, f.buffer_mut(), &mut self.table.state); let sb = super::scrollbar(ctx, ScrollbarOrientation::VerticalRight); - let sb_area = center.inner(&Margin { + let sb_area = center.inner(Margin { vertical: 1, horizontal: 0, }); diff --git a/src/widget/help.rs b/src/widget/help.rs index 992518d..7dc6b15 100644 --- a/src/widget/help.rs +++ b/src/widget/help.rs @@ -92,7 +92,7 @@ impl Widget for HelpPopup { if self.table.items.len() as u16 + 2 >= center.height { let sb = super::scrollbar(ctx, ScrollbarOrientation::VerticalRight).begin_symbol(Some("")); - let sb_area = center.inner(&Margin { + let sb_area = center.inner(Margin { vertical: 1, horizontal: 0, }); diff --git a/src/widget/page.rs b/src/widget/page.rs index cf7fb1b..924e82a 100644 --- a/src/widget/page.rs +++ b/src/widget/page.rs @@ -39,7 +39,7 @@ impl Widget for PagePopup { super::clear(center, buf, ctx.theme.bg); indicator.render(center, buf); - let input_area = center.inner(&Margin { + let input_area = center.inner(Margin { vertical: 1, horizontal: 1, }); diff --git a/src/widget/results.rs b/src/widget/results.rs index 6e29ec6..d8c78e0 100644 --- a/src/widget/results.rs +++ b/src/widget/results.rs @@ -130,7 +130,7 @@ impl super::Widget for ResultsWidget { }; let sb = super::scrollbar(ctx, ScrollbarOrientation::VerticalRight).begin_symbol(Some("")); - let sb_area = area.inner(&Margin { + let sb_area = area.inner(Margin { vertical: 1, horizontal: 0, }); diff --git a/src/widget/search.rs b/src/widget/search.rs index d52c340..61ce7b4 100644 --- a/src/widget/search.rs +++ b/src/widget/search.rs @@ -35,7 +35,7 @@ impl super::Widget for SearchWidget { let block = border_block(&ctx.theme, ctx.mode == Mode::Search).title(title!("Search")); Clear.render(area, buf); block.render(area, buf); - let input_area = area.inner(&Margin { + let input_area = area.inner(Margin { vertical: 1, horizontal: 1, }); diff --git a/src/widget/themes.rs b/src/widget/themes.rs index f1bd5dd..5f0263d 100644 --- a/src/widget/themes.rs +++ b/src/widget/themes.rs @@ -74,7 +74,7 @@ impl Widget for ThemePopup { // Only show scrollbar if content overflows if ctx.themes.len() as u16 + 1 >= center.height { let sb = super::scrollbar(ctx, ScrollbarOrientation::VerticalRight); - let sb_area = center.inner(&Margin { + let sb_area = center.inner(Margin { vertical: 1, horizontal: 0, }); diff --git a/src/widget/user.rs b/src/widget/user.rs index d4c159f..04fa7e2 100644 --- a/src/widget/user.rs +++ b/src/widget/user.rs @@ -37,7 +37,7 @@ impl Widget for UserPopup { super::clear(center, buf, ctx.theme.bg); indicator.render(center, buf); - let input_area = center.inner(&Margin { + let input_area = center.inner(Margin { vertical: 1, horizontal: 1, });