tycho-simulation 0.303.2

Provides tools for interacting with protocol states, calculating spot prices, and quoting token swaps.
Documentation
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
use std::{str::FromStr, time::Instant};

use futures::StreamExt;
use itertools::Itertools;
use num_bigint::BigUint;
use num_traits::{CheckedSub, One, Pow};
use ratatui::{
    crossterm::event::{self, Event, KeyCode, KeyEventKind},
    layout::{Constraint, Flex, Layout, Margin, Rect},
    style::{palette::tailwind, Color, Modifier, Style, Stylize},
    text::Text,
    widgets::{
        Block, BorderType, Cell, Clear, HighlightSpacing, Paragraph, Row, Scrollbar,
        ScrollbarOrientation, ScrollbarState, Table, TableState, Wrap,
    },
    DefaultTerminal, Frame,
};
use tokio::{select, sync::mpsc::Receiver};
use tracing::{info, warn};
use tycho_common::{simulation::protocol_sim::ProtocolSim, Bytes};
use tycho_simulation::protocol::models::{ProtocolComponent, Update};

const INFO_TEXT: [&str; 2] = [
    "(Esc) quit | (↑/↓) navigate | (↵) Toggle Quote | (d) Pool Details | (+/-) Adjust Quote | (z) Flip Direction | (=) Enter Amount",
    "(f) Filter by Protocol | (c) Clear Filter | Quote: Press ↵ to show/hide | Use +/- to adjust | = to enter custom amount",
];

const ITEM_HEIGHT: usize = 3;

struct TableColors {
    buffer_bg: Color,
    header_bg: Color,
    header_fg: Color,
    row_fg: Color,
    selected_row_style_fg: Color,
    selected_column_style_fg: Color,
    selected_cell_style_fg: Color,
    normal_row_color: Color,
    alt_row_color: Color,
    footer_border_color: Color,
}

impl TableColors {
    const fn new(color: &tailwind::Palette) -> Self {
        Self {
            buffer_bg: tailwind::SLATE.c950,
            header_bg: color.c900,
            header_fg: tailwind::SLATE.c200,
            row_fg: tailwind::SLATE.c200,
            selected_row_style_fg: color.c400,
            selected_column_style_fg: color.c400,
            selected_cell_style_fg: color.c600,
            normal_row_color: tailwind::SLATE.c950,
            alt_row_color: tailwind::SLATE.c900,
            footer_border_color: color.c400,
        }
    }
}

#[derive(Clone)]
struct Data {
    component: ProtocolComponent,
    state: Box<dyn ProtocolSim>,
    name: String,
    tokens: String,
    price: String,
}

impl Data {
    const fn ref_array(&self) -> [&String; 4] {
        [&self.name, &self.component.protocol_system, &self.tokens, &self.price]
    }
}

pub struct App {
    state: TableState,
    show_popup: bool,
    show_details: bool,
    quote_amount: BigUint,
    zero2one: bool,
    items: Vec<Data>,
    filtered_items: Vec<Data>,
    rx: Receiver<Update>,
    scroll_state: ScrollbarState,
    colors: TableColors,
    input_mode: bool,
    input_buffer: String,
    filter_mode: bool,
    filter_protocol: String,
    available_protocols: Vec<String>,
}

impl App {
    pub fn new(rx: Receiver<Update>) -> Self {
        Self {
            state: TableState::default().with_selected(0),
            show_popup: false,
            show_details: false,
            quote_amount: BigUint::one(),
            zero2one: true,
            rx,
            scroll_state: ScrollbarState::new(0),
            colors: TableColors::new(&tailwind::BLUE),
            items: Vec::new(),
            filtered_items: Vec::new(),
            input_mode: false,
            input_buffer: String::new(),
            filter_mode: false,
            filter_protocol: String::new(),
            available_protocols: Vec::new(),
        }
    }

    fn current_items(&self) -> &Vec<Data> {
        if self.filter_protocol.is_empty() {
            &self.items
        } else {
            &self.filtered_items
        }
    }

    pub fn move_row(&mut self, direction: isize) {
        let current_items_len = self.current_items().len();
        if current_items_len == 0 {
            return;
        }

        // Get current decimals, if any
        let current_decimals = self.state.selected().and_then(|idx| {
            let current_items = self.current_items();
            if idx < current_items.len() {
                let comp = &current_items[idx].component;
                Some(if self.zero2one { comp.tokens[0].decimals } else { comp.tokens[1].decimals })
            } else {
                None
            }
        });

        // Calculate the new index based on direction
        let new_index = match self.state.selected() {
            Some(i) => {
                ((i as isize + direction + current_items_len as isize) % current_items_len as isize)
                    as usize
            }
            None => 0,
        };

        // Update state and scroll position
        self.state.select(Some(new_index));
        self.scroll_state = self
            .scroll_state
            .position(new_index * ITEM_HEIGHT);

        // Adjust quote amount if decimals have changed
        if let Some(prev_decimals) = current_decimals {
            let current_items = self.current_items();
            if new_index < current_items.len() {
                let comp = &current_items[new_index].component;
                let decimals = comp.tokens[if self.zero2one { 0 } else { 1 }].decimals;
                if decimals >= prev_decimals {
                    self.quote_amount *= BigUint::from(10u64).pow(decimals - prev_decimals);
                } else {
                    let new_amount = self.quote_amount.clone() /
                        BigUint::from(10u64).pow(prev_decimals - decimals);
                    self.quote_amount =
                        if new_amount > BigUint::ZERO { new_amount } else { BigUint::one() };
                }
            }
        }
    }

    pub fn update_data(&mut self, update: Update) {
        info!("Got {} new pairs", update.new_pairs.len());
        info!("Total pairs: {}", self.items.len());
        for (id, comp) in update.new_pairs.iter() {
            let name = format!("{comp_id:#042x}", comp_id = comp.id);
            let tokens = comp
                .tokens
                .iter()
                .map(|a| a.symbol.clone())
                .join("/");

            match update.states.get(id) {
                Some(state) => {
                    // Check if spot_price calculation is successful
                    match state.spot_price(&comp.tokens[0], &comp.tokens[1]) {
                        Ok(price) => {
                            self.items.push(Data {
                                component: comp.clone(),
                                state: state.clone(),
                                name,
                                tokens,
                                price: price.to_string(),
                            });
                        }
                        Err(e) => {
                            // Skip pools with spot_price errors
                            warn!(
                                "Skipping pool {comp_id} due to spot_price error: {e}",
                                comp_id = comp.id
                            );
                        }
                    }
                }
                None => {
                    warn!("Received update for unknown pool {comp_id}", comp_id = comp.id)
                }
            };
        }

        for (address, state) in update.states.iter() {
            let eth_address = Bytes::from_str(address).expect("Bad address");
            let entry = self
                .items
                .iter()
                .find_position(|e| e.component.id == eth_address);
            if let Some((index, _)) = entry {
                let row = &self.items[index];
                match state.spot_price(&row.component.tokens[0], &row.component.tokens[1]) {
                    Ok(price) => {
                        // Update the price and state if calculation is successful
                        let row = self.items.get_mut(index).unwrap();
                        row.price = price.to_string();
                        row.state = state.clone();
                    }
                    Err(_) => {
                        // Remove the pool if spot_price calculation fails
                        warn!("Removing pool {} due to spot_price error", address);
                        self.items.remove(index);
                    }
                }
            }
        }

        for comp in update.removed_pairs.values() {
            let entry = self
                .items
                .iter()
                .enumerate()
                .find(|(_, e)| e.component.id == comp.id);
            if let Some((idx, _)) = entry {
                self.items.remove(idx);
            }
        }

        // Update available protocols
        self.update_available_protocols();
        // Update filtered items if filter is active
        self.update_filtered_items();
    }

    fn update_available_protocols(&mut self) {
        let mut protocols: Vec<String> = self
            .items
            .iter()
            .map(|item| item.component.protocol_system.clone())
            .collect();
        protocols.sort_unstable();
        protocols.dedup();
        self.available_protocols = protocols;
    }

    fn update_filtered_items(&mut self) {
        if self.filter_protocol.is_empty() {
            self.filtered_items.clear();
        } else {
            self.filtered_items = self
                .items
                .iter()
                .filter(|item| item.component.protocol_system == self.filter_protocol)
                .cloned()
                .collect();
        }
    }

    pub async fn run(mut self, mut terminal: DefaultTerminal) -> anyhow::Result<()> {
        let mut reader = event::EventStream::new();
        loop {
            terminal.draw(|frame| self.draw(frame))?;
            select! {
                maybe_data = self.rx.recv() => {
                    if let Some(data) = maybe_data {
                        self.update_data(data);
                    }
                },
                maybe_event = reader.next() => {
                    if let Some(Ok(Event::Key(key))) = maybe_event {
                        if key.kind == KeyEventKind::Press {
                            if self.input_mode {
                                match key.code {
                                    KeyCode::Char(c) if c.is_ascii_digit() => {
                                        self.input_buffer.push(c);
                                    },
                                    KeyCode::Backspace => {
                                        self.input_buffer.pop();
                                    },
                                    KeyCode::Enter => {
                                        if let Ok(amount) = BigUint::from_str(&self.input_buffer) {
                                            self.quote_amount = amount;
                                        }
                                        self.input_mode = false;
                                        self.input_buffer.clear();
                                    },
                                    KeyCode::Esc => {
                                        self.input_mode = false;
                                        self.input_buffer.clear();
                                    },
                                    _ => {}
                                }
                            } else {
                                match key.code {
                                    KeyCode::Char('q') | KeyCode::Esc => {
                                        if !self.show_popup && !self.show_details && !self.filter_mode {
                                            return Ok(())
                                        } else {
                                            self.show_popup = false;
                                            self.show_details = false;
                                            self.filter_mode = false;
                                        }
                                    },
                                    KeyCode::Char('j') | KeyCode::Down => self.move_row(1),
                                    KeyCode::Char('+') => {
                                        self.modify_quote(true)
                                    },
                                    KeyCode::Char('-') => {
                                        self.modify_quote(false)
                                    },
                                    KeyCode::Char('z') => {
                                        self.zero2one = !self.zero2one;
                                        self.quote_amount = BigUint::one();
                                    }
                                    KeyCode::Char('k') | KeyCode::Up => self.move_row(-1),
                                    KeyCode::Char('=') => {
                                        self.input_mode = true;
                                        self.input_buffer.clear();
                                    },
                                    KeyCode::Char('d') => self.show_details = !self.show_details,
                                    KeyCode::Char('f') => self.filter_mode = !self.filter_mode,
                                    KeyCode::Char('c') => {
                                        self.filter_protocol.clear();
                                        self.update_filtered_items();
                                        self.state.select(Some(0));
                                    },
                                    KeyCode::Enter => {
                                        if self.filter_mode {
                                            self.select_filter();
                                        } else {
                                            self.show_popup = !self.show_popup;
                                        }
                                    },
                                    _ => {}
                                }
                            }
                        }
                    }
                }
            };
        }
    }

    fn select_filter(&mut self) {
        if !self.available_protocols.is_empty() {
            let selected_idx = self.state.selected().unwrap_or(0) % self.available_protocols.len();
            self.filter_protocol = self.available_protocols[selected_idx].clone();
            self.update_filtered_items();
            self.filter_mode = false;
            self.state.select(Some(0));
        }
    }

    fn modify_quote(&mut self, increase: bool) {
        if !self.show_popup {
            return;
        }

        if let Some(idx) = self.state.selected() {
            let current_items = self.current_items();
            if idx < current_items.len() {
                let comp = &current_items[idx].component;
                let decimals =
                    if self.zero2one { comp.tokens[0].decimals } else { comp.tokens[1].decimals };
                if increase {
                    self.quote_amount += BigUint::from(10u64).pow(decimals);
                } else {
                    self.quote_amount = self
                        .quote_amount
                        .checked_sub(&BigUint::from(10u64).pow(decimals))
                        .unwrap_or(BigUint::one());
                }
            }
        }
    }

    fn draw(&mut self, frame: &mut Frame) {
        let vertical = &Layout::vertical([Constraint::Min(5), Constraint::Length(4)]);
        let rects = vertical.split(frame.area());

        self.render_table(frame, rects[0]);
        self.render_scrollbar(frame, rects[0]);
        self.render_footer(frame, rects[1]);
        if self.items.is_empty() {
            self.render_loading(frame);
        }
        if self.show_popup {
            self.render_quote_popup(frame);
        }
        if self.show_details {
            self.render_details_popup(frame);
        }
        if self.filter_mode {
            self.render_filter_popup(frame);
        }
        if self.input_mode {
            self.render_input_popup(frame);
        }
    }

    fn render_table(&mut self, frame: &mut Frame, area: Rect) {
        let header_style = Style::default()
            .fg(self.colors.header_fg)
            .bg(self.colors.header_bg);
        let selected_row_style = Style::default()
            .add_modifier(Modifier::REVERSED)
            .fg(self.colors.selected_row_style_fg);
        let selected_col_style = Style::default().fg(self.colors.selected_column_style_fg);
        let selected_cell_style = Style::default()
            .add_modifier(Modifier::REVERSED)
            .fg(self.colors.selected_cell_style_fg);

        let header = ["Pool", "Protocol", "Tokens", "Price"]
            .into_iter()
            .map(Cell::from)
            .collect::<Row>()
            .style(header_style)
            .height(1);
        let current_items = self.current_items();
        let rows = current_items
            .iter()
            .enumerate()
            .map(|(i, data)| {
                let color = match i % 2 {
                    0 => self.colors.normal_row_color,
                    _ => self.colors.alt_row_color,
                };
                let item = data.ref_array();
                item.into_iter()
                    .map(|content| Cell::from(Text::from(format!("\n{content}\n"))))
                    .collect::<Row>()
                    .style(
                        Style::new()
                            .fg(self.colors.row_fg)
                            .bg(color),
                    )
                    .height(ITEM_HEIGHT as u16)
            });
        let bar = "";
        let t = Table::new(
            rows,
            [
                // + 1 is for padding.
                Constraint::Length(70),
                Constraint::Min(1),
                Constraint::Min(1),
                Constraint::Min(1),
            ],
        )
        .header(header)
        .row_highlight_style(selected_row_style)
        .column_highlight_style(selected_col_style)
        .cell_highlight_style(selected_cell_style)
        .highlight_symbol(Text::from(vec!["".into(), bar.into(), bar.into(), "".into()]))
        .bg(self.colors.buffer_bg)
        .highlight_spacing(HighlightSpacing::Always);
        frame.render_stateful_widget(t, area, &mut self.state);
    }

    fn render_scrollbar(&mut self, frame: &mut Frame, area: Rect) {
        frame.render_stateful_widget(
            Scrollbar::default()
                .orientation(ScrollbarOrientation::VerticalRight)
                .begin_symbol(None)
                .end_symbol(None),
            area.inner(Margin { vertical: 1, horizontal: 1 }),
            &mut self.scroll_state,
        );
    }

    fn render_footer(&self, frame: &mut Frame, area: Rect) {
        let info_footer = Paragraph::new(Text::from_iter(INFO_TEXT))
            .style(
                Style::new()
                    .fg(self.colors.row_fg)
                    .bg(self.colors.buffer_bg),
            )
            .centered()
            .block(
                Block::bordered()
                    .border_type(BorderType::Double)
                    .border_style(Style::new().fg(self.colors.footer_border_color)),
            );
        frame.render_widget(info_footer, area);

        // Render pool count on the bottom right
        let pool_count_text = format!(" Pools: {} ", self.items.len());
        let pool_count = Paragraph::new(pool_count_text)
            .style(
                Style::new()
                    .fg(self.colors.header_fg)
                    .bg(self.colors.buffer_bg),
            )
            .right_aligned();

        // Position in the bottom right corner, inside the border
        let count_area = Rect {
            x: area.x + area.width.saturating_sub(20),
            y: area.y + area.height.saturating_sub(1),
            width: 20.min(area.width),
            height: 1,
        };
        frame.render_widget(pool_count, count_area);
    }

    fn render_loading(&self, frame: &mut Frame) {
        let area = frame.area();

        let block = Block::bordered();
        let popup = Paragraph::new(Text::from("\nLOADING...\n"))
            .centered()
            .block(block);
        let area = popup_area(area, Constraint::Percentage(50), Constraint::Length(5));
        frame.render_widget(Clear, area);
        frame.render_widget(popup, area);
    }

    fn render_quote_popup(&self, frame: &mut Frame) {
        let area = frame.area();

        if let Some(idx) = self.state.selected() {
            if self.quote_amount > BigUint::ZERO {
                let current_items = self.current_items();
                if idx < current_items.len() {
                    let comp = &current_items[idx].component;
                    let state = &current_items[idx].state;
                    let (token_in, token_out) = if self.zero2one {
                        (&comp.tokens[0], &comp.tokens[1])
                    } else {
                        (&comp.tokens[1], &comp.tokens[0])
                    };

                    let start = Instant::now();
                    let res = state.get_amount_out(self.quote_amount.clone(), token_in, token_out);
                    let duration = start.elapsed();

                    let text = res
                    .map(|data| {
                        format!(
                            "Swap Direction: {token_in_symbol}{token_out_symbol}\nQuote amount: {quote_amount}\nReceived amount: {amount}\nGas: {gas}\nDuration: {duration:?}",
                            token_in_symbol = token_in.symbol,
                            token_out_symbol = token_out.symbol,
                            quote_amount = self.quote_amount,
                            amount = data.amount,
                            gas = data.gas
                        )
                    })
                    .unwrap_or_else(|err| format!("{err:?}"));

                    let block = Block::bordered().title("Quote:");
                    let popup = Paragraph::new(Text::from(text))
                        .block(block)
                        .wrap(Wrap { trim: false });
                    let area =
                        popup_area(area, Constraint::Percentage(50), Constraint::Percentage(50));
                    frame.render_widget(Clear, area);
                    frame.render_widget(popup, area);
                }
            }
        }
    }

    fn render_input_popup(&self, frame: &mut Frame) {
        let area = frame.area();

        let text = format!(
            "Enter quote amount: {}\n\nPress Enter to confirm or Esc to cancel",
            self.input_buffer
        );
        let block = Block::bordered()
            .title("Quote Amount Input")
            .border_style(Style::new().fg(self.colors.footer_border_color));
        let input = Paragraph::new(Text::from(text))
            .block(block)
            .wrap(Wrap { trim: false })
            .style(
                Style::new()
                    .fg(self.colors.row_fg)
                    .bg(self.colors.buffer_bg),
            );
        let area = popup_area(area, Constraint::Percentage(50), Constraint::Length(8));
        frame.render_widget(Clear, area);
        frame.render_widget(input, area);
    }

    fn render_details_popup(&self, frame: &mut Frame) {
        let area = frame.area();

        if let Some(idx) = self.state.selected() {
            let current_items = self.current_items();
            if idx < current_items.len() {
                let comp = &current_items[idx].component;
                let mut text = format!(
                    "Pool ID: {:#042x}\nProtocol System: {}\nProtocol Type: {}\nChain: {:?}\nCreated At: {}\nCreation Tx: {:#042x}\n\nTokens:\n",
                    comp.id,
                    comp.protocol_system,
                    comp.protocol_type_name,
                    comp.chain,
                    comp.created_at.format("%Y-%m-%d %H:%M:%S"),
                    comp.creation_tx
                );

                for (i, token) in comp.tokens.iter().enumerate() {
                    text.push_str(&format!(
                        "  {}: {} ({}), decimals: {}\n",
                        i, token.symbol, token.address, token.decimals
                    ));
                }

                text.push_str("\nContract IDs:\n");
                for (i, contract_id) in comp.contract_ids.iter().enumerate() {
                    text.push_str(&format!("  {}: {:#042x}\n", i, contract_id));
                }

                if !comp.static_attributes.is_empty() {
                    text.push_str("\nStatic Attributes:\n");
                    for (key, value) in &comp.static_attributes {
                        text.push_str(&format!("  {}: {:#x}\n", key, value));
                    }
                }

                let block = Block::bordered()
                    .title("Pool Details")
                    .border_style(Style::new().fg(self.colors.footer_border_color));
                let popup = Paragraph::new(Text::from(text))
                    .block(block)
                    .wrap(Wrap { trim: false })
                    .style(
                        Style::new()
                            .fg(self.colors.row_fg)
                            .bg(self.colors.buffer_bg),
                    );
                let area = popup_area(area, Constraint::Percentage(80), Constraint::Percentage(80));
                frame.render_widget(Clear, area);
                frame.render_widget(popup, area);
            }
        }
    }

    fn render_filter_popup(&self, frame: &mut Frame) {
        let area = frame.area();

        let mut text = String::from("Select Protocol to Filter:\n\n");
        if self.available_protocols.is_empty() {
            text.push_str("No protocols available");
        } else {
            for (i, protocol) in self
                .available_protocols
                .iter()
                .enumerate()
            {
                let marker = if Some(i) == self.state.selected() { "" } else { "  " };
                text.push_str(&format!("{}{}\n", marker, protocol));
            }
            text.push_str("\nPress Enter to select, Esc to cancel");
        }

        let block = Block::bordered()
            .title("Protocol Filter")
            .border_style(Style::new().fg(self.colors.footer_border_color));
        let popup = Paragraph::new(Text::from(text))
            .block(block)
            .wrap(Wrap { trim: false })
            .style(
                Style::new()
                    .fg(self.colors.row_fg)
                    .bg(self.colors.buffer_bg),
            );
        let area = popup_area(area, Constraint::Percentage(60), Constraint::Percentage(60));
        frame.render_widget(Clear, area);
        frame.render_widget(popup, area);
    }
}

/// helper function to create a centered rect using up certain percentage of the available rect `r`
fn popup_area(area: Rect, x: Constraint, y: Constraint) -> Rect {
    let vertical = Layout::vertical([y]).flex(Flex::Center);
    let horizontal = Layout::horizontal([x]).flex(Flex::Center);
    let [area] = vertical.areas(area);
    let [area] = horizontal.areas(area);
    area
}