Skip to content

Commit

Permalink
feat(term): add entries requirement filter
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Apr 14, 2024
1 parent 04d9a88 commit 5d49d7f
Show file tree
Hide file tree
Showing 15 changed files with 383 additions and 42 deletions.
2 changes: 1 addition & 1 deletion crates/synd_term/src/application/input_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'a> InputParser<'a> {
# * \"MUST\"
# * \"SHOULD\"
# * \"MAY\"
# * For the category, please choose one category of the feed(for example, \"rust\"
# * For the category, please choose one category of the feed(for example, \"rust\")
#
# with '#' will be ignored, and an empty URL aborts the subscription.
#
Expand Down
19 changes: 15 additions & 4 deletions crates/synd_term/src/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ impl Application {
self.keymaps.disable(KeymapId::Login);
self.keymaps.enable(KeymapId::Tabs);
self.keymaps.enable(KeymapId::Entries);
self.keymaps.enable(KeymapId::Filter);
self.initial_fetch();
self.screen = Screen::Browse;
self.should_render = true;
Expand Down Expand Up @@ -339,7 +340,7 @@ impl Application {
Command::FetchSubscription { after, first } => {
self.fetch_subscription(ListAction::Append, after, first);
}
Command::UpdateSubscription {
Command::UpdateSubscriptionState {
action,
subscription,
request_seq,
Expand Down Expand Up @@ -380,12 +381,17 @@ impl Application {
Command::FetchEntries { after, first } => {
self.fetch_entries(ListAction::Append, after, first);
}
Command::UpdateEntries {
Command::UpdateEntriesState {
action,
payload,
request_seq,
} => {
self.in_flight.remove(request_seq);
self.components.filter.update_categories(
&self.categories,
action,
payload.entries.as_slice(),
);
self.components.entries.update_entries(action, payload);
self.should_render = true;
}
Expand All @@ -412,6 +418,11 @@ impl Application {
Command::OpenEntry => {
self.open_entry();
}
Command::MoveFilterRequirement(direction) => {
let filter = self.components.filter.move_requirement(direction);
self.components.entries.update_filter(filter);
self.should_render = true;
}
Command::HandleError {
message,
request_seq,
Expand Down Expand Up @@ -467,7 +478,7 @@ impl Application {
let request_seq = self.in_flight.add(RequestId::FetchSubscription);
let fut = async move {
match client.fetch_subscription(after, Some(first)).await {
Ok(subscription) => Ok(Command::UpdateSubscription {
Ok(subscription) => Ok(Command::UpdateSubscriptionState {
action,
subscription,
request_seq,
Expand Down Expand Up @@ -626,7 +637,7 @@ impl Application {
let request_seq = self.in_flight.add(RequestId::FetchEntries);
let fut = async move {
match client.fetch_entries(after, first).await {
Ok(payload) => Ok(Command::UpdateEntries {
Ok(payload) => Ok(Command::UpdateEntriesState {
action,
payload,
request_seq,
Expand Down
17 changes: 13 additions & 4 deletions crates/synd_term/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub enum Command {
after: Option<String>,
first: i64,
},
UpdateSubscription {
UpdateSubscriptionState {
action: ListAction,
subscription: SubscriptionOutput,
request_seq: RequestSequence,
Expand All @@ -74,7 +74,7 @@ pub enum Command {
after: Option<String>,
first: i64,
},
UpdateEntries {
UpdateEntriesState {
action: ListAction,
payload: payload::FetchEntriesPayload,
request_seq: RequestSequence,
Expand All @@ -85,6 +85,9 @@ pub enum Command {
MoveEntryLast,
OpenEntry,

// Filter
MoveFilterRequirement(Direction),

HandleError {
message: String,
request_seq: Option<RequestSequence>,
Expand All @@ -94,8 +97,8 @@ pub enum Command {
impl Display for Command {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Command::UpdateSubscription { .. } => f.write_str("UpdateSubscription"),
Command::UpdateEntries { .. } => f.write_str("UpdateEntries"),
Command::UpdateSubscriptionState { .. } => f.write_str("UpdateSubscription"),
Command::UpdateEntriesState { .. } => f.write_str("UpdateEntries"),
Command::CompleteDevieAuthorizationFlow { .. } => {
f.write_str("CompleteDeviceAuthorizationFlow")
}
Expand Down Expand Up @@ -168,4 +171,10 @@ impl Command {
pub fn move_subscribed_feed_last() -> Self {
Command::MoveSubscribedFeedLast
}
pub fn move_filter_requirement_left() -> Self {
Command::MoveFilterRequirement(Direction::Left)
}
pub fn move_filter_requirement_right() -> Self {
Command::MoveFilterRequirement(Direction::Right)
}
}
38 changes: 33 additions & 5 deletions crates/synd_term/src/config/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,28 @@ struct Entry {
aliases: Vec<String>,
}

#[derive(Deserialize)]
#[derive(Debug, Clone, Deserialize)]
pub struct Icon {
symbol: String,
color: Option<IconColor>,
}

impl Icon {
pub fn new(symbol: impl Into<String>) -> Self {
Self {
symbol: symbol.into(),
color: None,
}
}

#[must_use]
pub fn with_color(self, color: IconColor) -> Self {
Self {
color: Some(color),
..self
}
}

pub fn symbol(&self) -> &str {
self.symbol.as_str()
}
Expand All @@ -69,19 +84,32 @@ impl Icon {
}
}

#[derive(Deserialize, Default)]
struct IconColor {
#[derive(Clone, Debug, Deserialize, Default)]
pub struct IconColor {
rgb: Option<u32>,
// https://docs.rs/ratatui/latest/ratatui/style/enum.Color.html#variant.Red
name: Option<String>,
#[serde(skip)]
color: Option<Color>,
}

impl IconColor {
pub fn new(color: Color) -> Self {
Self {
rgb: None,
name: None,
color: Some(color),
}
}
}

impl IconColor {
fn color(&self) -> Option<Color> {
self.rgb
self.color.or(self
.rgb
.as_ref()
.map(|rgb| Color::from_u32(*rgb))
.or(self.name.as_ref().and_then(|s| s.parse().ok()))
.or(self.name.as_ref().and_then(|s| s.parse().ok())))
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/synd_term/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
use directories::ProjectDirs;

mod categories;
pub use categories::Categories;
pub use categories::{Categories, Icon, IconColor};

pub mod api {
pub const ENDPOINT: &str = "https://api.syndicationd.ymgyt.io:6100";
Expand Down
5 changes: 5 additions & 0 deletions crates/synd_term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub fn default() -> KeymapsConfig {
"e" => move_subscribed_feed_last,
},
});
let filter = keymap!({
"h" | "left" => move_filter_requirement_left,
"l" | "right" => move_filter_requirement_right,
});
let global = keymap!({
"q" | "C-c" => quit ,
});
Expand All @@ -42,6 +46,7 @@ pub fn default() -> KeymapsConfig {
tabs,
entries,
subscription,
filter,
global,
}
}
9 changes: 8 additions & 1 deletion crates/synd_term/src/keymap/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub enum KeymapId {
Tabs = 2,
Entries = 3,
Subscription = 4,
Filter = 5,
}

#[derive(Debug)]
Expand Down Expand Up @@ -65,6 +66,7 @@ pub struct KeymapsConfig {
pub tabs: KeyTrie,
pub entries: KeyTrie,
pub subscription: KeyTrie,
pub filter: KeyTrie,
pub global: KeyTrie,
}

Expand All @@ -76,7 +78,7 @@ impl Default for KeymapsConfig {

#[derive(Debug)]
pub struct Keymaps {
keymaps: Box<[Keymap; 5]>,
keymaps: Box<[Keymap; 6]>,
}

impl Keymaps {
Expand All @@ -88,6 +90,7 @@ impl Keymaps {
Keymap::new(KeymapId::Tabs, config.tabs),
Keymap::new(KeymapId::Entries, config.entries),
Keymap::new(KeymapId::Subscription, config.subscription),
Keymap::new(KeymapId::Filter, config.filter),
];

Self {
Expand Down Expand Up @@ -154,6 +157,10 @@ fn parse(s: &str) -> anyhow::Result<KeyEvent> {
"enter" => KeyCode::Enter,
"tab" => KeyCode::Tab,
"backtab" => KeyCode::BackTab,
"left" => KeyCode::Left,
"right" => KeyCode::Right,
"up" => KeyCode::Up,
"down" => KeyCode::Down,
single if single.chars().count() == 1 => KeyCode::Char(single.chars().next().unwrap()),
undefined => bail!("`{undefined}` is not implemented yet"),
};
Expand Down
14 changes: 12 additions & 2 deletions crates/synd_term/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,8 @@ pub struct Entry {
pub summary: Option<String>,
pub feed_title: Option<String>,
pub feed_url: String,
pub requirement: Option<Requirement>,
pub category: Option<Category<'static>>,
requirement: Option<Requirement>,
category: Option<Category<'static>>,
}

impl Entry {
Expand All @@ -195,6 +195,16 @@ impl Entry {
.as_deref()
.map(|summary| html2text::from_read(summary.as_bytes(), width))
}

pub fn requirement(&self) -> Requirement {
self.requirement.unwrap_or(ui::DEFAULT_REQUIREMNET)
}

pub fn category(&self) -> &Category<'static> {
self.category
.as_ref()
.unwrap_or_else(|| ui::default_category())
}
}

impl From<query::entries::Entry> for Entry {
Expand Down

0 comments on commit 5d49d7f

Please sign in to comment.