Skip to content

Commit

Permalink
feat: find website url
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Jan 22, 2024
1 parent 86fa1d8 commit cc5da3e
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 148 deletions.
89 changes: 1 addition & 88 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions synd/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,31 @@ impl Feed {
pub fn links(&self) -> impl Iterator<Item = &feedrs::Link> {
self.feed.links.iter()
}

/// Return website link to which feed syndicate
pub fn website_url(&self) -> Option<&str> {
match self.feed.feed_type {
// find rel == alternate link
FeedType::Atom => self
.feed
.links
.iter()
.find(|link| link.rel.as_deref() == Some("alternate"))
.map(|link| link.href.as_str()),

// TODO
FeedType::JSON => todo!(),

// TODO
FeedType::RSS0 => todo!(),

// use first one
FeedType::RSS1 | FeedType::RSS2 => {
assert!(self.feed.links.len() < 2);
self.feed.links.first().map(|link| link.href.as_str())
}
}
}
}

impl From<(String, feed_rs::model::Feed)> for Feed {
Expand Down
4 changes: 4 additions & 0 deletions syndapi/src/gql/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ impl Feed {
c
}

async fn website_url(&self) -> Option<&str> {
self.0.website_url()
}

// TODO: entries
// TODO: category
}
Expand Down
1 change: 1 addition & 0 deletions syndapi/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ async fn main() {
let dep = Dependency::new(args.kvsd).await.unwrap();

info!(version, "Runinng...");
tracing::debug!("Debug");

if let Err(err) = listen_and_serve(dep).await {
error!("{err:?}");
Expand Down
1 change: 0 additions & 1 deletion syndterm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ graphql_client = { workspace = true }
http = "0.2" # reqwest use 0.2
http-serde-ext = "0.1"
open = "5.0.1"
palette = "0.7.3"
# Use latest api
ratatui = { git = "https://github.com/ratatui-org/ratatui.git", branch = "main"}
reqwest = { workspace = true }
Expand Down
1 change: 1 addition & 0 deletions syndterm/gql/mutation.gql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fragment FeedMeta on Feed {
title
url
updated
websiteUrl
links {
nodes {
...Link
Expand Down
1 change: 1 addition & 0 deletions syndterm/gql/query.gql
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fragment FeedMeta on Feed {
title
url
updated
websiteUrl
links {
nodes {
...Link
Expand Down
12 changes: 12 additions & 0 deletions syndterm/gql/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@
"ofType": null
}
}
},
{
"args": [],
"deprecationReason": null,
"description": null,
"isDeprecated": false,
"name": "websiteUrl",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
}
],
"inputFields": null,
Expand Down
4 changes: 3 additions & 1 deletion syndterm/src/client/mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod subscribe_feed {
#![allow(dead_code)]
use std::result::Result;
pub const OPERATION_NAME: &str = "SubscribeFeed";
pub const QUERY : & str = "mutation SubscribeFeed($input: SubscribeFeedInput!) {\n subscribeFeed(input: $input) {\n __typename\n ... on SubscribeFeedSuccess {\n feed {\n ...FeedMeta\n }\n status {\n code\n }\n }\n ... on SubscribeFeedError {\n status {\n code\n }\n }\n }\n}\n\nfragment FeedMeta on Feed {\n id\n title\n url\n updated\n links {\n nodes {\n ...Link\n }\n }\n}\n\nfragment Link on Link {\n href\n rel\n mediaType\n title \n}\n" ;
pub const QUERY : & str = "mutation SubscribeFeed($input: SubscribeFeedInput!) {\n subscribeFeed(input: $input) {\n __typename\n ... on SubscribeFeedSuccess {\n feed {\n ...FeedMeta\n }\n status {\n code\n }\n }\n ... on SubscribeFeedError {\n status {\n code\n }\n }\n }\n}\n\nfragment FeedMeta on Feed {\n id\n title\n url\n updated\n websiteUrl\n links {\n nodes {\n ...Link\n }\n }\n}\n\nfragment Link on Link {\n href\n rel\n mediaType\n title \n}\n" ;
use super::*;
use serde::{Deserialize, Serialize};
#[allow(dead_code)]
Expand Down Expand Up @@ -59,6 +59,8 @@ pub mod subscribe_feed {
pub title: Option<String>,
pub url: String,
pub updated: Option<Rfc3339Time>,
#[serde(rename = "websiteUrl")]
pub website_url: Option<String>,
pub links: FeedMetaLinks,
}
#[derive(Deserialize, Debug)]
Expand Down
4 changes: 3 additions & 1 deletion syndterm/src/client/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod subscription {
#![allow(dead_code)]
use std::result::Result;
pub const OPERATION_NAME: &str = "Subscription";
pub const QUERY : & str = "query Subscription($after: String, $first: Int) {\n output: subscription {\n feeds(after: $after, first: $first) {\n nodes {\n ...FeedMeta\n }\n pageInfo {\n hasNextPage\n endCursor\n }\n }\n }\n}\n\nfragment FeedMeta on Feed {\n id\n title\n url\n updated\n links {\n nodes {\n ...Link\n }\n }\n}\n\nfragment Link on Link {\n href\n rel\n mediaType\n title \n}\n" ;
pub const QUERY : & str = "query Subscription($after: String, $first: Int) {\n output: subscription {\n feeds(after: $after, first: $first) {\n nodes {\n ...FeedMeta\n }\n pageInfo {\n hasNextPage\n endCursor\n }\n }\n }\n}\n\nfragment FeedMeta on Feed {\n id\n title\n url\n updated\n websiteUrl\n links {\n nodes {\n ...Link\n }\n }\n}\n\nfragment Link on Link {\n href\n rel\n mediaType\n title \n}\n" ;
use super::*;
use serde::{Deserialize, Serialize};
#[allow(dead_code)]
Expand All @@ -28,6 +28,8 @@ pub mod subscription {
pub title: Option<String>,
pub url: String,
pub updated: Option<Rfc3339Time>,
#[serde(rename = "websiteUrl")]
pub website_url: Option<String>,
pub links: FeedMetaLinks,
}
#[derive(Deserialize, Debug)]
Expand Down
12 changes: 3 additions & 9 deletions syndterm/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub struct FeedMeta {
pub url: String,
pub updated: Option<Time>,
pub links: Vec<Link>,
pub website_url: Option<String>,
}

impl From<query::subscription::FeedMeta> for FeedMeta {
Expand All @@ -48,6 +49,7 @@ impl From<query::subscription::FeedMeta> for FeedMeta {
url: f.url,
updated: f.updated.map(parse_time),
links: f.links.nodes.into_iter().map(From::from).collect(),
website_url: f.website_url,
}
}
}
Expand All @@ -59,19 +61,11 @@ impl From<mutation::subscribe_feed::FeedMeta> for FeedMeta {
url: f.url,
updated: f.updated.map(parse_time),
links: f.links.nodes.into_iter().map(From::from).collect(),
website_url: f.website_url,
}
}
}

impl FeedMeta {
pub fn site_link(&self) -> Option<&str> {
self.links
.iter()
.find(|link| link.rel.as_deref() == Some("alternate"))
.map(|link| link.href.as_str())
}
}

fn parse_time(t: impl AsRef<str>) -> Time {
DateTime::parse_from_rfc3339(t.as_ref())
.expect("invalid rfc3339 time")
Expand Down
40 changes: 0 additions & 40 deletions syndterm/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,6 @@ pub fn render(frame: &mut Frame, mut cx: Context<'_>) {

cx.state.tabs.render(tabs_area, frame.buffer_mut(), &cx);

background::RgbSwatch.render(content_area, frame.buffer_mut());

match cx.state.tabs.current() {
Tab::Subscription => cx
.state
Expand Down Expand Up @@ -75,41 +73,3 @@ fn centered(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
])
.split(layout[1])[1]
}

// steal from https://github.com/ratatui-org/ratatui/blob/main/examples/demo2/colors.rs
mod background {
use palette::{IntoColor, Okhsv, Srgb};
use ratatui::{prelude::*, widgets::*};

/// A widget that renders a color swatch of RGB colors.
///
/// The widget is rendered as a rectangle with the hue changing along the x-axis from 0.0 to 360.0
/// and the value changing along the y-axis (from 1.0 to 0.0). Each pixel is rendered as a block
/// character with the top half slightly lighter than the bottom half.
pub struct RgbSwatch;

impl Widget for RgbSwatch {
fn render(self, area: Rect, buf: &mut Buffer) {
for (yi, y) in (area.top()..area.bottom()).enumerate() {
let value = area.height as f32 - yi as f32;
let value_fg = value / (area.height as f32);
let value_bg = (value - 0.5) / (area.height as f32);
for (xi, x) in (area.left()..area.right()).enumerate() {
let hue = xi as f32 * 360.0 / area.width as f32;
let fg = color_from_oklab(hue, Okhsv::max_saturation(), value_fg);
let bg = color_from_oklab(hue, Okhsv::max_saturation(), value_bg);
buf.get_mut(x, y).set_char('▀').set_fg(fg).set_bg(bg);
}
}
}
}

/// Convert a hue and value into an RGB color via the OkLab color space.
///
/// See <https://bottosson.github.io/posts/oklab/> for more details.
pub fn color_from_oklab(hue: f32, saturation: f32, value: f32) -> Color {
let color: Srgb = Okhsv::new(hue, saturation, value).into_color();
let color = color.into_format();
Color::Rgb(color.red, color.green, color.blue)
}
}
11 changes: 3 additions & 8 deletions syndterm/src/ui/subscription.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use ratatui::{
prelude::{Buffer, Margin, Rect},
prelude::{Buffer, Rect},
style::Style,
text::{Line, Span},
widgets::{Clear, List, ListItem, Widget},
widgets::{List, ListItem, Widget},
};

use crate::{client::query::subscription::SubscriptionOutput, types, ui::Context};
Expand Down Expand Up @@ -38,11 +38,6 @@ impl Subscription {

impl Subscription {
pub fn render(&self, area: Rect, buf: &mut Buffer, _cx: &Context<'_>) {
let area = area.inner(&Margin {
vertical: 1,
horizontal: 2,
});
Clear.render(area, buf);
let list = {
let items = self
.feed_metas
Expand All @@ -62,7 +57,7 @@ impl Subscription {
Style::default(),
),
Span::styled(
format!(" | {}", feed.site_link().unwrap_or("???")),
format!(" | {}", feed.website_url.as_deref().unwrap_or("???")),
Style::default(),
),
])
Expand Down

0 comments on commit cc5da3e

Please sign in to comment.