Skip to content

Commit

Permalink
feat: better styles
Browse files Browse the repository at this point in the history
  • Loading branch information
kdheepak committed Feb 9, 2024
1 parent c59b8ff commit 13aab68
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 33 deletions.
24 changes: 4 additions & 20 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
serde_helper::keybindings::key_event_to_string,
tui::Tui,
widgets::{
crate_info_table::CrateInfoTableWidget,
crate_info_table::{CrateInfo, CrateInfoTableWidget},
help::{Help, HelpWidget},
popup_message::{Popup, PopupMessageWidget},
search_filter_prompt::{SearchFilterPrompt, SearchFilterPromptWidget},
Expand Down Expand Up @@ -128,7 +128,7 @@ pub struct App {
summary: Summary,

/// contains table state for info popup
crate_info: TableState,
crate_info: CrateInfo,

last_task_details_handle: HashMap<uuid::Uuid, JoinHandle<()>>,

Expand Down Expand Up @@ -337,8 +337,8 @@ impl App {
Action::ScrollDown => self.search_results.scroll_next(1),
Action::ScrollTop => self.search_results.scroll_to_top(),
Action::ScrollBottom => self.search_results.scroll_to_bottom(),
Action::ScrollCrateInfoUp => self.crate_info_scroll_previous(),
Action::ScrollCrateInfoDown => self.crate_info_scroll_next(),
Action::ScrollCrateInfoUp => self.crate_info.scroll_previous(),
Action::ScrollCrateInfoDown => self.crate_info.scroll_next(),
Action::ScrollSearchResultsUp => self.search_results.scroll_previous(1),
Action::ScrollSearchResultsDown => self.search_results.scroll_next(1),
Action::ReloadData => self.reload_data(),
Expand Down Expand Up @@ -471,22 +471,6 @@ impl App {
}
}

fn crate_info_scroll_previous(&mut self) {
let i = self
.crate_info
.selected()
.map_or(0, |i| i.saturating_sub(1));
self.crate_info.select(Some(i));
}

fn crate_info_scroll_next(&mut self) {
let i = self
.crate_info
.selected()
.map_or(0, |i| i.saturating_add(1));
self.crate_info.select(Some(i));
}

fn enter_insert_mode(&mut self, mode: Mode) {
self.switch_mode(mode);
self.input = self.input.clone().with_value(if self.mode.is_search() {
Expand Down
33 changes: 28 additions & 5 deletions src/widgets/crate_info_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,29 @@ use ratatui::{prelude::*, widgets::*};

use crate::config;

#[derive(Debug, Default)]
pub struct CrateInfo {
crate_info: TableState,
}

impl CrateInfo {
pub fn scroll_previous(&mut self) {
let i = self
.crate_info
.selected()
.map_or(0, |i| i.saturating_sub(1));
self.crate_info.select(Some(i));
}

pub fn scroll_next(&mut self) {
let i = self
.crate_info
.selected()
.map_or(0, |i| i.saturating_add(1));
self.crate_info.select(Some(i));
}
}

pub struct CrateInfoTableWidget {
crate_info: crates_io_api::CrateResponse,
}
Expand All @@ -14,7 +37,7 @@ impl CrateInfoTableWidget {
}

impl StatefulWidget for CrateInfoTableWidget {
type State = TableState;
type State = CrateInfo;
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
let ci = self.crate_info.clone();

Expand Down Expand Up @@ -112,11 +135,11 @@ impl StatefulWidget for CrateInfoTableWidget {
.highlight_style(config::get().color.base05)
.highlight_spacing(HighlightSpacing::Always);

if let Some(i) = state.selected() {
state.select(Some(i.min(selected_max)));
if let Some(i) = state.crate_info.selected() {
state.crate_info.select(Some(i.min(selected_max)));
} else {
state.select(Some(0));
state.crate_info.select(Some(0));
}
StatefulWidget::render(table_widget, area, buf, state);
StatefulWidget::render(table_widget, area, buf, &mut state.crate_info);
}
}
30 changes: 22 additions & 8 deletions src/widgets/summary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<'a> SummaryWidget<'a> {
])
}))
.collect_vec();
list_builder(items, "New Crates".into(), borders)
list_builder(items, "New Crates", selected, borders)
}

fn most_downloaded(&self, selected: bool) -> List {
Expand All @@ -123,7 +123,7 @@ impl<'a> SummaryWidget<'a> {
])
}))
.collect_vec();
list_builder(items, "Most Downloaded".into(), borders)
list_builder(items, "Most Downloaded", selected, borders)
}

fn just_updated(&self, selected: bool) -> List {
Expand All @@ -143,7 +143,7 @@ impl<'a> SummaryWidget<'a> {
])
}))
.collect_vec();
list_builder(items, "Just Updated".into(), borders)
list_builder(items, "Just Updated", selected, borders)
}

fn most_recently_downloaded(&self, selected: bool) -> List {
Expand All @@ -156,7 +156,7 @@ impl<'a> SummaryWidget<'a> {
])
}))
.collect_vec();
list_builder(items, "Most Recently Downloaded".into(), borders)
list_builder(items, "Most Recently Downloaded", selected, borders)
}

fn popular_keywords(&self, selected: bool) -> List {
Expand All @@ -169,7 +169,7 @@ impl<'a> SummaryWidget<'a> {
])
}))
.collect_vec();
list_builder(items, "Popular Keywords".into(), borders)
list_builder(items, "Popular Keywords", selected, borders)
}

fn popular_categories(&self, selected: bool) -> List {
Expand All @@ -182,7 +182,7 @@ impl<'a> SummaryWidget<'a> {
])
}))
.collect_vec();
list_builder(items, "Popular Categories".into(), borders)
list_builder(items, "Popular Categories", selected, borders)
}

fn render_list(
Expand All @@ -201,12 +201,26 @@ impl<'a> SummaryWidget<'a> {
}
}

fn list_builder(items: Vec<Text>, title: String, borders: Borders) -> List {
fn list_builder<'a>(
items: Vec<Text<'a>>,
title: &'a str,
selected: bool,
borders: Borders,
) -> List<'a> {
let title_style = if selected {
Style::default()
.fg(config::get().color.base00)
.bg(config::get().color.base0a)
.bold()
} else {
Style::default().fg(config::get().color.base0d).bold()
};
List::new(items)
.block(
Block::default()
.borders(borders)
.title(format!(" {title}").bold().fg(config::get().color.base05))
.title(Line::from(vec![" ".into(), title.into(), " ".into()]))
.title_style(title_style)
.title_alignment(Alignment::Left),
)
.highlight_symbol(HIGHLIGHT_SYMBOL)
Expand Down

0 comments on commit 13aab68

Please sign in to comment.