Skip to content

Commit

Permalink
feat: errors as notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Beastwick18 committed May 31, 2024
1 parent 70d4767 commit 8b9294d
Show file tree
Hide file tree
Showing 17 changed files with 238 additions and 293 deletions.
34 changes: 7 additions & 27 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use crate::{
batch::BatchWidget,
category::CategoryPopup,
clients::ClientsPopup,
error::ErrorPopup,
filter::FilterPopup,
help::HelpPopup,
notifications::NotificationWidget,
Expand Down Expand Up @@ -86,7 +85,6 @@ pub enum Mode {
Theme,
Sources,
Clients,
Error,
Page,
User,
Help,
Expand All @@ -105,7 +103,6 @@ impl Display for Mode {
Mode::Sources => "Sources".to_owned(),
Mode::Clients => "Clients".to_owned(),
Mode::Loading(_) => "Loading".to_owned(),
Mode::Error => "Error".to_owned(),
Mode::Page => "Page".to_owned(),
Mode::User => "User".to_owned(),
Mode::Help => "Help".to_owned(),
Expand Down Expand Up @@ -139,7 +136,6 @@ pub struct Context {
notifications: Vec<String>,
failed_config_load: bool,
should_quit: bool,
should_redraw: bool,
should_dismiss_notifications: bool,
}

Expand All @@ -156,10 +152,6 @@ impl Context {
self.should_dismiss_notifications = true;
}

pub fn redraw(&mut self) {
self.should_redraw = true;
}

pub fn save_config(&mut self) -> Result<(), Box<dyn Error>> {
#[cfg(not(feature = "integration-test"))]
if !self.failed_config_load {
Expand Down Expand Up @@ -194,7 +186,6 @@ impl Default for Context {
deltatime: 0.0,
failed_config_load: true,
should_quit: false,
should_redraw: false,
should_dismiss_notifications: false,
}
}
Expand All @@ -212,7 +203,6 @@ pub struct Widgets {
pub clients: ClientsPopup,
pub search: SearchWidget,
pub results: ResultsWidget,
pub error: ErrorPopup,
pub page: PagePopup,
pub user: UserPopup,
pub help: HelpPopup,
Expand Down Expand Up @@ -261,13 +251,17 @@ impl App {
.for_each(|n| self.widgets.notification.add_notification(n));
ctx.notifications.clear();
}
if !ctx.errors.is_empty() {
ctx.errors
.clone()
.into_iter()
.for_each(|n| self.widgets.notification.add_error(n));
ctx.errors.clear();
}
if ctx.should_dismiss_notifications {
self.widgets.notification.dismiss_all();
ctx.should_dismiss_notifications = false;
}
if !ctx.errors.is_empty() {
ctx.mode = Mode::Error;
}
if ctx.mode == Mode::Batch && ctx.batch.is_empty() {
ctx.mode = Mode::Normal;
}
Expand Down Expand Up @@ -409,11 +403,6 @@ impl App {
}
};
}
// if self.widgets.notification.is_animating() {
// let now = Instant::now();
// ctx.deltatime = (now - last_time.unwrap_or(now)).as_secs_f64();
// last_time = Some(now);
// }
}
Ok(())
}
Expand Down Expand Up @@ -447,13 +436,6 @@ impl App {
Mode::Sort(_) => self.widgets.sort.draw(f, ctx, f.size()),
Mode::Filter => self.widgets.filter.draw(f, ctx, f.size()),
Mode::Theme => self.widgets.theme.draw(f, ctx, f.size()),
Mode::Error => {
// Get the oldest error first
if let Some(error) = ctx.errors.pop_front() {
self.widgets.error.with_error(error);
}
self.widgets.error.draw(f, ctx, f.size());
}
Mode::Help => self.widgets.help.draw(f, ctx, f.size()),
Mode::Page => self.widgets.page.draw(f, ctx, f.size()),
Mode::User => self.widgets.user.draw(f, ctx, f.size()),
Expand Down Expand Up @@ -508,7 +490,6 @@ impl App {
Mode::Search => self.widgets.search.handle_event(ctx, evt),
Mode::Filter => self.widgets.filter.handle_event(ctx, evt),
Mode::Theme => self.widgets.theme.handle_event(ctx, evt),
Mode::Error => self.widgets.error.handle_event(ctx, evt),
Mode::Page => self.widgets.page.handle_event(ctx, evt),
Mode::User => self.widgets.user.handle_event(ctx, evt),
Mode::Help => self.widgets.help.handle_event(ctx, evt),
Expand Down Expand Up @@ -554,7 +535,6 @@ impl App {
Mode::User => UserPopup::get_help(),
Mode::Sources => SourcesPopup::get_help(),
Mode::Clients => ClientsPopup::get_help(),
Mode::Error => None,
Mode::Help => None,
Mode::KeyCombo(_) => None,
Mode::Loading(_) => None,
Expand Down
1 change: 1 addition & 0 deletions src/client/qbit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ impl DownloadClient for QbitClient {
conf: ClientConfig,
client: reqwest::Client,
) -> DownloadResult {
// return DownloadResult::error(DownloadError("Failed to login :\\"));
let Some(qbit) = conf.qbit.to_owned() else {
return DownloadResult::error(DownloadError(
"Failed to get qBittorrent config".to_owned(),
Expand Down
60 changes: 31 additions & 29 deletions src/util/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,36 +69,38 @@ pub fn key_to_string(key: KeyCode, modifier: KeyModifiers) -> String {
KeyCode::Menu => "Menu".to_owned(),
KeyCode::KeypadBegin => "Begin".to_owned(),
KeyCode::Media(m) => match m {
MediaKeyCode::Play => "MediaPlay".to_owned(),
MediaKeyCode::Pause => "MediaPause".to_owned(),
MediaKeyCode::PlayPause => "MediaPlayPause".to_owned(),
MediaKeyCode::Reverse => "MediaReverse".to_owned(),
MediaKeyCode::Stop => "MediaStop".to_owned(),
MediaKeyCode::FastForward => "MediaFastForward".to_owned(),
MediaKeyCode::Rewind => "MediaRewind".to_owned(),
MediaKeyCode::TrackNext => "MediaTrackNext".to_owned(),
MediaKeyCode::TrackPrevious => "MediaTrackPrevious".to_owned(),
MediaKeyCode::Record => "MediaRecord".to_owned(),
MediaKeyCode::LowerVolume => "MediaLowerVolume".to_owned(),
MediaKeyCode::RaiseVolume => "MediaRaiseVolume".to_owned(),
MediaKeyCode::MuteVolume => "MediaMuteVolume".to_owned(),
},
MediaKeyCode::Play => "MediaPlay",
MediaKeyCode::Pause => "MediaPause",
MediaKeyCode::PlayPause => "MediaPlayPause",
MediaKeyCode::Reverse => "MediaReverse",
MediaKeyCode::Stop => "MediaStop",
MediaKeyCode::FastForward => "MediaFastForward",
MediaKeyCode::Rewind => "MediaRewind",
MediaKeyCode::TrackNext => "MediaTrackNext",
MediaKeyCode::TrackPrevious => "MediaTrackPrevious",
MediaKeyCode::Record => "MediaRecord",
MediaKeyCode::LowerVolume => "MediaLowerVolume",
MediaKeyCode::RaiseVolume => "MediaRaiseVolume",
MediaKeyCode::MuteVolume => "MediaMuteVolume",
}
.to_owned(),
KeyCode::Modifier(m) => match m {
ModifierKeyCode::LeftShift => "LeftShift".to_owned(),
ModifierKeyCode::LeftControl => "LeftControl".to_owned(),
ModifierKeyCode::LeftAlt => "LeftAlt".to_owned(),
ModifierKeyCode::LeftSuper => "LeftSuper".to_owned(),
ModifierKeyCode::LeftHyper => "LeftHyper".to_owned(),
ModifierKeyCode::LeftMeta => "LeftMeta".to_owned(),
ModifierKeyCode::RightShift => "RightShift".to_owned(),
ModifierKeyCode::RightControl => "RightControl".to_owned(),
ModifierKeyCode::RightAlt => "RightAlt".to_owned(),
ModifierKeyCode::RightSuper => "RightSuper".to_owned(),
ModifierKeyCode::RightHyper => "RightHyper".to_owned(),
ModifierKeyCode::RightMeta => "RightMeta".to_owned(),
ModifierKeyCode::IsoLevel3Shift => "IsoLevel3Shift".to_owned(),
ModifierKeyCode::IsoLevel5Shift => "IsoLevel5Shift".to_owned(),
},
ModifierKeyCode::LeftShift => "LeftShift",
ModifierKeyCode::LeftControl => "LeftControl",
ModifierKeyCode::LeftAlt => "LeftAlt",
ModifierKeyCode::LeftSuper => "LeftSuper",
ModifierKeyCode::LeftHyper => "LeftHyper",
ModifierKeyCode::LeftMeta => "LeftMeta",
ModifierKeyCode::RightShift => "RightShift",
ModifierKeyCode::RightControl => "RightControl",
ModifierKeyCode::RightAlt => "RightAlt",
ModifierKeyCode::RightSuper => "RightSuper",
ModifierKeyCode::RightHyper => "RightHyper",
ModifierKeyCode::RightMeta => "RightMeta",
ModifierKeyCode::IsoLevel3Shift => "IsoLevel3Shift",
ModifierKeyCode::IsoLevel5Shift => "IsoLevel5Shift",
}
.to_owned(),
};
let modifier = match modifier {
KeyModifiers::CONTROL => "C-",
Expand Down
11 changes: 10 additions & 1 deletion src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use ratatui::{
},
Frame,
};
use unicode_width::UnicodeWidthStr as _;

use crate::{app::Context, style, theme::Theme};

pub mod batch;
pub mod category;
pub mod clients;
pub mod error;
pub mod filter;
pub mod help;
pub mod input;
Expand Down Expand Up @@ -125,6 +125,15 @@ pub fn scrollbar(ctx: &Context, orientation: ScrollbarOrientation) -> Scrollbar<
}

pub fn clear(area: Rect, buf: &mut Buffer, fill: Color) {
// Deal with wide chars which might extend too far
if area.left() > 0 && buf.area.contains((area.left() - 1, area.top()).into()) {
for i in area.top()..area.bottom() {
let c = buf.get_mut(area.left() - 1, i);
if c.symbol().width() > 1 {
c.set_char(' ');
}
}
}
Clear.render(area, buf);
Block::new().bg(fill).render(area, buf);
}
Expand Down
16 changes: 2 additions & 14 deletions src/widget/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,28 +112,17 @@ impl Widget for CategoryPopup {
" ".into(),
e.name.to_owned().into(),
])])
// match i == self.minor {
// true => row.bg(ctx.theme.hl_bg),
// false => row,
// }
});
// self.table.select(self.major + self.minor + 1);
self.table.scrollbar_state = self
.table
.scrollbar_state
.content_length(cat.entries.len() + ctx.src_info.cats.len());
// let last_elem = self.major + cat.entries.len() + 1;
// if last_elem > center.height - 2 {
//
// }
// *self.table.state.offset_mut() = 0;
// self.table.scrollbar_state = self.table.scrollbar_state.position(0);

tbl.splice(self.major + 1..self.major + 1, cat_rows);

let center = super::centered_rect(33, 14, area);
let clear = super::centered_rect(center.width + 2, center.height, area);
super::clear(clear, f.buffer_mut(), ctx.theme.bg);
// let clear = super::centered_rect(center.width + 2, center.height, area);
super::clear(center, f.buffer_mut(), ctx.theme.bg);
let table = Table::new(tbl, [Constraint::Percentage(100)])
.block(border_block(&ctx.theme, true).title(title!("Category")))
.highlight_style(Style::default().bg(ctx.theme.hl_bg));
Expand All @@ -145,7 +134,6 @@ impl Widget for CategoryPopup {
horizontal: 0,
});
StatefulWidget::render(sb, sb_area, f.buffer_mut(), &mut self.table.scrollbar_state);
// .render(center, f.buffer_mut());
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/widget/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ impl Widget for ClientsPopup {
fn draw(&mut self, f: &mut Frame, ctx: &Context, area: Rect) {
let buf = f.buffer_mut();
let center = super::centered_rect(30, self.table.items.len() as u16 + 2, area);
let clear = super::centered_rect(center.width + 2, center.height, area);
// let clear = super::centered_rect(center.width + 2, center.height, area);
let items = self.table.items.iter().map(|item| {
Row::new(vec![match item == &ctx.client {
true => format!("  {}", item),
false => format!(" {}", item),
}])
});
super::clear(clear, buf, ctx.theme.bg);
super::clear(center, buf, ctx.theme.bg);
let table = Table::new(items, [Constraint::Percentage(100)])
.block(border_block(&ctx.theme, true).title(title!("Download Client")))
.highlight_style(style!(bg:ctx.theme.hl_bg));
Expand Down
84 changes: 0 additions & 84 deletions src/widget/error.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/widget/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Default for FilterPopup {
impl Widget for FilterPopup {
fn draw(&mut self, f: &mut Frame, ctx: &Context, area: Rect) {
let center = super::centered_rect(30, ctx.src_info.filters.len() as u16 + 2, area);
let clear = super::centered_rect(center.width + 2, center.height, area);
// let clear = super::centered_rect(center.width + 2, center.height, area);
let items =
ctx.src_info
.filters
Expand All @@ -39,7 +39,7 @@ impl Widget for FilterPopup {
true => Row::new(vec![format!("  {}", item.to_owned())]),
false => Row::new(vec![format!(" {}", item.to_owned())]),
});
super::clear(clear, f.buffer_mut(), ctx.theme.bg);
super::clear(center, f.buffer_mut(), ctx.theme.bg);
Table::new(items, [Constraint::Percentage(100)])
.block(border_block(&ctx.theme, true).title(title!("Filter")))
.highlight_style(style!(bg:ctx.theme.hl_bg))
Expand Down
Loading

0 comments on commit 8b9294d

Please sign in to comment.