Skip to content

Commit

Permalink
added ens searchbar and input field, missing API request
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Jan 9, 2024
1 parent f84476e commit 3dbe598
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 42 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ futures = "0.3.30"
rand = { version = "0.7.3", default-features = false, features = ["std"] }
ratatui = "0.25.0"
serde = "1.0.194"
tokio = "1.35.1"
tokio = { version = "1.35.1", features = ["full"] }
url = "2.5.0"
14 changes: 7 additions & 7 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
enum InputMode {
pub enum InputMode {
Normal,
Editing,
}

// App holds the state of the application
pub struct App {
/// Current value of the input box
input: Input,
/// Current input mode
input_mode: InputMode,
pub input_mode: InputMode,
/// History of recorded messages
messages: Vec<String>,
pub messages: Vec<String>,
// Current input into search bar
pub input: String,
}

impl App {
fn default() -> App {
pub fn default() -> App {
App {
input: Input::default(),
input: "".to_owned(),
input_mode: InputMode::Normal,
messages: Vec::new(),
}
Expand Down
48 changes: 26 additions & 22 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::error::Error;
use std::io::{self, Write};
use std::sync::mpsc;
use std::io::{self};
use std::sync::{mpsc, Arc};
use std::thread;
use std::time::{Duration, Instant};

use app::{App, InputMode};
use crossterm::{
event::{self, Event as CEvent, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode},
Expand All @@ -15,9 +16,11 @@ use ratatui::{
widgets::{Block, BorderType, Borders},
Terminal,
};
use tokio::sync::Mutex;
use widgets::search::render_search_block;
use widgets::welcome::render_welcome;

mod app;
mod models;
mod network;
mod util;
Expand All @@ -31,11 +34,16 @@ use crate::widgets::{
tabs::{render_tab_titles, TabsState},
};

fn main() -> Result<(), Box<dyn Error>> {
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
enable_raw_mode().expect("can run in raw mode");

let (tx, rx) = mpsc::channel();
let tick_rate = Duration::from_millis(200);

let app = Arc::new(Mutex::new(App::default()));
let mut app = app.lock().await;

thread::spawn(move || {
let mut last_tick = Instant::now();
loop {
Expand All @@ -58,15 +66,14 @@ fn main() -> Result<(), Box<dyn Error>> {
});

let stdout = io::stdout();
let mut stdout = stdout.lock();
let stdout = stdout.lock();
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;

let mut table = StatefulTable::new();

let mut token_chart = TokenChart::new();

let mut tabs = TabsState::new(vec!["1Min", "1H", "1D", "1M", "3M", "6M", "1Y"]);
let word = String::new();

terminal.clear()?;

Expand All @@ -87,28 +94,25 @@ fn main() -> Result<(), Box<dyn Error>> {
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage(30),
Constraint::Percentage(10),
Constraint::Percentage(50),
Constraint::Percentage(40),
Constraint::Percentage(30),
]
.as_ref(),
)
.margin(5)
.split(f.size());

// Render welcome
f.render_widget(render_welcome(), chunks[0]);

// Render search bar at the stop
// f.render_widget(render_search_block(word), chunks[1]);

// f.render_stateful_widget(render_table(&mut table), chunks[1], &mut table.state);
if let Some((search_bar, search_bar_rect)) = render_search_block(chunks[0], &mut app) {
// Render search bar at the stop
f.render_widget(search_bar, search_bar_rect);
}

// Render the chart at bottom
f.render_widget(render_chart(&mut token_chart), chunks[2]);
// Render welcome in the middle
f.render_widget(render_welcome(), chunks[1]);

// Render Tab Titles at the top
f.render_widget(render_tab_titles(&mut tabs), chunks[2]);
// Render table at the bottom
f.render_stateful_widget(render_table(&mut table), chunks[2], &mut table.state);
})?;

match rx.recv()? {
Expand All @@ -118,15 +122,15 @@ fn main() -> Result<(), Box<dyn Error>> {
terminal.show_cursor()?;
break;
}
KeyCode::Char('s') => {}
KeyCode::Char('s') => {
app.input_mode = InputMode::Editing;
}
KeyCode::Down => {
table.next();
}
KeyCode::Up => {
table.previous();
}
KeyCode::Right => tabs.next(),
KeyCode::Left => tabs.previous(),
KeyCode::Esc | KeyCode::Enter => {
break;
}
Expand Down
6 changes: 4 additions & 2 deletions src/util/constants.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub const RATE_LIMIT: usize = 60;

pub const GENERAL_HELP_TEXT: [&str] =
pub const GENERAL_HELP_TEXT: &str =
"<esc>: Cancel, q: Quit, ?: Keybindings, s: Focus on the Search bar";

pub const SEARCH_HELP_TEXT: [&str] =
pub const SEARCH_HELP_TEXT: &str =
"Type either an ENS or Ethereum-based address to search for a position";

pub const TICK_RATE: u64 = 200;
2 changes: 1 addition & 1 deletion src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod constants;
pub mod event;
pub mod event_handling;
pub mod list;
30 changes: 21 additions & 9 deletions src/widgets/search.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
use ratatui::{prelude::*, widgets::*};

use super::spinner::Spinner;
use crate::app::App;

pub fn render_search_block<'a>(word: &str) -> Block<'a> {
let searching_block = Block::default()
.title(format!(
"{} Searching for {word}",
Spinner::default().to_string()
))
.border_style(Style::default().fg(Color::Green))
pub fn render_search_block<'a>(
outer: Rect,
app: &'a mut App,
) -> Option<(ratatui::widgets::Paragraph<'a>, Rect)> {
let [searchbar, _, _] = *Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Max(3), Constraint::Min(0), Constraint::Max(1)].as_ref())
.split(outer)
else {
return None;
};

let searchbar_block = Block::default()
.border_style(Style::default())
.title(format!("Search by Address / ENS"))
.borders(Borders::ALL)
.border_type(BorderType::Plain);

searching_block
let input = Paragraph::new(app.input.as_str())
.style(Style::default().fg(Color::White))
.block(searchbar_block);

Some((input, searchbar))
}

0 comments on commit 3dbe598

Please sign in to comment.