Skip to content

Commit

Permalink
feat(term): make entries limit configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed May 4, 2024
1 parent 60ec0f7 commit 206bbad
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 19 deletions.
22 changes: 18 additions & 4 deletions crates/synd_term/src/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,15 @@ pub enum Populate {
pub struct Config {
pub idle_timer_interval: Duration,
pub throbber_timer_interval: Duration,
pub entries_limit: usize,
}

impl Default for Config {
fn default() -> Self {
Self {
idle_timer_interval: Duration::from_secs(250),
throbber_timer_interval: Duration::from_millis(250),
entries_limit: config::feed::DEFAULT_ENTRIES_LIMIT,
}
}
}
Expand Down Expand Up @@ -455,12 +457,21 @@ impl Application {
payload.entries.as_slice(),
);
// paginate
if payload.page_info.has_next_page {
next = Some(Command::FetchEntries {
next = payload
.page_info
.has_next_page
.then(|| Command::FetchEntries {
after: payload.page_info.end_cursor.clone(),
first: config::client::INITIAL_ENTRIES_TO_FETCH,
first: self
.config
.entries_limit
.saturating_sub(
self.components.entries.count() + payload.entries.len(),
)
.min(payload.entries.len())
.try_into()
.unwrap_or(0),
});
}
self.components.entries.update_entries(populate, payload);
self.should_render = true;
}
Expand Down Expand Up @@ -747,6 +758,9 @@ impl Application {
impl Application {
#[tracing::instrument(skip(self))]
fn fetch_entries(&mut self, populate: Populate, after: Option<String>, first: i64) {
if first <= 0 {
return;
}
let client = self.client.clone();
let request_seq = self.in_flight.add(RequestId::FetchEntries);
let fut = async move {
Expand Down
29 changes: 23 additions & 6 deletions crates/synd_term/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,40 @@ impl From<Palette> for tailwind::Palette {
#[derive(Parser, Debug)]
#[command(version, propagate_version = true, name = "synd")]
pub struct Args {
/// `synd_api` endpoint
#[arg(long, global = true, default_value = config::api::ENDPOINT, env = config::env::ENDPOINT)]
pub endpoint: Url,
/// Log file path
#[arg(long, default_value = config::log_path().into_os_string(), env = config::env::LOG_PATH)]
pub log: PathBuf,
/// Color palette
#[arg(value_enum, long = "theme", default_value_t = Palette::Slate, env = config::env::THEME)]
pub palette: Palette,
#[command(subcommand)]
pub command: Option<Command>,
#[command(flatten)]
pub api: ApiOptions,
#[command(flatten)]
pub feed: FeedOptions,
}

#[derive(clap::Args, Debug)]
#[command(next_help_heading = "Api options")]
pub struct ApiOptions {
/// `synd_api` endpoint
#[arg(long, global = true, default_value = config::api::ENDPOINT, env = config::env::ENDPOINT)]
pub endpoint: Url,
/// Client timeout
#[arg(long, value_parser = parse_duration::parse, default_value = config::client::DEFAULT_TIMEOUT)]
pub timeout: Duration,
pub client_timeout: Duration,
}

#[derive(clap::Args, Debug)]
#[command(next_help_heading = "Feed options")]
pub struct FeedOptions {
/// categories.toml path
#[arg(long,aliases = ["category"],value_name = "CATEGORIES TOML PATH")]
pub categories: Option<PathBuf>,
#[command(subcommand)]
pub command: Option<Command>,
/// Feed entries limit to fetch
#[arg(long, aliases = ["max-entries"], default_value_t = config::feed::DEFAULT_ENTRIES_LIMIT)]
pub entries_limit: usize,
}

#[derive(Subcommand, Debug)]
Expand Down
7 changes: 6 additions & 1 deletion crates/synd_term/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub mod env {
pub const LOG_DIRECTIVE: &str = env_key!("LOG");

pub const ENDPOINT: &str = env_key!("ENDPOINT");
pub const LOG_PATH: &str = env_key!("LOG");
pub const LOG_PATH: &str = env_key!("LOG_PATH");
pub const THEME: &str = env_key!("THEME");
}

Expand All @@ -43,6 +43,11 @@ pub mod credential {
pub const FALLBACK_EXPIRE: Duration = Duration::from_secs(60 * 15);
}

pub mod feed {
/// Default entries limit to fetch
pub const DEFAULT_ENTRIES_LIMIT: usize = 200;
}

pub fn cache_dir() -> &'static Path {
project_dirs().cache_dir()
}
Expand Down
25 changes: 17 additions & 8 deletions crates/synd_term/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use anyhow::Context as _;
use crossterm::event::EventStream;
use futures_util::TryFutureExt;
use synd_term::{
application::Application,
cli::{self, Args, Palette},
application::{Application, Config},
cli::{self, ApiOptions, Args, FeedOptions, Palette},
client::Client,
config::{self, Categories},
terminal::Terminal,
Expand Down Expand Up @@ -64,15 +64,22 @@ async fn init_app(
endpoint: Url,
timeout: Duration,
palette: Palette,
categories: Option<PathBuf>,
FeedOptions {
categories,
entries_limit,
}: FeedOptions,
) -> anyhow::Result<Application> {
let terminal = Terminal::new().context("Failed to construct terminal")?;
let client = Client::new(endpoint, timeout).context("Failed to construct client")?;
let categories = categories
.map(Categories::load)
.transpose()?
.unwrap_or_else(Categories::default_toml);
let mut app = Application::new(terminal, client, categories)
let config = Config {
entries_limit,
..Default::default()
};
let mut app = Application::with(terminal, client, categories, config)
.with_theme(Theme::with_palette(&palette.into()));

if let Some(cred) = app.restore_credential().await {
Expand All @@ -85,12 +92,14 @@ async fn init_app(
#[tokio::main]
async fn main() {
let Args {
endpoint,
api: ApiOptions {
endpoint,
client_timeout,
},
feed,
log,
command,
palette,
categories,
timeout,
} = cli::parse();

let log = if command.is_some() { None } else { Some(log) };
Expand All @@ -108,7 +117,7 @@ async fn main() {

let mut event_stream = EventStream::new();

if let Err(err) = init_app(endpoint, timeout, palette, categories)
if let Err(err) = init_app(endpoint, client_timeout, palette, feed)
.and_then(|app| {
tracing::info!("Running...");
app.run(&mut event_stream)
Expand Down
5 changes: 5 additions & 0 deletions crates/synd_term/src/ui/components/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ impl Entries {
}
}

/// Return entries count
pub fn count(&self) -> usize {
self.entries.len()
}

pub fn update_entries(&mut self, action: Populate, payload: payload::FetchEntriesPayload) {
match action {
Populate::Append => self.entries.extend(payload.entries),
Expand Down
1 change: 1 addition & 0 deletions crates/synd_term/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ mod test {
let config = Config {
idle_timer_interval: Duration::from_millis(1000),
throbber_timer_interval: Duration::from_secs(3600), // disable throbber
..Default::default()
};
// or mpsc and tokio_stream ReceiverStream
let (tx, rx) = tokio::sync::mpsc::unbounded_channel();
Expand Down

0 comments on commit 206bbad

Please sign in to comment.