diff options
| author | lukeflo | 2024-11-22 11:21:32 +0100 |
|---|---|---|
| committer | lukeflo | 2024-11-22 11:21:32 +0100 |
| commit | 2621a347a0e6f2a2e1b625ca26aad3a0cf8e58f5 (patch) | |
| tree | 51b58d427c930d37d4468e94f6b9fd120da1f45b /src | |
| parent | c05fb327c70eed7dcbc0caccc745e168e6cba5fb (diff) | |
| download | bibiman-2621a347a0e6f2a2e1b625ca26aad3a0cf8e58f5.tar.gz bibiman-2621a347a0e6f2a2e1b625ca26aad3a0cf8e58f5.zip | |
filepath as OsString, new color sheme
Diffstat (limited to 'src')
| -rw-r--r-- | src/app.rs | 46 | ||||
| -rw-r--r-- | src/bibiman/bibisetup.rs | 7 | ||||
| -rw-r--r-- | src/bibiman/entries.rs | 6 | ||||
| -rw-r--r-- | src/main.rs | 8 | ||||
| -rw-r--r-- | src/tui/popup.rs | 8 | ||||
| -rw-r--r-- | src/tui/ui.rs | 54 |
6 files changed, 80 insertions, 49 deletions
@@ -23,6 +23,7 @@ use crate::tui::commands::InputCmdAction; use crate::tui::popup::PopupKind; use crate::tui::{self, Tui}; use crate::{bibiman::Bibiman, tui::commands::CmdAction}; +use std::ffi::OsStr; use std::process::{Command, Stdio}; use tui::Event; use tui_input::backend::crossterm::EventHandler; @@ -236,14 +237,14 @@ impl App { // Choose ressource depending an selected popup field if self.bibiman.popup_area.popup_list[popup_idx].contains("Weblink") { - let object = prepare_weblink( - self.bibiman.entry_table.entry_table_items[entry_idx].doi_url(), - ); - open_connected_res(&object)?; + let object = + self.bibiman.entry_table.entry_table_items[entry_idx].doi_url(); + let url = prepare_weblink(object); + open_connected_link(&url)?; } else if self.bibiman.popup_area.popup_list[popup_idx].contains("File") { let object = self.bibiman.entry_table.entry_table_items[entry_idx].filepath(); - open_connected_res(object)?; + open_connected_file(object)?; } else { eprintln!("Unable to find ressource to open"); }; @@ -322,8 +323,9 @@ impl App { } } -pub fn open_connected_res(object: &str) -> Result<()> { +pub fn open_connected_file(file: &OsStr) -> Result<()> { // Build command to execute pdf-reader. 'xdg-open' is Linux standard + // TODO: make custom opener command possible through config let cmd = { match std::env::consts::OS { "linux" => String::from("xdg-open"), @@ -336,7 +338,7 @@ pub fn open_connected_res(object: &str) -> Result<()> { // Pass filepath as argument, pipe stdout and stderr to /dev/null // to keep the TUI clean (where is it piped on Windows???) let _ = Command::new(&cmd) - .arg(object) + .arg(file) .stdout(Stdio::null()) .stderr(Stdio::null()) .spawn() @@ -345,13 +347,35 @@ pub fn open_connected_res(object: &str) -> Result<()> { Ok(()) } +pub fn open_connected_link(link: &str) -> Result<()> { + // Build command to execute pdf-reader. 'xdg-open' is Linux standard + // TODO: make custom opener command possible through config + let cmd = { + match std::env::consts::OS { + "linux" => String::from("xdg-open"), + "macos" => String::from("open"), + "windows" => String::from("start"), + _ => panic!("Couldn't detect OS for setting correct opener"), + } + }; + + // Pass filepath as argument, pipe stdout and stderr to /dev/null + // to keep the TUI clean (where is it piped on Windows???) + let _ = Command::new(&cmd) + .arg(link) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .wrap_err("Opening link not possible"); + + Ok(()) +} + pub fn prepare_weblink(url: &str) -> String { if url.starts_with("10.") { - let prefix = "https://doi.org/".to_string(); - prefix + url + "https://doi.org/".to_string() + url } else if url.starts_with("www.") { - let prefix = "https://".to_string(); - prefix + url + "https://".to_string() + url } else { url.to_string() } diff --git a/src/bibiman/bibisetup.rs b/src/bibiman/bibisetup.rs index e12aa8f..85438b3 100644 --- a/src/bibiman/bibisetup.rs +++ b/src/bibiman/bibisetup.rs @@ -19,6 +19,7 @@ use biblatex::{self, Bibliography}; use biblatex::{ChunksExt, Type}; use color_eyre::owo_colors::OwoColorize; use itertools::Itertools; +use std::ffi::OsString; use std::{fs, path::PathBuf}; use crate::cliargs; @@ -52,7 +53,7 @@ pub struct BibiData { pub citekey: String, pub abstract_text: String, pub doi_url: Option<String>, - pub filepath: Option<String>, + pub filepath: Option<OsString>, pub subtitle: Option<String>, } @@ -249,9 +250,9 @@ impl BibiSetup { } } - pub fn get_filepath(citekey: &str, biblio: &Bibliography) -> Option<String> { + pub fn get_filepath(citekey: &str, biblio: &Bibliography) -> Option<OsString> { if biblio.get(citekey).unwrap().file().is_ok() { - Some(biblio.get(citekey).unwrap().file().unwrap()) + Some(biblio.get(citekey).unwrap().file().unwrap().into()) } else { None } diff --git a/src/bibiman/entries.rs b/src/bibiman/entries.rs index 75c7db5..34020c8 100644 --- a/src/bibiman/entries.rs +++ b/src/bibiman/entries.rs @@ -15,6 +15,8 @@ // along with this program. If not, see <https://www.gnu.org/licenses/>. ///// +use std::ffi::{OsStr, OsString}; + use crate::bibiman::bibisetup::BibiData; use ratatui::widgets::{ScrollbarState, TableState}; @@ -140,7 +142,7 @@ pub struct EntryTableItem { pub citekey: String, pub abstract_text: String, pub doi_url: Option<String>, - pub filepath: Option<String>, + pub filepath: Option<OsString>, pub subtitle: Option<String>, } @@ -199,7 +201,7 @@ impl EntryTableItem { self.doi_url.as_ref().unwrap() } - pub fn filepath(&self) -> &str { + pub fn filepath(&self) -> &OsStr { self.filepath.as_ref().unwrap() } diff --git a/src/main.rs b/src/main.rs index 8a78d62..95bf16a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,9 +27,11 @@ pub mod errorsetup; pub mod tui; // Color indices -const MAIN_BLUE_COLOR_INDEX: u8 = 75; -const MAIN_PURPLE_COLOR_INDEX: u8 = 135; -const MAIN_GREEN_COLOR_INDEX: u8 = 29; +const MAIN_ENTRY_COLOR_INDEX: u8 = 36; +const MAIN_KEYWORD_COLOR_INDEX: u8 = 101; +const MAIN_INFO_COLOR_INDEX: u8 = 99; +const CONFIRM_COLOR_INDEX: u8 = 47; +const WARN_COLOR_INDEX: u8 = 124; const TEXT_HIGHLIGHT_COLOR_INDEX: u8 = 254; const TEXT_FG_COLOR_INDEX: u8 = 250; diff --git a/src/tui/popup.rs b/src/tui/popup.rs index cd6c680..b0450fa 100644 --- a/src/tui/popup.rs +++ b/src/tui/popup.rs @@ -21,7 +21,7 @@ use ratatui::{ widgets::ListState, }; -use crate::{MAIN_BLUE_COLOR_INDEX, MAIN_PURPLE_COLOR_INDEX}; +use crate::{MAIN_ENTRY_COLOR_INDEX, MAIN_KEYWORD_COLOR_INDEX}; #[derive(Debug)] pub enum PopupKind { @@ -81,20 +81,20 @@ impl PopupArea { helptext.push(Line::from( Span::raw(keys) .bold() - .fg(Color::Indexed(MAIN_BLUE_COLOR_INDEX)), + .fg(Color::Indexed(MAIN_ENTRY_COLOR_INDEX)), )) } else if help == "sub" { helptext.push(Line::from("")); helptext.push(Line::from( Span::raw(keys) .bold() - .fg(Color::Indexed(MAIN_BLUE_COLOR_INDEX)), + .fg(Color::Indexed(MAIN_ENTRY_COLOR_INDEX)), )) } else { helptext.push(Line::from(vec![ Span::raw(keys) .bold() - .fg(Color::Indexed(MAIN_PURPLE_COLOR_INDEX)), + .fg(Color::Indexed(MAIN_KEYWORD_COLOR_INDEX)), Span::raw(help), ])) } diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 1148e57..74b10d7 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -21,8 +21,8 @@ use crate::bibiman::{CurrentArea, FormerArea}; use crate::tui::popup::PopupKind; use crate::App; use crate::{ - MAIN_BLUE_COLOR_INDEX, MAIN_GREEN_COLOR_INDEX, MAIN_PURPLE_COLOR_INDEX, TEXT_FG_COLOR_INDEX, - TEXT_HIGHLIGHT_COLOR_INDEX, + CONFIRM_COLOR_INDEX, MAIN_ENTRY_COLOR_INDEX, MAIN_INFO_COLOR_INDEX, MAIN_KEYWORD_COLOR_INDEX, + TEXT_FG_COLOR_INDEX, TEXT_HIGHLIGHT_COLOR_INDEX, WARN_COLOR_INDEX, }; use ratatui::layout::{Direction, Position}; use ratatui::widgets::Clear; @@ -41,9 +41,11 @@ use ratatui::{ // Text colors const TEXT_FG_COLOR: Color = Color::Indexed(TEXT_FG_COLOR_INDEX); const TEXT_BRIGHT_FG_COLOR: Color = Color::Indexed(TEXT_HIGHLIGHT_COLOR_INDEX); -const MAIN_BLUE: Color = Color::Indexed(MAIN_BLUE_COLOR_INDEX); -const MAIN_PURPLE: Color = Color::Indexed(MAIN_PURPLE_COLOR_INDEX); -const MAIN_GREEN: Color = Color::Indexed(MAIN_GREEN_COLOR_INDEX); +const ENTRY_COLOR: Color = Color::Indexed(MAIN_ENTRY_COLOR_INDEX); +const KEYWORD_COLOR: Color = Color::Indexed(MAIN_KEYWORD_COLOR_INDEX); +const CONFIRM_COLOR: Color = Color::Indexed(CONFIRM_COLOR_INDEX); +const WARN_COLOR: Color = Color::Indexed(WARN_COLOR_INDEX); +const INFO_COLOR: Color = Color::Indexed(MAIN_INFO_COLOR_INDEX); // Background colors const HEADER_FOOTER_BG: Color = Color::Indexed(235); @@ -53,17 +55,17 @@ const POPUP_BG: Color = Color::Indexed(234); // Keyword Box const KEYWORD_BOX_SELECTED_BORDER_STYLE: Style = Style::new().fg(TEXT_BRIGHT_FG_COLOR); const KEYWORD_BOX_SELECTED_TITLE_STYLE: Style = - Style::new().fg(MAIN_PURPLE).add_modifier(Modifier::BOLD); + Style::new().fg(KEYWORD_COLOR).add_modifier(Modifier::BOLD); const KEYWORD_BOX_UNSELECTED_BORDER_STYLE: Style = Style::new().fg(TEXT_FG_COLOR); const KEYWORD_BOX_UNSELECTED_TITLE_STYLE: Style = - Style::new().fg(MAIN_PURPLE).add_modifier(Modifier::BOLD); + Style::new().fg(KEYWORD_COLOR).add_modifier(Modifier::BOLD); // Entry box const ENTRY_BOX_SELECTED_BORDER_STYLE: Style = Style::new().fg(TEXT_BRIGHT_FG_COLOR); const ENTRY_BOX_SELECTED_TITLE_STYLE: Style = - Style::new().fg(MAIN_BLUE).add_modifier(Modifier::BOLD); + Style::new().fg(ENTRY_COLOR).add_modifier(Modifier::BOLD); const ENTRY_BOX_UNSELECTED_BORDER_STYLE: Style = Style::new().fg(TEXT_FG_COLOR); const ENTRY_BOX_UNSELECTED_TITLE_STYLE: Style = - Style::new().fg(MAIN_BLUE).add_modifier(Modifier::BOLD); + Style::new().fg(ENTRY_COLOR).add_modifier(Modifier::BOLD); // Default box // const BOX_SELECTED_BORDER_STYLE: Style = Style::new().fg(TEXT_BRIGHT_FG_COLOR); const BOX_SELECTED_TITLE_STYLE: Style = Style::new() @@ -77,15 +79,15 @@ const POPUP_HELP_BOX: Style = Style::new().fg(TEXT_FG_COLOR).bg(POPUP_BG); // Entry table styles const ENTRY_SELECTED_ROW_STYLE: Style = Style::new() - .fg(MAIN_BLUE) + .fg(ENTRY_COLOR) .add_modifier(Modifier::BOLD) .add_modifier(Modifier::REVERSED); const KEYWORD_SELECTED_ROW_STYLE: Style = Style::new() - .fg(MAIN_PURPLE) + .fg(KEYWORD_COLOR) .add_modifier(Modifier::BOLD) .add_modifier(Modifier::REVERSED); const SELECTION_SELECTED_ROW_STYLE: Style = Style::new() - // .fg(MAIN_BLUE) + // .fg(ENTRY_COLOR) .add_modifier(Modifier::BOLD) .add_modifier(Modifier::REVERSED); const SELECTED_TABLE_COL_STYLE: Style = Style::new().add_modifier(Modifier::BOLD); @@ -98,9 +100,9 @@ const SCROLLBAR_UPPER_CORNER: Option<&str> = Some("┓"); const SCROLLBAR_LOWER_CORNER: Option<&str> = Some("┛"); // Info area styles -const INFO_STYLE_AUTHOR: Style = Style::new().fg(MAIN_GREEN); -const INFO_STYLE_TITLE: Style = Style::new().fg(MAIN_BLUE).add_modifier(Modifier::ITALIC); -const INFO_STYLE_YEAR: Style = Style::new().fg(MAIN_PURPLE); +const INFO_STYLE_AUTHOR: Style = Style::new().fg(INFO_COLOR); +const INFO_STYLE_TITLE: Style = Style::new().fg(ENTRY_COLOR).add_modifier(Modifier::ITALIC); +const INFO_STYLE_YEAR: Style = Style::new().fg(KEYWORD_COLOR); const INFO_STYLE_DOI: Style = Style::new().fg(TEXT_FG_COLOR); const INFO_STYLE_FILE: Style = Style::new().fg(TEXT_FG_COLOR); const INFO_STYLE_ABSTRACT: Style = Style::new().fg(TEXT_FG_COLOR); @@ -168,7 +170,7 @@ pub fn render_popup(app: &mut App, frame: &mut Frame) { .title_alignment(Alignment::Center) .style(POPUP_HELP_BOX) .border_set(symbols::border::THICK) - .border_style(Style::new().fg(MAIN_BLUE)); + .border_style(Style::new().fg(ENTRY_COLOR)); let text: Text = PopupArea::popup_help(); @@ -199,13 +201,13 @@ pub fn render_popup(app: &mut App, frame: &mut Frame) { let area = frame.area(); let block = Block::bordered() - .title_top(" Message ".bold().fg(MAIN_GREEN)) - .border_style(Style::new().fg(MAIN_GREEN)) + .title_top(" Message ".bold().fg(CONFIRM_COLOR)) + .border_style(Style::new().fg(CONFIRM_COLOR)) .style(POPUP_HELP_BOX); let content = Paragraph::new(app.bibiman.popup_area.popup_message.clone()) .block(block) - .style(Style::new().fg(MAIN_GREEN)); + .style(Style::new().fg(CONFIRM_COLOR)); // Calculate popup size. Width is number of string chars plus 2 for border let popup_area = popup_area( @@ -222,13 +224,13 @@ pub fn render_popup(app: &mut App, frame: &mut Frame) { let area = frame.area(); let block = Block::bordered() - .title_top(" Warning ".bold().fg(Color::Red)) + .title_top(" Warning ".bold().fg(WARN_COLOR)) .border_style(Style::new().fg(Color::Red)) .style(POPUP_HELP_BOX); let content = Paragraph::new(app.bibiman.popup_area.popup_message.clone()) .block(block) - .style(Style::new().fg(Color::Red)); + .style(Style::new().fg(WARN_COLOR)); // Calculate popup size. Width is number of string chars plus 2 for border let popup_area = popup_area( @@ -256,7 +258,7 @@ pub fn render_popup(app: &mut App, frame: &mut Frame) { .title_alignment(Alignment::Center) .style(POPUP_HELP_BOX) .border_set(symbols::border::THICK) - .border_style(Style::new().fg(MAIN_PURPLE)); + .border_style(Style::new().fg(KEYWORD_COLOR)); let list = List::new(list_items) .block(block) @@ -275,9 +277,9 @@ pub fn render_popup(app: &mut App, frame: &mut Frame) { // }; // let popup = Popup::new(sized_list) - // .title(" Select ".bold().into_centered_line().fg(MAIN_PURPLE)) + // .title(" Select ".bold().into_centered_line().fg(KEYWORD_COLOR)) // .border_set(symbols::border::THICK) - // // .border_style(Style::new().fg(MAIN_GREEN)) + // // .border_style(Style::new().fg(CONFIRM_COLOR)) // .style(POPUP_HELP_BOX); // frame.render_stateful_widget( @@ -293,7 +295,7 @@ pub fn render_popup(app: &mut App, frame: &mut Frame) { pub fn render_header(frame: &mut Frame, rect: Rect) { let main_header = Paragraph::new("BIBIMAN – BibLaTeX manager TUI") .bold() - .fg(MAIN_BLUE) + .fg(ENTRY_COLOR) .centered(); frame.render_widget(main_header, rect) } @@ -732,7 +734,7 @@ pub fn render_selected_item(app: &mut App, frame: &mut Frame, rect: Rect) { if cur_entry.filepath.is_some() { lines.push(Line::from(vec![ Span::styled("File: ", style_value), - Span::styled(cur_entry.filepath(), INFO_STYLE_FILE), + Span::styled(cur_entry.filepath().to_string_lossy(), INFO_STYLE_FILE), ])); } lines.push(Line::from("")); |
