Skip to content

Commit

Permalink
fully navigatable
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Jan 14, 2024
1 parent e9da722 commit 40771e9
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 31 deletions.
25 changes: 14 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crossterm::{
event::{self, Event as CEvent, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode},
};
use ethers::core::k256::sha2::digest::Key;
use network::network::{handle_tokio, Network, NetworkEvent};
use ratatui::backend::Backend;
use ratatui::layout::Rect;
Expand Down Expand Up @@ -137,18 +138,13 @@ async fn main() -> Result<(), Box<dyn Error>> {
// with at least a margin of 1
let size = f.size();

let outer_block = Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Thick);
f.render_widget(outer_block, size);

let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage(10),
Constraint::Percentage(50),
Constraint::Percentage(30),
Constraint::Percentage(40),
Constraint::Percentage(40),
Constraint::Percentage(10),
]
.as_ref(),
Expand All @@ -169,14 +165,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
}

// Render welcome in the middle
let (welcome, welcome_block) = render_welcome();
let (welcome, welcome_block) = render_welcome(&mut app);
f.render_widget(welcome_block, chunks[1]);
f.render_widget(welcome, chunks[1]);

// Render table at the bottom
let (table_widget, table_block) = render_table(&table);
f.render_widget(table_block, chunks[2]);
f.render_stateful_widget(table_widget, chunks[2], &mut table.state);
f.render_stateful_widget(render_table(&table, &mut app), chunks[2], &mut table.state);

//Render the help text at the bottom
let help_text = Paragraph::new(GENERAL_HELP_TEXT)
Expand Down Expand Up @@ -207,6 +201,12 @@ async fn main() -> Result<(), Box<dyn Error>> {
KeyCode::Char('h') => {
app.show_help = true;
}
KeyCode::Char('1') => {
app.change_active_block(ActiveBlock::PositionInfo);
}
KeyCode::Char('2') => {
app.change_active_block(ActiveBlock::MyPositions);
}
KeyCode::Esc => {
app.show_help = false;
}
Expand Down Expand Up @@ -251,6 +251,9 @@ async fn main() -> Result<(), Box<dyn Error>> {
KeyCode::Char('h') => {
app.show_help = true;
}
KeyCode::Char('s') => {
app.change_active_block(ActiveBlock::SearchBar);
}
KeyCode::Esc => {
app.show_help = false;
}
Expand Down
45 changes: 27 additions & 18 deletions src/widgets/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use ratatui::{
widgets::{Block, BorderType, Borders, Cell, Row, Table, TableState},
};

use crate::{app::App, routes::ActiveBlock};

pub struct StatefulTable<'a> {
pub state: TableState,
pub items: Vec<Vec<&'a str>>,
Expand All @@ -15,11 +17,9 @@ impl<'a> StatefulTable<'a> {
state: TableState::default(),
// #TODO: Pull token prices and populate here
items: vec![
vec!["ETH", "$2,400", "-0.08%"],
vec!["USDC", "$1.01", "-0.02%"],
vec!["BTC", "$40,000", "+20.54%"],
vec!["UNI", "$21.30", "+10.00%"],
vec!["DAI", "$1.00", "0.00%"],
vec!["USDC/ETH", "$2,400", "✓", "2d"],
vec!["ETH/MATIC", "$100", "X", "10d"],
vec!["USDC/TETH", "$2,000", "✓", "2w"],
],
}
}
Expand Down Expand Up @@ -52,7 +52,7 @@ impl<'a> StatefulTable<'a> {
}
}

pub fn render_table<'a>(table: &StatefulTable<'a>) -> (Table<'a>, Block<'a>) {
pub fn render_table<'a>(table: &StatefulTable<'a>, app: &'a mut App) -> Table<'a> {
// Table Layout
let selected_style = Style::default().add_modifier(Modifier::REVERSED);
let normal_style = Style::default().bg(Color::LightBlue);
Expand All @@ -62,7 +62,7 @@ pub fn render_table<'a>(table: &StatefulTable<'a>) -> (Table<'a>, Block<'a>) {
let header = Row::new(header_cells)
.style(normal_style)
.height(1)
.bottom_margin(1);
.bottom_margin(5);
let rows = table.items.iter().map(|item| {
let height = item
.iter()
Expand All @@ -74,22 +74,31 @@ pub fn render_table<'a>(table: &StatefulTable<'a>) -> (Table<'a>, Block<'a>) {
Row::new(cells).height(height as u16).bottom_margin(1)
});

// Stateful Table for tokens
// Stateful Table for positions
let widths = &[
Constraint::Percentage(50),
Constraint::Length(30),
Constraint::Max(10),
Constraint::Percentage(20),
Constraint::Percentage(20),
Constraint::Percentage(20),
Constraint::Percentage(20),
];

let table = Table::new(rows, widths)
.header(header)
.highlight_style(selected_style)
.highlight_symbol(">> ");

let block = Block::default()
.title("My Positions")
.borders(Borders::ALL)
.border_type(BorderType::Thick);
.highlight_symbol(">> ")
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Thick)
.border_style(Style::default().fg(
if let ActiveBlock::MyPositions = app.get_current_route().get_active_block() {
Color::Green
} else {
Color::White
},
))
.title("My Positions"),
);

(table, block)
table
}
13 changes: 11 additions & 2 deletions src/widgets/welcome.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
use ratatui::{prelude::*, widgets::*};

pub fn render_welcome<'a>() -> (Paragraph<'a>, Block<'a>) {
use crate::{app::App, routes::ActiveBlock};

pub fn render_welcome<'a>(app: &'a mut App) -> (Paragraph<'a>, Block<'a>) {
let outer_block = Block::default()
.title("Position Info")
.borders(Borders::ALL)
.border_type(BorderType::Plain);
.border_type(BorderType::Thick)
.border_style(Style::default().fg(
if let ActiveBlock::PositionInfo = app.get_current_route().get_active_block() {
Color::Green
} else {
Color::White
},
));

let banner = Paragraph::new(Text::from(
cfonts::render(cfonts::Options {
Expand Down

0 comments on commit 40771e9

Please sign in to comment.