Skip to content

Commit f602f24

Browse files
virtualritztarkah
andauthored
Rust 2021, clippy clean, cargo fmt, replaced std::sync primitives (#140)
* Rust 2021, clipp clean, cargo fmt, swapped std::sync primitives for rclite & parking_lot alternatives. * Use static ssl --------- Co-authored-by: Cory Forsstrom <[email protected]>
1 parent db63944 commit f602f24

14 files changed

+550
-523
lines changed

Cargo.lock

+471-433
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+13-15
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
name = "tickrs"
33
version = "0.14.6"
44
authors = ["tarkah <[email protected]>"]
5-
edition = "2018"
5+
edition = "2021"
66
license = "MIT"
77
repository = "https://github.com/tarkah/tickrs"
88
readme = "README.md"
99
description = "Realtime ticker data in your terminal 📈"
10-
keywords = ["tui","terminal","stocks"]
10+
keywords = ["tui", "terminal", "stocks"]
1111
categories = ["command-line-utilities"]
1212

1313
[profile.release]
@@ -20,22 +20,20 @@ members = [
2020
]
2121

2222
[dependencies]
23-
tickrs-api = { path = "api/", version = "0.14.6" }
24-
2523
anyhow = "1.0"
26-
crossbeam-channel = "0.4"
24+
async-std = "1.12"
25+
better-panic = "0.3"
2726
chrono = "0.4"
28-
chrono-tz = "0.5"
27+
crossbeam-channel = "0.5"
28+
crossterm = "0.25" # use the same version as tui
2929
dirs-next = "2.0.0"
30-
itertools = "0.9"
30+
futures = "0.3"
31+
itertools = "0.10"
3132
lazy_static = "1.4"
33+
parking_lot = "0.12.1"
34+
rclite = "0.1.5"
3235
serde = { version = "1", features = ["derive"] }
33-
serde_yaml = "0.8"
36+
serde_yaml = "0.9"
3437
structopt = "0.3"
35-
36-
async-std = "1.5"
37-
futures = "0.3"
38-
39-
better-panic = "0.2"
40-
crossterm = "0.19"
41-
tui = { version = "0.14", default-features = false, features = ["crossterm","serde"] }
38+
tickrs-api = { path = "api/", version = "0.14.6" }
39+
tui = { version = "0.19", default-features = false, features = ["crossterm","serde"] }

api/Cargo.toml

+6-11
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,19 @@
22
name = "tickrs-api"
33
version = "0.14.6"
44
authors = ["tarkah <[email protected]>"]
5-
edition = "2018"
5+
edition = "2021"
66
license = "MIT"
77
description = "API for tickrs"
88
repository = "https://github.com/tarkah/tickrs"
99

10-
1110
[dependencies]
1211
anyhow = "1.0"
13-
chrono = { version = "0.4", features = ["serde"] }
14-
serde_urlencoded = "0.6"
15-
serde_json = "1.0"
16-
serde = { version = "1.0", features=["derive"] }
17-
1812
futures = "0.3"
19-
20-
http = "0.1"
21-
curl = { version = "0.4", default-features=false, features = ["static-ssl"] }
22-
isahc = "0.8"
13+
http = "0.2"
14+
isahc = { version = "1.7", features = ["static-ssl"] }
15+
serde = { version = "1.0", features = ["derive"] }
16+
serde_json = "1.0"
17+
serde_urlencoded = "0.7"
2318

2419
[dev-dependencies]
2520
async-std = { version = "1", features = ["attributes"] }

api/src/client.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::collections::HashMap;
33
use anyhow::{bail, Context, Result};
44
use futures::AsyncReadExt;
55
use http::Uri;
6-
use isahc::prelude::*;
6+
use isahc::HttpClient;
77
use serde::de::DeserializeOwned;
88

99
use crate::model::{Chart, ChartData, Company, CompanyData, Options, OptionsHeader};

rustfmt.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
edition = "2018"
1+
edition = "2021"
22
imports_granularity = "Module"
33
group_imports = "StdExternalCrate"

src/app.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl App {
3535
let mut timestamp_updates = self.default_timestamp_service.updates();
3636

3737
if let Some(new_defaults) = timestamp_updates.pop() {
38-
*DEFAULT_TIMESTAMPS.write().unwrap() = new_defaults;
38+
*DEFAULT_TIMESTAMPS.write() = new_defaults;
3939
}
4040
}
4141
}

src/common.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ impl TimeFrame {
187187
}
188188

189189
pub fn format_time(&self, timestamp: i64) -> String {
190-
let utc_date = Utc.timestamp(timestamp, 0);
190+
let utc_date = Utc.timestamp_opt(timestamp, 0).unwrap();
191191
let local_date = utc_date.with_timezone(&Local);
192192

193193
let fmt = match self {

src/draw.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ fn draw_summary<B: Backend>(frame: &mut Frame<B>, app: &mut App, mut area: Rect)
245245
area = add_padding(area, 1, PaddingDirection::All);
246246
area = add_padding(area, 1, PaddingDirection::Right);
247247

248-
let show_volumes = *SHOW_VOLUMES.read().unwrap() && app.chart_type != ChartType::Kagi;
248+
let show_volumes = *SHOW_VOLUMES.read() && app.chart_type != ChartType::Kagi;
249249
let stock_widget_height = if show_volumes { 7 } else { 6 };
250250

251251
let height = area.height;

src/event.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,12 @@ pub fn handle_key_bindings(
326326
}
327327
(_, KeyModifiers::NONE, KeyCode::Char('v')) => {
328328
if app.chart_type != ChartType::Kagi {
329-
let mut show_volumes = SHOW_VOLUMES.write().unwrap();
329+
let mut show_volumes = SHOW_VOLUMES.write();
330330
*show_volumes = !*show_volumes;
331331
}
332332
}
333333
(_, KeyModifiers::NONE, KeyCode::Char('p')) => {
334-
let mut guard = ENABLE_PRE_POST.write().unwrap();
334+
let mut guard = ENABLE_PRE_POST.write();
335335
*guard = !*guard;
336336
}
337337
(Mode::DisplaySummary, modifiers, keycode) => {
@@ -340,7 +340,7 @@ pub fn handle_key_bindings(
340340
}
341341
}
342342
(_, KeyModifiers::NONE, KeyCode::Char('x')) => {
343-
let mut show_x_labels = SHOW_X_LABELS.write().unwrap();
343+
let mut show_x_labels = SHOW_X_LABELS.write();
344344
*show_x_labels = !*show_x_labels;
345345
}
346346
(_, KeyModifiers::SHIFT, KeyCode::Left) | (_, KeyModifiers::NONE, KeyCode::Char('<')) => {

src/main.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
extern crate tickrs_api as api;
2-
31
use std::collections::HashMap;
4-
use std::sync::{Arc, Mutex, RwLock};
52
use std::time::Duration;
63
use std::{io, panic, thread};
74

85
use crossbeam_channel::{bounded, select, unbounded, Receiver, Sender};
96
use crossterm::event::{Event, MouseEvent, MouseEventKind};
107
use crossterm::{cursor, execute, terminal};
118
use lazy_static::lazy_static;
9+
use parking_lot::{Mutex, RwLock};
10+
use rclite::Arc;
1211
use service::default_timestamps::DefaultTimestampService;
12+
use tickrs_api as api;
1313
use tui::backend::CrosstermBackend;
1414
use tui::Terminal;
1515

@@ -114,13 +114,13 @@ fn main() {
114114
loop {
115115
select! {
116116
recv(redraw_requested) -> _ => {
117-
let mut app = app.lock().unwrap();
117+
let mut app = app.lock();
118118

119119
draw::draw(&mut terminal, &mut app);
120120
}
121121
// Default redraw on every duration
122122
default(Duration::from_millis(500)) => {
123-
let mut app = app.lock().unwrap();
123+
let mut app = app.lock();
124124

125125
// Drive animation of loading icon
126126
for stock in app.stocks.iter_mut() {
@@ -138,7 +138,7 @@ fn main() {
138138
// Notified that new data has been fetched from API, update widgets
139139
// so they can update their state with this new information
140140
recv(data_received) -> _ => {
141-
let mut app = app.lock().unwrap();
141+
let mut app = app.lock();
142142

143143
app.update();
144144

@@ -151,11 +151,11 @@ fn main() {
151151
}
152152
}
153153
recv(ui_events) -> message => {
154-
let mut app = app.lock().unwrap();
154+
let mut app = app.lock();
155155

156156
if app.debug.enabled {
157-
if let Ok(event) = message {
158-
app.debug.last_event = Some(event);
157+
if let Ok(ref event) = message {
158+
app.debug.last_event = Some(event.clone());
159159
}
160160
}
161161

src/widget/chart_configuration.rs

+30-30
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::THEME;
1818
#[derive(Default, Debug, Clone)]
1919
pub struct ChartConfigurationState {
2020
pub input: Input,
21-
pub selection: Option<Selection>,
21+
pub selection: Option<KagiSelection>,
2222
pub error_message: Option<String>,
2323
pub kagi_options: KagiOptions,
2424
pub cache_state: CacheState,
@@ -27,7 +27,7 @@ pub struct ChartConfigurationState {
2727
impl ChartConfigurationState {
2828
pub fn add_char(&mut self, c: char) {
2929
let input_field = match self.selection {
30-
Some(Selection::KagiReversalValue) => &mut self.input.kagi_reversal_value,
30+
Some(KagiSelection::ReversalValue) => &mut self.input.kagi_reversal_value,
3131
_ => return,
3232
};
3333

@@ -41,7 +41,7 @@ impl ChartConfigurationState {
4141

4242
pub fn del_char(&mut self) {
4343
let input_field = match self.selection {
44-
Some(Selection::KagiReversalValue) => &mut self.input.kagi_reversal_value,
44+
Some(KagiSelection::ReversalValue) => &mut self.input.kagi_reversal_value,
4545
_ => return,
4646
};
4747

@@ -50,14 +50,14 @@ impl ChartConfigurationState {
5050

5151
fn get_tab_artifacts(&mut self) -> Option<(&mut usize, usize)> {
5252
let tab_field = match self.selection {
53-
Some(Selection::KagiReversalType) => &mut self.input.kagi_reversal_type,
54-
Some(Selection::KagiPriceType) => &mut self.input.kagi_price_type,
53+
Some(KagiSelection::ReversalType) => &mut self.input.kagi_reversal_type,
54+
Some(KagiSelection::PriceType) => &mut self.input.kagi_price_type,
5555
_ => return None,
5656
};
5757

5858
let mod_value = match self.selection {
59-
Some(Selection::KagiReversalType) => 2,
60-
Some(Selection::KagiPriceType) => 2,
59+
Some(KagiSelection::ReversalType) => 2,
60+
Some(KagiSelection::PriceType) => 2,
6161
_ => 1,
6262
};
6363
Some((tab_field, mod_value))
@@ -155,21 +155,21 @@ impl ChartConfigurationState {
155155

156156
pub fn selection_up(&mut self) {
157157
let new_selection = match self.selection {
158-
None => Selection::KagiReversalValue,
159-
Some(Selection::KagiReversalValue) => Selection::KagiReversalType,
160-
Some(Selection::KagiReversalType) => Selection::KagiPriceType,
161-
Some(Selection::KagiPriceType) => Selection::KagiReversalValue,
158+
None => KagiSelection::ReversalValue,
159+
Some(KagiSelection::ReversalValue) => KagiSelection::ReversalType,
160+
Some(KagiSelection::ReversalType) => KagiSelection::PriceType,
161+
Some(KagiSelection::PriceType) => KagiSelection::ReversalValue,
162162
};
163163

164164
self.selection = Some(new_selection);
165165
}
166166

167167
pub fn selection_down(&mut self) {
168168
let new_selection = match self.selection {
169-
None => Selection::KagiPriceType,
170-
Some(Selection::KagiPriceType) => Selection::KagiReversalType,
171-
Some(Selection::KagiReversalType) => Selection::KagiReversalValue,
172-
Some(Selection::KagiReversalValue) => Selection::KagiPriceType,
169+
None => KagiSelection::PriceType,
170+
Some(KagiSelection::PriceType) => KagiSelection::ReversalType,
171+
Some(KagiSelection::ReversalType) => KagiSelection::ReversalValue,
172+
Some(KagiSelection::ReversalValue) => KagiSelection::PriceType,
173173
};
174174

175175
self.selection = Some(new_selection);
@@ -213,7 +213,7 @@ impl ChartConfigurationState {
213213
})
214214
.unwrap_or(0);
215215

216-
self.selection = Some(Selection::KagiPriceType);
216+
self.selection = Some(KagiSelection::PriceType);
217217
self.input.kagi_reversal_value = format!("{:.2}", reversal_amount);
218218
self.input.kagi_reversal_type = reversal_type;
219219
self.input.kagi_price_type = price_type;
@@ -252,10 +252,10 @@ pub enum KagiReversalOption {
252252
}
253253

254254
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
255-
pub enum Selection {
256-
KagiPriceType,
257-
KagiReversalType,
258-
KagiReversalValue,
255+
pub enum KagiSelection {
256+
PriceType,
257+
ReversalType,
258+
ReversalValue,
259259
}
260260

261261
pub struct ChartConfigurationWidget {
@@ -358,7 +358,7 @@ fn render_kagi_options(mut area: Rect, buf: &mut Buffer, state: &mut ChartConfig
358358
Spans::default(),
359359
Spans::from(vec![
360360
Span::styled(
361-
if state.selection == Some(Selection::KagiPriceType) {
361+
if state.selection == Some(KagiSelection::PriceType) {
362362
"> "
363363
} else {
364364
" "
@@ -370,7 +370,7 @@ fn render_kagi_options(mut area: Rect, buf: &mut Buffer, state: &mut ChartConfig
370370
Spans::default(),
371371
Spans::from(vec![
372372
Span::styled(
373-
if state.selection == Some(Selection::KagiReversalType) {
373+
if state.selection == Some(KagiSelection::ReversalType) {
374374
"> "
375375
} else {
376376
" "
@@ -382,7 +382,7 @@ fn render_kagi_options(mut area: Rect, buf: &mut Buffer, state: &mut ChartConfig
382382
Spans::default(),
383383
Spans::from(vec![
384384
Span::styled(
385-
if state.selection == Some(Selection::KagiReversalValue) {
385+
if state.selection == Some(KagiSelection::ReversalValue) {
386386
"> "
387387
} else {
388388
" "
@@ -400,7 +400,7 @@ fn render_kagi_options(mut area: Rect, buf: &mut Buffer, state: &mut ChartConfig
400400
"Close",
401401
style().fg(THEME.text_normal()).bg(
402402
match (state.selection, state.input.kagi_price_type) {
403-
(Some(Selection::KagiPriceType), 0) => THEME.highlight_focused(),
403+
(Some(KagiSelection::PriceType), 0) => THEME.highlight_focused(),
404404
(_, 0) => THEME.highlight_unfocused(),
405405
(_, _) => THEME.background(),
406406
},
@@ -411,7 +411,7 @@ fn render_kagi_options(mut area: Rect, buf: &mut Buffer, state: &mut ChartConfig
411411
"High / Low",
412412
style().fg(THEME.text_normal()).bg(
413413
match (state.selection, state.input.kagi_price_type) {
414-
(Some(Selection::KagiPriceType), 1) => THEME.highlight_focused(),
414+
(Some(KagiSelection::PriceType), 1) => THEME.highlight_focused(),
415415
(_, 1) => THEME.highlight_unfocused(),
416416
(_, _) => THEME.background(),
417417
},
@@ -424,7 +424,7 @@ fn render_kagi_options(mut area: Rect, buf: &mut Buffer, state: &mut ChartConfig
424424
"Pct",
425425
style().fg(THEME.text_normal()).bg(
426426
match (state.selection, state.input.kagi_reversal_type) {
427-
(Some(Selection::KagiReversalType), 0) => THEME.highlight_focused(),
427+
(Some(KagiSelection::ReversalType), 0) => THEME.highlight_focused(),
428428
(_, 0) => THEME.highlight_unfocused(),
429429
(_, _) => THEME.background(),
430430
},
@@ -435,7 +435,7 @@ fn render_kagi_options(mut area: Rect, buf: &mut Buffer, state: &mut ChartConfig
435435
"Amount",
436436
style().fg(THEME.text_normal()).bg(
437437
match (state.selection, state.input.kagi_reversal_type) {
438-
(Some(Selection::KagiReversalType), 1) => THEME.highlight_focused(),
438+
(Some(KagiSelection::ReversalType), 1) => THEME.highlight_focused(),
439439
(_, 1) => THEME.highlight_unfocused(),
440440
(_, _) => THEME.background(),
441441
},
@@ -446,12 +446,12 @@ fn render_kagi_options(mut area: Rect, buf: &mut Buffer, state: &mut ChartConfig
446446
Spans::from(vec![Span::styled(
447447
format!("{: <22}", &state.input.kagi_reversal_value),
448448
style()
449-
.fg(if state.selection == Some(Selection::KagiReversalValue) {
449+
.fg(if state.selection == Some(KagiSelection::ReversalValue) {
450450
THEME.text_secondary()
451451
} else {
452452
THEME.text_normal()
453453
})
454-
.bg(if state.selection == Some(Selection::KagiReversalValue) {
454+
.bg(if state.selection == Some(KagiSelection::ReversalValue) {
455455
THEME.highlight_unfocused()
456456
} else {
457457
THEME.background()
@@ -468,7 +468,7 @@ fn render_kagi_options(mut area: Rect, buf: &mut Buffer, state: &mut ChartConfig
468468
.render(layout[2], buf);
469469

470470
// Set "cursor" color
471-
if matches!(state.selection, Some(Selection::KagiReversalValue)) {
471+
if matches!(state.selection, Some(KagiSelection::ReversalValue)) {
472472
let size = terminal::size().unwrap_or((0, 0));
473473

474474
let x = layout[2].left() as usize + state.input.kagi_reversal_value.len().min(20);

0 commit comments

Comments
 (0)