From 63d4410fdfe7712faec287aee2f5c0ca288dc996 Mon Sep 17 00:00:00 2001 From: lukeflo Date: Sat, 22 Feb 2025 00:48:41 +0100 Subject: replace Config crate with Figment -> better merging of default and config values --- src/tui/ui.rs | 243 +++++++++++++++++++++++----------------------------------- 1 file changed, 96 insertions(+), 147 deletions(-) (limited to 'src/tui/ui.rs') diff --git a/src/tui/ui.rs b/src/tui/ui.rs index 5fbe283..2d58aec 100644 --- a/src/tui/ui.rs +++ b/src/tui/ui.rs @@ -150,11 +150,11 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { .title_alignment(Alignment::Center) .style( Style::new() - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()) - .bg(cfg.colors.as_ref().unwrap().popup_bg_color.unwrap()), + .fg(cfg.colors.main_text_color) + .bg(cfg.colors.popup_bg_color), ) .border_set(symbols::border::THICK) - .border_style(Style::new().fg(cfg.colors.as_ref().unwrap().entry_color.unwrap())); + .border_style(Style::new().fg(cfg.colors.entry_color)); let text: Text = PopupArea::popup_help(cfg); @@ -191,23 +191,20 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { .title_alignment(Alignment::Center) .style( Style::new() - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()) - .bg(cfg.colors.as_ref().unwrap().popup_bg_color.unwrap()), + .fg(cfg.colors.main_text_color) + .bg(cfg.colors.popup_bg_color), ) .border_set(symbols::border::THICK) - .border_style(Style::new().fg(cfg.colors.as_ref().unwrap().entry_color.unwrap())); + .border_style(Style::new().fg(cfg.colors.entry_color)); // Prepare the input fields let content = vec![Line::from(vec![ - Span::styled( - "DOI: ", - Style::new().fg(cfg.colors.as_ref().unwrap().entry_color.unwrap()), - ), + Span::styled("DOI: ", Style::new().fg(cfg.colors.entry_color)), Span::raw(app.input.value().to_string().clone()), ])]; let paragraph = Paragraph::new(content) .block(block.clone()) - .style(Style::new().fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap())) + .style(Style::new().fg(cfg.colors.main_text_color)) .wrap(Wrap { trim: false }); let doi_lines = paragraph.line_count(area.width / 2); @@ -225,21 +222,17 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { let area = frame.area(); let block = Block::bordered() - .title_top( - " Message " - .bold() - .fg(cfg.colors.as_ref().unwrap().confirm_color.unwrap()), - ) - .border_style(Style::new().fg(cfg.colors.as_ref().unwrap().confirm_color.unwrap())) + .title_top(" Message ".bold().fg(cfg.colors.confirm_color)) + .border_style(Style::new().fg(cfg.colors.confirm_color)) .style( Style::new() - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()) - .bg(cfg.colors.as_ref().unwrap().popup_bg_color.unwrap()), + .fg(cfg.colors.main_text_color) + .bg(cfg.colors.popup_bg_color), ); let content = Paragraph::new(app.bibiman.popup_area.popup_message.clone()) .block(block) - .style(Style::new().fg(cfg.colors.as_ref().unwrap().confirm_color.unwrap())); + .style(Style::new().fg(cfg.colors.confirm_color)); // Calculate popup size. Width is number of string chars plus 2 for border let popup_area = popup_area( @@ -256,21 +249,17 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { let area = frame.area(); let block = Block::bordered() - .title_top( - " Warning " - .bold() - .fg(cfg.colors.as_ref().unwrap().warn_color.unwrap()), - ) + .title_top(" Warning ".bold().fg(cfg.colors.warn_color)) .border_style(Style::new().fg(Color::Red)) .style( Style::new() - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()) - .bg(cfg.colors.as_ref().unwrap().popup_bg_color.unwrap()), + .fg(cfg.colors.main_text_color) + .bg(cfg.colors.popup_bg_color), ); let content = Paragraph::new(app.bibiman.popup_area.popup_message.clone()) .block(block) - .style(Style::new().fg(cfg.colors.as_ref().unwrap().warn_color.unwrap())); + .style(Style::new().fg(cfg.colors.warn_color)); // Calculate popup size. Width is number of string chars plus 2 for border let popup_area = popup_area( @@ -306,15 +295,15 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { .title_alignment(Alignment::Center) .style( Style::new() - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()) - .bg(cfg.colors.as_ref().unwrap().popup_bg_color.unwrap()), + .fg(cfg.colors.main_text_color) + .bg(cfg.colors.popup_bg_color), ) .border_set(symbols::border::THICK) - .border_style(Style::new().fg(cfg.colors.as_ref().unwrap().keyword_color.unwrap())); + .border_style(Style::new().fg(cfg.colors.keyword_color)); let list = List::new(list_items).block(block).highlight_style( Style::new() - // .fg(cfg.colors.as_ref().unwrap().entry_color.unwrap()) + // .fg(cfg.colors.entry_color) .add_modifier(Modifier::BOLD) .add_modifier(Modifier::REVERSED), ); @@ -333,7 +322,7 @@ pub fn render_popup(app: &mut App, cfg: &BibiConfig, frame: &mut Frame) { pub fn render_header(cfg: &BibiConfig, frame: &mut Frame, rect: Rect) { let main_header = Paragraph::new("BIBIMAN – BibLaTeX manager TUI") .bold() - .fg(cfg.colors.as_ref().unwrap().entry_color.unwrap()) + .fg(cfg.colors.entry_color) .centered(); frame.render_widget(main_header, rect) } @@ -349,36 +338,29 @@ pub fn render_footer(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rect: R let title_lenght: u16 = search_title.chars().count() as u16; - let block = Block::new().padding(Padding::horizontal(1)).bg(cfg - .colors - .as_ref() - .unwrap() - .bar_bg_color - .unwrap()); + let block = Block::new() + .padding(Padding::horizontal(1)) + .bg(cfg.colors.bar_bg_color); let search_string = Paragraph::new(Line::from(vec![ Span::styled( search_title, if let Some(FormerArea::EntryArea) = app.bibiman.former_area { Style::new() - .fg(cfg.colors.as_ref().unwrap().entry_color.unwrap()) + .fg(cfg.colors.entry_color) .add_modifier(Modifier::BOLD) } else if let Some(FormerArea::TagArea) = app.bibiman.former_area { Style::new() - .fg(cfg.colors.as_ref().unwrap().keyword_color.unwrap()) + .fg(cfg.colors.keyword_color) .add_modifier(Modifier::BOLD) } else { Style::new() - .fg(cfg.colors.as_ref().unwrap().highlight_text_color.unwrap()) + .fg(cfg.colors.highlight_text_color) .add_modifier(Modifier::BOLD) }, ), - Span::raw(app.bibiman.search_struct.search_string.clone()).fg(cfg - .colors - .as_ref() - .unwrap() - .highlight_text_color - .unwrap()), + Span::raw(app.bibiman.search_struct.search_string.clone()) + .fg(cfg.colors.highlight_text_color), ])) .block(block); @@ -400,10 +382,10 @@ pub fn render_file_info(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rect symbols::border::PLAIN }) .border_style(if let CurrentArea::EntryArea = app.bibiman.current_area { - Style::new().fg(cfg.colors.as_ref().unwrap().highlight_text_color.unwrap()) + Style::new().fg(cfg.colors.highlight_text_color) } else { Style::new() - .fg(cfg.colors.as_ref().unwrap().entry_color.unwrap()) + .fg(cfg.colors.entry_color) .add_modifier(Modifier::BOLD) }); @@ -421,42 +403,33 @@ pub fn render_file_info(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rect && app.bibiman.main_bibfiles.first().unwrap().is_file() { Line::from(vec![ - Span::raw("File: ") - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()) - .bold(), + Span::raw("File: ").fg(cfg.colors.main_text_color).bold(), Span::raw( app.bibiman.main_bibfiles[0] .file_name() .unwrap() .to_string_lossy(), ) - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()) + .fg(cfg.colors.main_text_color) .bold(), ]) - .bg(cfg.colors.as_ref().unwrap().bar_bg_color.unwrap()) + .bg(cfg.colors.bar_bg_color) } else { Line::from(vec![ Span::raw("Multiple files (") - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()) + .fg(cfg.colors.main_text_color) .bold(), Span::raw(count_files(&app.bibiman.main_bibfiles).to_string()) - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()) - .bold(), - Span::raw(")") - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()) + .fg(cfg.colors.main_text_color) .bold(), + Span::raw(")").fg(cfg.colors.main_text_color).bold(), ]) - .bg(cfg.colors.as_ref().unwrap().bar_bg_color.unwrap()) + .bg(cfg.colors.bar_bg_color) }; let cur_keywords = Line::from(if !app.bibiman.tag_list.selected_keywords.is_empty() { vec![ - Span::raw("Selected keywords: ").fg(cfg - .colors - .as_ref() - .unwrap() - .main_text_color - .unwrap()), + Span::raw("Selected keywords: ").fg(cfg.colors.main_text_color), // Show all keywords in correct order if list is filtered // successively by multiple keywords Span::raw(app.bibiman.tag_list.selected_keywords.join(" → ")) @@ -466,7 +439,7 @@ pub fn render_file_info(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rect } else { vec![Span::raw(" ")] }) - .bg(cfg.colors.as_ref().unwrap().bar_bg_color.unwrap()); + .bg(cfg.colors.bar_bg_color); // .render(keyword_area, buf); let item_count = Line::from( @@ -502,42 +475,36 @@ pub fn render_file_info(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rect .to_string() }, ) - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()) + .fg(cfg.colors.main_text_color) .bold(), - Span::raw("/").fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()), - Span::raw(app.bibiman.entry_table.entry_table_items.len().to_string()).fg(cfg - .colors - .as_ref() - .unwrap() - .main_text_color - .unwrap()), + Span::raw("/").fg(cfg.colors.main_text_color), + Span::raw(app.bibiman.entry_table.entry_table_items.len().to_string()) + .fg(cfg.colors.main_text_color), ] } else { vec![Span::raw("No entries")] }, ) .right_aligned() - .bg(cfg.colors.as_ref().unwrap().bar_bg_color.unwrap()); + .bg(cfg.colors.bar_bg_color); frame.render_widget(file_info, file_area); frame.render_widget(cur_keywords, keyword_area); frame.render_widget(item_count, count_area); } pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rect: Rect) { - let entry_box_selected_border_style: Style = - Style::new().fg(cfg.colors.as_ref().unwrap().highlight_text_color.unwrap()); + let entry_box_selected_border_style: Style = Style::new().fg(cfg.colors.highlight_text_color); let entry_box_selected_title_style: Style = Style::new() - .fg(cfg.colors.as_ref().unwrap().entry_color.unwrap()) + .fg(cfg.colors.entry_color) .add_modifier(Modifier::BOLD); - let entry_box_unselected_border_style: Style = - Style::new().fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()); + let entry_box_unselected_border_style: Style = Style::new().fg(cfg.colors.main_text_color); let entry_box_unselected_title_style: Style = Style::new() - .fg(cfg.colors.as_ref().unwrap().entry_color.unwrap()) + .fg(cfg.colors.entry_color) .add_modifier(Modifier::BOLD); let selected_table_col_style: Style = Style::new().add_modifier(Modifier::BOLD); let selectec_table_cell_style: Style = Style::new().add_modifier(Modifier::REVERSED); let entry_selected_row_style: Style = Style::new() - .fg(cfg.colors.as_ref().unwrap().entry_color.unwrap()) + .fg(cfg.colors.entry_color) .add_modifier(Modifier::BOLD) .add_modifier(Modifier::REVERSED); @@ -567,8 +534,8 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec let header_style = Style::default() .bold() - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()) - .bg(cfg.colors.as_ref().unwrap().bar_bg_color.unwrap()); + .fg(cfg.colors.main_text_color) + .bg(cfg.colors.bar_bg_color); let header = Row::new(vec![ Cell::from( @@ -592,9 +559,9 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec if let EntryTableColumn::Authors = app.bibiman.entry_table.entry_table_selected_column { - cfg.colors.as_ref().unwrap().selected_row_bg_color.unwrap() + cfg.colors.selected_row_bg_color } else { - cfg.colors.as_ref().unwrap().bar_bg_color.unwrap() + cfg.colors.bar_bg_color }, ), ), @@ -618,9 +585,9 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec .bg( if let EntryTableColumn::Title = app.bibiman.entry_table.entry_table_selected_column { - cfg.colors.as_ref().unwrap().selected_row_bg_color.unwrap() + cfg.colors.selected_row_bg_color } else { - cfg.colors.as_ref().unwrap().bar_bg_color.unwrap() + cfg.colors.bar_bg_color }, ), ), @@ -644,9 +611,9 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec .bg( if let EntryTableColumn::Year = app.bibiman.entry_table.entry_table_selected_column { - cfg.colors.as_ref().unwrap().selected_row_bg_color.unwrap() + cfg.colors.selected_row_bg_color } else { - cfg.colors.as_ref().unwrap().bar_bg_color.unwrap() + cfg.colors.bar_bg_color }, ), ), @@ -671,9 +638,9 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec if let EntryTableColumn::Pubtype = app.bibiman.entry_table.entry_table_selected_column { - cfg.colors.as_ref().unwrap().selected_row_bg_color.unwrap() + cfg.colors.selected_row_bg_color } else { - cfg.colors.as_ref().unwrap().bar_bg_color.unwrap() + cfg.colors.bar_bg_color }, ), ), @@ -705,7 +672,7 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec // args.colors.highlight_text_color, // 20, // )), - Style::new().fg(cfg.colors.as_ref().unwrap().highlight_text_color.unwrap()), + Style::new().fg(cfg.colors.highlight_text_color), ) .height(1) }); @@ -760,9 +727,7 @@ pub fn render_entrytable(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rec pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rect: Rect) { // We get the info depending on the item's state. - let style_value = Style::new() - .bold() - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()); + let style_value = Style::new().bold().fg(cfg.colors.main_text_color); let lines = { if app .bibiman @@ -782,10 +747,7 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, lines.push(Line::from(vec![ Span::styled("Authors: ", style_value), // Span::styled(cur_entry.authors.clone(), Style::new().green()), - Span::styled( - cur_entry.authors(), - Style::new().fg(cfg.colors.as_ref().unwrap().info_color.unwrap()), - ), + Span::styled(cur_entry.authors(), Style::new().fg(cfg.colors.info_color)), ])); if cur_entry.subtitle.is_some() { lines.push(Line::from(vec![ @@ -793,19 +755,19 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, Span::styled( cur_entry.title(), Style::new() - .fg(cfg.colors.as_ref().unwrap().entry_color.unwrap()) + .fg(cfg.colors.entry_color) .add_modifier(Modifier::ITALIC), ), Span::styled( ": ", Style::new() - .fg(cfg.colors.as_ref().unwrap().entry_color.unwrap()) + .fg(cfg.colors.entry_color) .add_modifier(Modifier::ITALIC), ), Span::styled( cur_entry.subtitle(), Style::new() - .fg(cfg.colors.as_ref().unwrap().entry_color.unwrap()) + .fg(cfg.colors.entry_color) .add_modifier(Modifier::ITALIC), ), ])); @@ -815,17 +777,14 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, Span::styled( cur_entry.title(), Style::new() - .fg(cfg.colors.as_ref().unwrap().entry_color.unwrap()) + .fg(cfg.colors.entry_color) .add_modifier(Modifier::ITALIC), ), ])); } lines.push(Line::from(vec![ Span::styled("Year: ", style_value), - Span::styled( - cur_entry.year(), - Style::new().fg(cfg.colors.as_ref().unwrap().keyword_color.unwrap()), - ), + Span::styled(cur_entry.year(), Style::new().fg(cfg.colors.keyword_color)), ])); // Render keywords in info box in Markdown code style if !cur_entry.keywords.is_empty() { @@ -838,31 +797,25 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, let mut content = vec![Span::styled("Keywords: ", style_value)]; for k in kw { // Add half block highlighted in bg color to enlarge block - content.push( - Span::raw("▐").fg(cfg.colors.as_ref().unwrap().bar_bg_color.unwrap()), - ); + content.push(Span::raw("▐").fg(cfg.colors.bar_bg_color)); content.push(Span::styled( k, - Style::default() - .bg(cfg.colors.as_ref().unwrap().bar_bg_color.unwrap()) - .fg( - // Highlight selected keyword green - if app - .bibiman - .tag_list - .selected_keywords - .iter() - .any(|e| e == k) - { - Color::Green - } else { - cfg.colors.as_ref().unwrap().main_text_color.unwrap() - }, - ), + Style::default().bg(cfg.colors.bar_bg_color).fg( + // Highlight selected keyword green + if app + .bibiman + .tag_list + .selected_keywords + .iter() + .any(|e| e == k) + { + Color::Green + } else { + cfg.colors.main_text_color + }, + ), )); - content.push( - Span::raw("▌").fg(cfg.colors.as_ref().unwrap().bar_bg_color.unwrap()), - ); + content.push(Span::raw("▌").fg(cfg.colors.bar_bg_color)); } lines.push(Line::from(content)) } @@ -874,9 +827,7 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, Span::styled("DOI/URL: ", style_value), Span::styled( cur_entry.doi_url(), - Style::new() - .fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()) - .underlined(), + Style::new().fg(cfg.colors.main_text_color).underlined(), ), ])); } @@ -885,14 +836,14 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, Span::styled("File: ", style_value), Span::styled( cur_entry.filepath().to_string_lossy(), - Style::new().fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()), + Style::new().fg(cfg.colors.main_text_color), ), ])); } lines.push(Line::from("")); lines.push(Line::from(vec![Span::styled( cur_entry.abstract_text.clone(), - Style::new().fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()), + Style::new().fg(cfg.colors.main_text_color), )])); lines } else { @@ -909,7 +860,7 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, let block = Block::bordered() .title(Line::raw(" Entry Information ").centered().bold()) .border_set(symbols::border::PLAIN) - .border_style(Style::new().fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap())) + .border_style(Style::new().fg(cfg.colors.main_text_color)) .padding(Padding::horizontal(1)); // INFO: '.line_count' method only possible with unstable-rendered-line-info feature -> API might change: https://github.com/ratatui/ratatui/issues/293#ref-pullrequest-2027056434 @@ -963,18 +914,16 @@ pub fn render_selected_item(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, } pub fn render_taglist(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rect: Rect) { - let keyword_box_selected_border_style: Style = - Style::new().fg(cfg.colors.as_ref().unwrap().highlight_text_color.unwrap()); + let keyword_box_selected_border_style: Style = Style::new().fg(cfg.colors.highlight_text_color); let keyword_box_selected_title_style: Style = Style::new() - .fg(cfg.colors.as_ref().unwrap().keyword_color.unwrap()) + .fg(cfg.colors.keyword_color) .add_modifier(Modifier::BOLD); - let keyword_box_unselected_border_style: Style = - Style::new().fg(cfg.colors.as_ref().unwrap().main_text_color.unwrap()); + let keyword_box_unselected_border_style: Style = Style::new().fg(cfg.colors.main_text_color); let keyword_box_unselected_title_style: Style = Style::new() - .fg(cfg.colors.as_ref().unwrap().keyword_color.unwrap()) + .fg(cfg.colors.keyword_color) .add_modifier(Modifier::BOLD); let keyword_selected_row_style: Style = Style::new() - .fg(cfg.colors.as_ref().unwrap().keyword_color.unwrap()) + .fg(cfg.colors.keyword_color) .add_modifier(Modifier::BOLD) .add_modifier(Modifier::REVERSED); @@ -1018,9 +967,9 @@ pub fn render_taglist(app: &mut App, cfg: &BibiConfig, frame: &mut Frame, rect: // args.colors.highlight_text_color, // 20, // ) - cfg.colors.as_ref().unwrap().highlight_text_color.unwrap() + cfg.colors.highlight_text_color } else { - cfg.colors.as_ref().unwrap().main_text_color.unwrap() + cfg.colors.main_text_color }, )) //.bg(color) }) -- cgit v1.2.3