Repository: tarkah/tickrs (language: rust)

Issue:
Provide command line option to change refresh interval
Hey hey :wave:

it would be great to have an option like `-n`, `--interval` or something like that (similar to `watch`?) so we can change the data update interval. When we only want to show e.g. the 5Y data, it makes no sense to request new data every few seconds and re-draw it.


Discussion / hints:
So on the 5Y graph, there are 2 API connections that occur. One is for the graph data, which only refreshes every 24 hours since the data points are daily. The second connection queries the current market price every second. I'd think you still want to see live price fluctuation even when looking at a 5yr view?

The one change I can think of is having the current market price API disabled when outside market hours so it doesn't make any requests.

Does this make sense?
I'll add an interval option that can specify seconds. Interval, if passed, will be used to specify the current market price update speed.

Also, I'll use the max between the supplied interval and the default update for each timeframe.

For example, the 1 week time frame has an update interval of 5 minutes. If the user supplies an interval of 3 minutes, it'll still refresh every 5 since that's how often the API has new data. However if the user passes 10 minutes, it'll use 10 as the refresh interval for the graph data as well.

Fix patch:
diff --git a/Cargo.lock b/Cargo.lock
index a709e30..848a508 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1100,6 +1100,7 @@ dependencies = [
  "crossterm 0.17.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "futures 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
  "itertools 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "structopt 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
  "tickrs-api 0.3.4",
  "tui 0.9.5 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index fe7699c..3dd3609 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,6 +24,7 @@ crossbeam-channel = "0.4"
 chrono = "0.4"
 chrono-tz = "0.5"
 itertools = "0.9"
+lazy_static = "1.4"
 structopt = "0.3"
 
 async-std = "1.5"
diff --git a/src/cli.rs b/src/cli.rs
index 2f763f0..6af380a 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -1,6 +1,6 @@
 use structopt::StructOpt;
 
-#[derive(Debug, StructOpt)]
+#[derive(Debug, StructOpt, Clone)]
 #[structopt(
     name = "tickrs",
     about = "Realtime ticker data in your terminal 📈",
@@ -16,6 +16,9 @@ pub struct Opt {
     #[structopt(long)]
     /// Start in summary mode
     pub summary: bool,
+    #[structopt(short = "i", long, default_value = "1")]
+    /// Interval to update data from API (seconds)
+    pub update_interval: u64,
 }
 
 pub fn get_opts() -> Opt {
diff --git a/src/main.rs b/src/main.rs
index 662d2d1..3f799cb 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,6 +7,8 @@ use crossterm::event::{Event, MouseEvent};
 use crossterm::execute;
 use crossterm::terminal;
 
+use lazy_static::lazy_static;
+
 use tui::backend::CrosstermBackend;
 use tui::Terminal;
 
@@ -26,8 +28,13 @@ mod widget;
 use crate::app::DebugInfo;
 use crate::common::TimeFrame;
 
+lazy_static! {
+    pub static ref OPTS: cli::Opt = cli::get_opts();
+    pub static ref UPDATE_INTERVAL: u64 = OPTS.update_interval;
+}
+
 fn main() {
-    let opts = cli::get_opts();
+    let opts = OPTS.clone();
 
     better_panic::install();
 
diff --git a/src/task.rs b/src/task.rs
index dba13d3..f8bcbb4 100644
--- a/src/task.rs
+++ b/src/task.rs
@@ -1,3 +1,5 @@
+use crate::UPDATE_INTERVAL;
+
 use async_std::sync::Arc;
 use async_std::task;
 use crossbeam_channel::{bounded, select, unbounded, Receiver, Sender};
@@ -52,7 +54,7 @@ pub trait AsyncTask: 'static {
 
             // If no update interval is defined, exit task
             let update_interval = if let Some(interval) = update_interval {
-                interval
+                interval.max(Duration::from_secs(*UPDATE_INTERVAL))
             } else {
                 return;
             };


Test patch: