Skip to content

Commit

Permalink
Display option & stock currency instead of assuming USD (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
MabezDev authored Aug 23, 2020
1 parent ac15d0d commit f5c15b9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 26 deletions.
2 changes: 2 additions & 0 deletions api/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub struct CompanyPrice {
pub long_name: String,
pub regular_market_price: CompanyRegularMarketPrice,
pub regular_market_previous_close: CompanyRegularMarketPreviousClose,
pub currency: Option<String>,
}

#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -206,6 +207,7 @@ pub struct OptionsContract {
pub ask: Option<f32>,
pub implied_volatility: Option<f32>,
pub in_the_money: Option<bool>,
pub currency: Option<String>,
}

fn deserialize_vec<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
Expand Down
33 changes: 18 additions & 15 deletions src/widget/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ impl OptionsState {

self.exp_dates = dates;

if prev_len == 0 {
if prev_len == 0 && !self.exp_dates.is_empty() {
self.set_exp_date(self.exp_dates[0]);
}
}
Expand Down Expand Up @@ -323,8 +323,8 @@ impl StatefulWidget for OptionsWidget {
let rows = selected_data.iter().map(|d| {
Row::StyledData(
vec![
format!("${: <7.2}", d.strike),
format!("${: <7.2}", d.last_price),
format!("{: <7.2}", d.strike),
format!("{: <7.2}", d.last_price),
format!("{: >7.2}%", d.percent_change),
]
.into_iter(),
Expand Down Expand Up @@ -392,10 +392,12 @@ impl StatefulWidget for OptionsWidget {

columns[1] = add_padding(columns[1], 2, PaddingDirection::Left);

let gap_strike = 18 - (format!("${:.2}", option.strike).len() + 7);
let gap_last = 18 - (format!("${:.2}", option.last_price).len() + 6);
let gap_ask = 18 - (format!("${:.2}", option.ask.unwrap_or_default()).len() + 4);
let gap_bid = 18 - (format!("${:.2}", option.bid.unwrap_or_default()).len() + 4);
let currency = option.currency.as_deref().unwrap_or("USD");

let gap_strike = 19 - (format!("{:.2} {}", option.strike, currency).len() + 7);
let gap_last = 15 - (format!("{:.2}", option.last_price).len() + 6);
let gap_ask = 15 - (format!("{:.2}", option.ask.unwrap_or_default()).len() + 4);
let gap_bid = 15 - (format!("{:.2}", option.bid.unwrap_or_default()).len() + 4);
let gap_volume = 18 - (format!("{}", option.volume.unwrap_or_default()).len() + 7);
let gap_open_int =
18 - (format!("{}", option.open_interest.unwrap_or_default()).len() + 9);
Expand All @@ -409,24 +411,25 @@ impl StatefulWidget for OptionsWidget {

let column_0 = [
Text::raw(format!(
"strike:{}${:.2}\n\n",
"strike:{}{:.2} {}\n\n",
" ".repeat(gap_strike),
option.strike
option.strike,
currency
)),
Text::raw(format!(
"price:{}${:.2}\n\n",
"price:{}{:.2}\n\n",
" ".repeat(gap_last),
option.last_price
option.last_price,
)),
Text::raw(format!(
"bid:{}${:.2}\n\n",
"bid:{}{:.2}\n\n",
" ".repeat(gap_ask),
option.bid.unwrap_or_default()
option.bid.unwrap_or_default(),
)),
Text::raw(format!(
"ask:{}${:.2}",
"ask:{}{:.2}",
" ".repeat(gap_bid),
option.ask.unwrap_or_default()
option.ask.unwrap_or_default(),
)),
];

Expand Down
22 changes: 11 additions & 11 deletions src/widget/stock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,16 @@ impl StatefulWidget for StockWidget {
fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
let pct_change = state.pct_change();

let (company_name, currency) = match state.profile.as_ref() {
Some(profile) => (
profile.price.short_name.as_str(),
profile.price.currency.as_deref().unwrap_or("USD"),
),
None => ("", ""),
};

// Draw widget block
{
let company_name = match state.profile.as_ref() {
Some(profile) => &profile.price.short_name,
None => "",
};

block::new(&format!(" {} - {} ", state.symbol, company_name), None).render(area, buf);
}

Expand Down Expand Up @@ -231,7 +234,7 @@ impl StatefulWidget for StockWidget {
let company_info = [
Text::raw("c: "),
Text::styled(
format!("${:.2}", state.current_price),
format!("{:.2} {}", state.current_price, currency),
Style::default().modifier(Modifier::BOLD).fg(Color::Yellow),
),
Text::styled(
Expand All @@ -246,14 +249,11 @@ impl StatefulWidget for StockWidget {
),
Text::raw("h: "),
Text::styled(
format!("${:.2}\n", high),
format!("{:.2}\n", high),
Style::default().fg(Color::LightCyan),
),
Text::raw("l: "),
Text::styled(
format!("${:.2}", low),
Style::default().fg(Color::LightCyan),
),
Text::styled(format!("{:.2}", low), Style::default().fg(Color::LightCyan)),
];

let expand_info = [
Expand Down

0 comments on commit f5c15b9

Please sign in to comment.