coinlive 0.2.5

Interactive command line tool that displays live cryptocurrency prices and simple historical price charts.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! The UI is made of different pages
#![allow(dead_code)]

/// The help page
pub mod help;
/// The about page
pub mod about;
/// The price list page
pub mod price_list;
/// The price table page
pub mod price_table;
/// The graph page
pub mod graph;
/// The search page
pub mod search;
/// Pretty printing of floats and Decimal
pub mod nice;

use crate::utils::*;
use std::cell::RefCell;
use std::rc::Rc;
use tui::{
    backend::Backend,
    style::{Style, Color, Modifier},
    widgets::{Paragraph},
    layout::{Layout, Constraint, Direction, Rect, Alignment},
    text::{Span, Spans},
    terminal::Frame,
};
use tokio::sync::mpsc::{UnboundedSender, unbounded_channel};
use std::collections::HashMap;
use chrono::Local;
use std::marker::Copy;
use dec::Decimal64;
use inlinable_string::{InlineString};

/// Stores the relevant market data with some extra rendering information
pub struct MarketState {
    px: Decimal64,
    ts: u64,
    last_px: Decimal64,
    px_24h: Decimal64,
}

impl MarketState {
    /// Create new `MarketState` with NANs.
    fn new() -> Self {
        MarketState { px: Decimal64::NAN, ts: 0, last_px: Decimal64::NAN, px_24h:Decimal64::NAN }
    }
    /// Update `MarketState` with data from `Update`
    fn update(self: &mut Self, update: &Update) {
        self.last_px = self.px;
        self.px = update.px;
        self.px_24h = update.px_24h;
        self.ts = update.ts;
    }
    /// Make a nicely formatted price string
    pub fn price_string(self: &Self) -> String {
        fmt_dec(self.px)
    }
    /// Make a percentage string that has 6 width !TODO! improve
    pub fn percentage_string(self: &Self) -> String {
        let hundred: Decimal64 = "100".parse().expect("INTERNAL ERROR");
        let p = (self.last_px-self.px_24h)/self.px_24h;
        let mut s = if p.is_infinite() || p.is_nan() {
            String::from("-")
        } else {
            format!("{}", p*hundred)
        };
        if p.is_positive() && !(p.is_infinite() || p.is_nan()) { 
            s.insert_str(0,"+");
        } 
        s.truncate(6);
        format!("{:>6}", s)
    } 
    /// Generate a style for this price
    pub fn style(self: &Self) -> Style {
        if self.px > self.last_px {
            Style::default().fg(Color::Green)
        } else if self.px < self.last_px {
            Style::default().fg(Color::Red)
        } else {
            Style::default()
        }
    }
    /// Generate a style for this percentage
    pub fn style_percent(self: &Self) -> Style {
        if self.px > self.px_24h {
            Style::default().fg(Color::Green)
        } else if self.px < self.px_24h {
            Style::default().fg(Color::Red)
        } else {
            Style::default()
        }
    }
}

/// Messages that the `UI` can receive
#[derive(Debug)]
pub enum Msg {
    WS(u64, String),    // timestamp (millis) and websocket data
    Infos(Vec<Info>),   // Downloaded infos for each symbol
    Msg(String),        // info message to UI
    PriceList,          // On 'l' key press show PriceList
    PriceTable,         // On 't' key press show PriceTable
    Graph(Option<u32>), // On 'g' display graph with given time scale, or stored time scale if Nothing
    TogglePercent,      // On '%' key press
    ToggleExtended,     // On 'x' key press
    Search,             // On 's' show the search widget
    ArrowUp,            // On arrow up
    ArrowDown,          // On arrow down
    ArrowLeft,          // On srrow left
    ArrowRight,         // On arrow right
    Home,               // Home Home key reset cursor to top left
    Enter,              // On pressing enter
    Help,               // On 'h' key press show help
    About,              // On 'a' key press show about page
    Esc,                // On ESC go back to previous page
    Stop                // stop ui
}

/// Just tui::Terminal<...>
type Term = tui::Terminal<tui::backend::TermionBackend<termion::raw::RawTerminal<std::io::Stdout>>>;

/// All the different pages
#[derive(Debug, Clone, PartialEq, PartialOrd)]
enum UIView {
    PriceList,  // display PriceList
    PriceTable, // display PriceTable
    Graph,      // display graph
    Search,     // display search widget
    Empty,      // display PriceTable
    Help,       // display help
    About,      // display help
}

impl Copy for UIView { }


/// Current state of the `UI`
pub struct UIState {
    message: String,
    markets: HashMap<Symbol, MarketState>,
    latency: u64,
    ui_mode: UIView,
    ui_mode_back: Option<UIView>,       // where to go back to if ESC is pressed
    show_percent: bool,                 // 
    extended: bool,                     // extended view of table page
    ts_last_update: u64,                // ts of last market update
    lookup: Option<HashMap<Symbol, Info>>,
    infos: Option<Vec<Info>>,
    klines: Option<Vec<Bar>>,
    symbol: Symbol,
    time_scale: u32,                    // time scale for graph
    cursor_ix: u16,                     // x position of symbol in search widget
    cursor_iy: u16,                     // y position of symbol in search widget
}

impl UIState {
    /// New `UIState` with empty fields, 0 latency, ui_mode `PriceList`
    fn new() -> Self {
        UIState { 
            message: String::new(), 
            markets: HashMap::new(),
            latency: 0,
            ui_mode: UIView::Empty,
            ui_mode_back: None,
            show_percent: false,
            extended: true,
            ts_last_update: 0,
            lookup: None,
            infos: None,
            klines: None,
            symbol: InlineString::from("BTCUSDT"),
            time_scale: 0,
            cursor_ix: 0,
            cursor_iy: 0,
        }
    }
    fn update(self: &mut Self, updates: &Vec<Update>) {
        if let Some(lookup) = &self.lookup {
            for u in updates {
                if u.ts > self.ts_last_update { self.ts_last_update = u.ts; }
                let info = lookup.get(&u.symbol);
                if let Some(_) = info {
                    self.markets.entry(u.symbol.clone()).or_insert(MarketState::new()).update(&u);
                }
            }
        }
    }
}
/// Encapsulates the `UI`
pub struct UI {
    pub tx: UnboundedSender<Msg>,
    pub handle: tokio::task::JoinHandle<()>,
}

impl UI {
    /// Create new `UI`
    pub fn new(mut terminal: Term) -> Self {
        terminal.clear().expect("Terminal failed!");
        let (tx, mut rx) = unbounded_channel();
        let handle = tokio::spawn( async move {
            let mut state = UIState::new();
            let mut buf: Vec<Update> = Vec::with_capacity(2000);    // buffer for parse_updates
            let mut cursor_moved: bool = false;                     // used for setting message after draw is done
            while let Some(msg) = rx.recv().await {
                match msg {
                    Msg::Infos(infos_) => {
                        state.infos = Some(infos_.iter().cloned().filter(|i| i.quote != "TUSD" && i.quote != "BUSD" && i.quote != "USDC").collect());
                        state.lookup = Some(infos_to_lookup(&infos_));
                        state.ui_mode = UIView::PriceList;
                    },
                    Msg::WS(ts_rec, msg) => {
                        if let Ok(us) = parse_updates(&msg, &mut buf) {
                            state.update(&us);
                        } else if let Ok(ts) = msg.parse::<u64>() {
                            state.latency = ts_rec-ts;
                        } else {
                            state.message = format!("{:?}", msg);
                            break;
                        }
                    },
                    Msg::Msg(msg) => {
                        state.message = msg;
                    },
                    Msg::PriceList => {
                        state.ui_mode = UIView::PriceList;
                        state.message = String::from("Show price list");
                    },
                    Msg::PriceTable => {
                        state.ui_mode = UIView::PriceTable;
                        state.message = String::from("Show price table");
                    },
                    Msg::Graph(scale) => {
                        state.time_scale = scale.unwrap_or(state.time_scale);
                        UI::graph(&mut state, &mut terminal).await;
                    },
                    Msg::Search => {
                        state.ui_mode_back = Some(state.ui_mode);
                        state.ui_mode = UIView::Search;
                        state.message = String::from("Select symbol");
                    },
                    Msg::ArrowUp => {
                        if state.ui_mode == UIView::Search {
                            if state.cursor_iy > 0 { 
                                state.cursor_iy -= 1;
                                cursor_moved = true;
                            }
                        }
                    },
                    Msg::ArrowDown => {
                        if state.ui_mode == UIView::Search {
                            state.cursor_iy += 1;   // ! height needs to be checked elsewhere!
                            cursor_moved = true;
                        }
                    },
                    Msg::ArrowLeft => {
                        if state.ui_mode == UIView::Search {
                            if state.cursor_ix > 0 { 
                                state.cursor_ix -= 1;
                                cursor_moved = true;
                            }
                        }
                    },
                    Msg::ArrowRight => {
                        if state.ui_mode == UIView::Search {
                            state.cursor_ix += 1;   // ! width needs to be checked elsewhere!
                            cursor_moved = true;
                        }
                    },
                    Msg::Home => {
                        if state.ui_mode == UIView::Search {
                            state.cursor_ix = 0;
                            state.cursor_iy = 0;
                            cursor_moved = true;
                        }
                    },
                    Msg::Enter => {
                        if state.ui_mode == UIView::Search {
                            state.message = format!("Graph {}", state.symbol);
                            state.ui_mode_back = Some(state.ui_mode);
                            state.ui_mode = UIView::Graph;
                            UI::graph(&mut state, &mut terminal).await;
                        }
                    },
                    Msg::TogglePercent => {
                        state.show_percent = !state.show_percent;
                        if state.show_percent { state.message = String::from("Show %"); }
                        else { state.message = String::from("Show prices"); }
                    },
                    Msg::ToggleExtended => {
                        state.extended = !state.extended;
                        if state.extended { state.message = String::from("Show extended"); }
                        else { state.message = String::from("Show reduced"); }
                    },
                    Msg::Help => {
                        state.ui_mode_back = Some(state.ui_mode);
                        state.ui_mode = UIView::Help;
                        state.message = String::from("Help");
                    },
                    Msg::About => {
                        state.ui_mode_back = Some(state.ui_mode);
                        state.ui_mode = UIView::About;
                        state.message = String::from("About");
                    },
                    Msg::Esc => {
                        state.ui_mode = state.ui_mode_back.unwrap_or(UIView::PriceList);
                        state.ui_mode_back = None;
                        state.message.clear();
                    },
                    Msg::Stop => { 
                        state.message = String::from("Stop");
                        UI::draw(&mut state, &mut terminal);
                        return; 
                    }
                }
                UI::draw(&mut state, &mut terminal); 
                if cursor_moved {
                    state.message = format!("SEL {}", state.symbol);
                    cursor_moved = false;
                }
            }
        });
        UI { tx: tx, handle: handle }
    }
    /// Draw Graph
    pub async fn graph(mut state: &mut UIState, mut terminal: &mut Term) {
        let interval: Interval = match state.time_scale {
            1 => Interval::I5m,
            2 => Interval::I15m,
            3 => Interval::I30m,
            4 => Interval::I1h,
            5 => Interval::I2h,
            6 => Interval::I4h,
            7 => Interval::I8h,
            8 => Interval::I12h,
            9 => Interval::I1d,
            _ => Interval::I1m,
        };
        state.message = format!("Getting {} klines for {}", interval.str(), state.symbol);
        UI::draw(&mut state, &mut terminal);
        match get_klines(&state.symbol, &interval).await {
            Ok(klines) => {
                state.ui_mode = UIView::Graph;
                state.message = format!("Show {} klines for {}", interval.str(), state.symbol);
                state.klines = Some(klines);
            },
            Err(e) => {
                state.message = format!("Failed to get klines: {:?}", e);
            }
        }
    }
    /// Draw `UI`
    fn draw(state: &mut UIState, terminal: &mut Term) {
        terminal.draw(|f| {
            let size = f.size();
            let chunks = Layout::default()
                .direction(Direction::Vertical)
                .margin(0)
                .constraints(
                    [
                        Constraint::Min(0),
                        Constraint::Length(1),
                    ].as_ref()
                )
                .split(size);
            match state.ui_mode {
                UIView::PriceList => {
                    if let Some(infos) = &state.infos {
                        let price_list = price_list::PriceList::new(&infos, &state.markets, state.show_percent);
                        f.render_widget(price_list, chunks[0]);
                    }
                },
                UIView::PriceTable => {
                    if let Some(infos) = &state.infos {
                        let price_table = price_table::PriceTable::new(&infos, &state.markets, state.show_percent, state.extended);
                        f.render_widget(price_table, chunks[0]);
                    }
                },
                UIView::Graph => {
                    if let Some(infos) = &mut state.infos {
                        if let Some(klines) = &state.klines {
                            let graph = graph::Graph::new(&infos, klines, Interval::I1m, state.symbol.clone());
                            f.render_widget(graph, chunks[0]);
                        }
                    }
                },
                UIView::Search => {
                    // The `Search` object needs to be able to modify i_symbol and cursor (ix, iy), so
                    // we use interior mutability via Rc<RefCell<...>>.
                    // - i_symbol is the index of the selected symbol
                    // - cursor is unchanged unless the display bounds are be exceeded
                    if let Some(infos) = &state.infos {
                        let ref_i_symbol = Rc::new(RefCell::new(0));
                        let ref_cursor = Rc::new(RefCell::new((state.cursor_ix,state.cursor_iy)));
                        let search = search::Search::new(infos, ref_i_symbol.clone(), ref_cursor.clone());
                        f.render_widget(search, chunks[0]);
                        // Now stick cursor (ix, iy) back into state
                        let (ix, iy) = (*ref_cursor).take();
                        state.cursor_ix = ix; state.cursor_iy = iy;
                        // Finally adjust state.symbol if necessary
                        if let Some(infos) = &state.infos {
                            let i_symbol: usize = (*ref_i_symbol).take();
                            if i_symbol < infos.len() { // check bounds just in case
                                let symbol = &infos[i_symbol].symbol;
                                state.symbol = symbol.clone();
                            }
                        }
                    }
                },
                UIView::Empty => {
                    // draw splash screen
                    about::draw_about(f, chunks[0]);
                }
                UIView::Help => {
                    f.render_widget(help::help(), chunks[0]);
                },
                UIView::About => {
                    about::draw_about(f, chunks[0]);
                }
            }
            UI::draw_message_bar(f, state, chunks[1]);
        }).expect("Failed to draw!");
    }
    /// Draw the message bar at the bottom
    fn draw_message_bar<B: Backend>(f: &mut Frame<B>, state: &UIState, area: Rect) {
        // layout horizontally into three pieces:
        // - current time
        // - state.message
        // - latency (floating right)
        let chunks = Layout::default()
            .direction(Direction::Horizontal)
            .constraints(
                [ Constraint::Length(13)    // 13 chars in "| HH:MM:SS | "
                , Constraint::Min(0)
                , Constraint::Length(9)     // enough for 99999ms
                ].as_ref()
            )
            .split(area);
        let now = Local::now();
        let now_span = Spans::from(vec![
            Span::from("| "),
            Span::styled(format!("{}", now.format("%H:%M:%S")), Style::default().add_modifier(Modifier::ITALIC)),
            Span::from(" | ")
        ]);
        let msg_span = Span::from(state.message.as_str());
        let lat_span = if state.ts_last_update != 0 {
            let delta = now.timestamp_millis() as u64-state.ts_last_update;
            let s = format!("{}ms", delta);
            let style = Style::default().fg(
                if      delta < 5000  { Color::Green  }
                else if delta < 15000 { Color::Yellow }
                else                  { Color::Red    }
            );
            Span::styled(s, style)
        } else {
            Span::styled("- ms", Style::default().fg(Color::Gray))
        };
        f.render_widget(Paragraph::new(now_span), chunks[0]);
        f.render_widget(Paragraph::new(msg_span), chunks[1]);
        f.render_widget(Paragraph::new(lat_span).alignment(Alignment::Right), chunks[2]);
    }
}