tickrs 0.15.0

Realtime ticker data in your terminal 📈
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use ratatui::backend::Backend;
use ratatui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Borders, Clear, Paragraph, Tabs, Wrap};
use ratatui::{Frame, Terminal};

use crate::app::{App, Mode, ScrollDirection};
use crate::common::{ChartType, TimeFrame};
use crate::service::Service;
use crate::theme::style;
use crate::widget::{
    block, AddStockWidget, ChartConfigurationWidget, OptionsWidget, StockSummaryWidget,
    StockWidget, HELP_HEIGHT, HELP_WIDTH,
};
use crate::{SHOW_VOLUMES, THEME};

pub fn draw(terminal: &mut Terminal<impl Backend>, app: &mut App) {
    let current_size = terminal.size().unwrap_or_default();

    if current_size.width <= 10 || current_size.height <= 10 {
        return;
    }

    if app.debug.enabled {
        app.debug.dimensions = (current_size.width, current_size.height);
    }

    terminal
        .draw(|frame| {
            // Set background color
            frame.render_widget(Block::default().style(style()), frame.size());

            if app.debug.enabled && app.mode == Mode::AddStock {
                // layout[0] - Main window
                // layout[1] - Add Stock window
                // layout[2] - Debug window
                let layout = Layout::default()
                    .constraints([
                        Constraint::Min(0),
                        Constraint::Length(3),
                        Constraint::Length(5),
                    ])
                    .split(frame.size());

                if !app.stocks.is_empty() {
                    match app.previous_mode {
                        Mode::DisplaySummary => draw_summary(frame, app, layout[0]),
                        _ => draw_main(frame, app, layout[0]),
                    }
                }

                draw_add_stock(frame, app, layout[1]);
                draw_debug(frame, app, layout[2]);
            } else if app.debug.enabled {
                // layout[0] - Main window
                // layout[1] - Debug window
                let layout = Layout::default()
                    .constraints([Constraint::Min(0), Constraint::Length(5)])
                    .split(frame.size());

                match app.mode {
                    Mode::DisplaySummary => draw_summary(frame, app, layout[0]),
                    Mode::Help => draw_help(frame, app, layout[0]),
                    _ => draw_main(frame, app, layout[0]),
                }

                draw_debug(frame, app, layout[1]);
            } else if app.mode == Mode::AddStock {
                // layout[0] - Main window
                // layout[1] - Add Stock window
                let layout = Layout::default()
                    .constraints([Constraint::Min(0), Constraint::Length(3)])
                    .split(frame.size());

                if !app.stocks.is_empty() {
                    match app.previous_mode {
                        Mode::DisplaySummary => draw_summary(frame, app, layout[0]),
                        _ => draw_main(frame, app, layout[0]),
                    }
                }

                draw_add_stock(frame, app, layout[1]);
            } else {
                // layout - Main window
                let layout = frame.size();

                match app.mode {
                    Mode::DisplaySummary => draw_summary(frame, app, layout),
                    Mode::Help => draw_help(frame, app, layout),
                    _ => draw_main(frame, app, layout),
                }
            };
        })
        .unwrap();
}

fn draw_main(frame: &mut Frame, app: &mut App, area: Rect) {
    // layout[0] - Header
    // layout[1] - Main widget
    let mut layout = Layout::default()
        .constraints([Constraint::Length(3), Constraint::Min(0)])
        .split(area)
        .to_vec();

    if !app.stocks.is_empty() {
        frame.render_widget(crate::widget::block::new(" Tabs "), layout[0]);
        let padded = add_padding(layout[0], 1, PaddingDirection::All);
        layout[0] = padded;

        // header[0] - Stock symbol tabs
        // header[1] - (Optional) help icon
        let header = if app.hide_help {
            vec![layout[0]]
        } else {
            let split = Layout::default()
                .direction(Direction::Horizontal)
                .constraints([Constraint::Min(0), Constraint::Length(10)])
                .split(layout[0]);
            split.to_vec()
        };

        // Draw tabs
        {
            let tabs: Vec<_> = app.stocks.iter().map(|w| Line::from(w.symbol())).collect();

            frame.render_widget(
                Tabs::new(tabs)
                    .select(app.current_tab)
                    .style(style().fg(THEME.text_secondary()))
                    .highlight_style(style().fg(THEME.text_primary())),
                header[0],
            );
        }

        // Draw help icon
        if !app.hide_help {
            frame.render_widget(
                Paragraph::new(Line::from(Span::styled("Help '?'", style())))
                    .style(style().fg(THEME.text_normal()))
                    .alignment(Alignment::Center),
                header[1],
            );
        }
    }

    // Make sure only displayed stock has network activity
    app.stocks.iter().enumerate().for_each(|(idx, s)| {
        if idx == app.current_tab {
            s.stock_service.resume();
        } else {
            s.stock_service.pause();
        }
    });

    // Draw main widget
    if let Some(stock) = app.stocks.get_mut(app.current_tab) {
        // main_chunks[0] - Stock widget
        // main_chunks[1] - Options widget / Configuration widget (optional)
        let mut main_chunks =
            if app.mode == Mode::DisplayOptions || app.mode == Mode::ConfigureChart {
                Layout::default()
                    .direction(Direction::Horizontal)
                    .constraints([Constraint::Min(0), Constraint::Length(44)])
                    .split(layout[1])
                    .to_vec()
            } else {
                vec![layout[1]]
            };

        match app.mode {
            Mode::DisplayStock | Mode::AddStock => {
                frame.render_stateful_widget(StockWidget {}, main_chunks[0], stock);
            }
            // If width is too small, don't render stock widget and use entire space
            // for options / configure widget
            Mode::DisplayOptions | Mode::ConfigureChart => {
                if main_chunks[0].width >= 19 {
                    frame.render_stateful_widget(StockWidget {}, main_chunks[0], stock);
                } else {
                    main_chunks[1] = layout[1];
                }
            }
            _ => {}
        }

        match app.mode {
            Mode::DisplayOptions => {
                if let Some(options) = stock.options.as_mut() {
                    if main_chunks[1].width >= 44 && main_chunks[1].height >= 14 {
                        frame.render_stateful_widget(OptionsWidget {}, main_chunks[1], options);
                    } else {
                        let mut padded = main_chunks[1];
                        padded = add_padding(padded, 1, PaddingDirection::Left);
                        padded = add_padding(padded, 1, PaddingDirection::Top);
                        main_chunks[1] = padded;

                        frame.render_widget(
                            Paragraph::new(Line::from(Span::styled(
                                "Increase screen size to display options",
                                style(),
                            ))),
                            main_chunks[1],
                        );
                    }
                }
            }
            Mode::ConfigureChart => {
                if main_chunks[1].width >= 44 && main_chunks[1].height >= 14 {
                    let state = &mut stock.chart_configuration;

                    let chart_type = stock.chart_type;

                    frame.render_stateful_widget(
                        ChartConfigurationWidget { chart_type },
                        main_chunks[1],
                        state,
                    );
                } else {
                    let mut padded = main_chunks[1];
                    padded = add_padding(padded, 1, PaddingDirection::Left);
                    padded = add_padding(padded, 1, PaddingDirection::Top);
                    main_chunks[1] = padded;

                    frame.render_widget(
                        Paragraph::new(Line::from(Span::styled(
                            "Increase screen size to display configuration screen",
                            style(),
                        )))
                        .wrap(Wrap { trim: false }),
                        main_chunks[1],
                    );
                }
            }
            _ => {}
        }
    }
}

fn draw_add_stock(frame: &mut Frame, app: &mut App, area: Rect) {
    frame.render_stateful_widget(AddStockWidget {}, area, &mut app.add_stock);
}

fn draw_summary(frame: &mut Frame, app: &mut App, mut area: Rect) {
    let border = block::new(" Summary ");
    frame.render_widget(border, area);
    area = add_padding(area, 1, PaddingDirection::All);
    area = add_padding(area, 1, PaddingDirection::Right);

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

    let height = area.height;
    let num_to_render = (((height - 3) / stock_widget_height) as usize).min(app.stocks.len());

    // If the user queued an up / down scroll, calculate the new offset, store it in
    // state and use it for this render. Otherwise use stored offset from state.
    let mut scroll_offset = if let Some(direction) = app.summary_scroll_state.queued_scroll.take() {
        let new_offset = match direction {
            ScrollDirection::Up => {
                if app.summary_scroll_state.offset == 0 {
                    0
                } else {
                    (app.summary_scroll_state.offset - 1).min(app.stocks.len())
                }
            }
            ScrollDirection::Down => {
                (app.summary_scroll_state.offset + 1).min(app.stocks.len() - num_to_render)
            }
        };

        app.summary_scroll_state.offset = new_offset;

        new_offset
    } else {
        app.summary_scroll_state.offset
    };

    // If we resize the app up, adj the offset
    if num_to_render + scroll_offset > app.stocks.len() {
        scroll_offset -= (num_to_render + scroll_offset) - app.stocks.len();
        app.summary_scroll_state.offset = scroll_offset;
    }

    // layouy[0] - Header
    // layouy[1] - Summary window
    // layouy[2] - Empty
    let mut layout = Layout::default()
        .constraints([
            Constraint::Length(1),
            Constraint::Length((num_to_render * stock_widget_height as usize) as u16),
            Constraint::Min(0),
        ])
        .split(area)
        .to_vec();

    // header[0]
    // header[1] - (Optional) help icon
    let header = if app.hide_help {
        Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Min(0)])
            .split(layout[0])
            .to_vec()
    } else {
        Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Min(0), Constraint::Length(8)])
            .split(layout[0])
            .to_vec()
    };

    // Draw help icon
    if !app.hide_help {
        frame.render_widget(
            Paragraph::new(Line::from(Span::styled("Help '?'", style())))
                .style(style().fg(THEME.text_normal()))
                .alignment(Alignment::Center),
            header[1],
        );
    }

    let contraints = app.stocks[scroll_offset..num_to_render + scroll_offset]
        .iter()
        .map(|_| Constraint::Length(stock_widget_height))
        .collect::<Vec<_>>();

    let stock_layout = Layout::default()
        .constraints(contraints)
        .split(layout[1])
        .to_vec();

    // Make sure only displayed stocks have network activity
    app.stocks.iter().enumerate().for_each(|(idx, s)| {
        if idx >= scroll_offset && idx < num_to_render + scroll_offset {
            s.stock_service.resume();
        } else {
            s.stock_service.pause();
        }
    });

    for (idx, stock) in app.stocks[scroll_offset..num_to_render + scroll_offset]
        .iter_mut()
        .enumerate()
    {
        frame.render_stateful_widget(StockSummaryWidget {}, stock_layout[idx], stock);
    }

    // Draw time frame & paging
    {
        let mut current = layout[2];
        current = add_padding(current, 1, PaddingDirection::Left);
        frame.render_widget(Clear, current);
        frame.render_widget(Block::default().style(style()), current);

        let offset = current.height - 2;
        current = add_padding(current, offset, PaddingDirection::Top);

        frame.render_widget(
            Block::default()
                .borders(Borders::TOP)
                .border_style(style().fg(THEME.border_secondary())),
            current,
        );

        current = add_padding(current, 1, PaddingDirection::Top);
        layout[2] = current;

        let time_frames = TimeFrame::tab_names()
            .iter()
            .map(|s| Line::from(*s))
            .collect::<Vec<_>>();

        // botton_layout[0] - time frame
        // botton_layout[1] - paging indicator
        let bottom_layout = Layout::default()
            .direction(Direction::Horizontal)
            .constraints([Constraint::Min(0), Constraint::Length(3)])
            .split(layout[2])
            .to_vec();

        let tabs = Tabs::new(time_frames)
            .select(app.summary_time_frame.idx())
            .style(style().fg(THEME.text_secondary()))
            .highlight_style(style().fg(THEME.text_primary()));

        frame.render_widget(tabs, bottom_layout[0]);

        let more_up = scroll_offset > 0;
        let more_down = scroll_offset + num_to_render < app.stocks.len();

        let up_arrow = Span::styled(
            "",
            style().fg(if more_up {
                THEME.text_normal()
            } else {
                THEME.gray()
            }),
        );
        let down_arrow = Span::styled(
            "",
            style().fg(if more_down {
                THEME.text_normal()
            } else {
                THEME.gray()
            }),
        );

        frame.render_widget(
            Paragraph::new(Line::from(vec![up_arrow, Span::raw(" "), down_arrow])),
            bottom_layout[1],
        );
    }
}

fn draw_help(frame: &mut Frame, app: &App, area: Rect) {
    let mut layout = area;

    if layout.width < HELP_WIDTH as u16 || layout.height < HELP_HEIGHT as u16 {
        frame.render_widget(
            Paragraph::new(Line::from(Span::styled(
                "Increase screen size to display help",
                style(),
            ))),
            layout,
        );
    } else {
        layout = app.help.get_rect(layout);

        frame.render_widget(app.help, layout);
    }
}

fn draw_debug(frame: &mut Frame, app: &mut App, area: Rect) {
    app.debug.mode = app.mode;

    let debug_text = Line::from(Span::styled(format!("{:?}", app.debug), style()));
    let debug_paragraph = Paragraph::new(debug_text).wrap(Wrap { trim: true });

    frame.render_widget(debug_paragraph, area);
}

pub fn add_padding(mut rect: Rect, n: u16, direction: PaddingDirection) -> Rect {
    match direction {
        PaddingDirection::Top => {
            rect.y += n;
            rect.height = rect.height.saturating_sub(n);
            rect
        }
        PaddingDirection::Bottom => {
            rect.height = rect.height.saturating_sub(n);
            rect
        }
        PaddingDirection::Left => {
            rect.x += n;
            rect.width = rect.width.saturating_sub(n);
            rect
        }
        PaddingDirection::Right => {
            rect.width = rect.width.saturating_sub(n);
            rect
        }
        PaddingDirection::All => {
            rect.y += n;
            rect.height = rect.height.saturating_sub(n * 2);

            rect.x += n;
            rect.width = rect.width.saturating_sub(n * 2);

            rect
        }
    }
}

#[allow(dead_code)]
pub enum PaddingDirection {
    Top,
    Bottom,
    Left,
    Right,
    All,
}