Repository: tarkah/tickrs (language: rust)

Issue:
Provide command line option to select time resolution at startup
Hey hey :wave:

It would be amazing to be able to select the time resolution from the command line already.

For example: you have a raspberry pi zero with a display and want it to start up with a specific time resolution automatically on boot.


Discussion / hints:
Great idea, I'll get this one added no problem!

Fix patch:
diff --git a/src/cli.rs b/src/cli.rs
index 6af380a..7139f5a 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -1,3 +1,5 @@
+use crate::common::TimeFrame;
+
 use structopt::StructOpt;
 
 #[derive(Debug, StructOpt, Clone)]
@@ -19,6 +21,9 @@ pub struct Opt {
     #[structopt(short = "i", long, default_value = "1")]
     /// Interval to update data from API (seconds)
     pub update_interval: u64,
+    #[structopt(short = "t", long, default_value = "1D", possible_values(&["1D", "1W", "1M", "3M", "6M", "1Y", "5Y"]))]
+    /// Use specified time frame when starting program and when new stocks are added
+    pub time_frame: TimeFrame,
 }
 
 pub fn get_opts() -> Opt {
diff --git a/src/common.rs b/src/common.rs
index 6acd186..5333147 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -1,8 +1,10 @@
 use api::{model::ChartData, Range};
 use itertools::izip;
+
+use std::str::FromStr;
 use std::time::Duration;
 
-#[derive(PartialEq, Clone, Copy, PartialOrd)]
+#[derive(PartialEq, Clone, Copy, PartialOrd, Debug)]
 pub enum TimeFrame {
     Day1,
     Week1,
@@ -13,6 +15,25 @@ pub enum TimeFrame {
     Year5,
 }
 
+impl FromStr for TimeFrame {
+    type Err = &'static str;
+
+    fn from_str(s: &str) -> Result<Self, Self::Err> {
+        use TimeFrame::*;
+
+        match s {
+            "1D" => Ok(Day1),
+            "1W" => Ok(Week1),
+            "1M" => Ok(Month1),
+            "3M" => Ok(Month3),
+            "6M" => Ok(Month6),
+            "1Y" => Ok(Year1),
+            "5Y" => Ok(Year5),
+            _ => Err("Valid time frames are: '1D', '1W', '1M', '3M', '6M', '1Y', '5Y'"),
+        }
+    }
+}
+
 impl TimeFrame {
     pub fn idx(self) -> usize {
         match self {
diff --git a/src/main.rs b/src/main.rs
index 3f799cb..3068439 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -31,6 +31,7 @@ use crate::common::TimeFrame;
 lazy_static! {
     pub static ref OPTS: cli::Opt = cli::get_opts();
     pub static ref UPDATE_INTERVAL: u64 = OPTS.update_interval;
+    pub static ref TIME_FRAME: TimeFrame = OPTS.time_frame;
 }
 
 fn main() {
@@ -83,7 +84,7 @@ fn main() {
         } else {
             app::Mode::DisplayStock
         },
-        summary_time_frame: TimeFrame::Day1,
+        summary_time_frame: opts.time_frame,
     };
 
     for stock in app.stocks.iter_mut() {
diff --git a/src/widget/stock.rs b/src/widget/stock.rs
index 7422fc7..c6e5e61 100644
--- a/src/widget/stock.rs
+++ b/src/widget/stock.rs
@@ -2,6 +2,7 @@ use super::{block, OptionsState};
 use crate::common::*;
 use crate::draw::{add_padding, PaddingDirection};
 use crate::service::{self, Service};
+use crate::TIME_FRAME;
 
 use api::model::CompanyData;
 
@@ -26,7 +27,7 @@ pub struct StockState {
 
 impl StockState {
     pub fn new(symbol: String) -> StockState {
-        let time_frame = TimeFrame::Day1;
+        let time_frame = *TIME_FRAME;
 
         let stock_service = service::stock::StockService::new(symbol.clone(), time_frame);
 


Test patch: