Skip to content

Commit

Permalink
default to my positions on start
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Jan 10, 2024
1 parent 788c79b commit 58fc897
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 33 deletions.
54 changes: 46 additions & 8 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use ethers::types::NameOrAddress;
use std::sync::mpsc::Sender;

use crate::{
models::states::InputMode,
network::network::NetworkEvent,
routes::{ActiveBlock, Route},
};

Expand All @@ -10,24 +14,30 @@ pub struct App {
/// History of recorded messages
pub messages: Vec<String>,
// Current input into search bar
pub input: String,
pub search_input: String,
/// whether to show help dialogue
pub show_help: bool,
/// Position of the cursor
pub cursor_position: usize,
/// Current route
pub routes: Vec<Route>,
/// Whether the app is loading
pub is_loading: bool,
/// The channel to send network events to
pub network_txn: Option<Sender<NetworkEvent>>,
}

impl App {
pub fn default() -> App {
App {
cursor_position: 0,
input: "".to_owned(),
search_input: "".to_owned(),
input_mode: InputMode::Normal,
messages: Vec::new(),
routes: vec![Route::default()],
show_help: false,
is_loading: false,
network_txn: None,
}
}

Expand Down Expand Up @@ -65,13 +75,13 @@ impl App {
}

pub fn enter_char(&mut self, new_char: char) {
self.input.insert(self.cursor_position, new_char);
self.search_input.insert(self.cursor_position, new_char);

self.move_cursor_right();
}

pub fn paste(&mut self, data: String) {
self.input = format!("{}{}", self.input, data);
self.search_input = format!("{}{}", self.search_input, data);
for _ in 0..data.len() {
self.move_cursor_right();
}
Expand All @@ -88,22 +98,50 @@ impl App {
let from_left_to_current_index = current_index - 1;

// Getting all characters before the selected character.
let before_char_to_delete = self.input.chars().take(from_left_to_current_index);
let before_char_to_delete = self.search_input.chars().take(from_left_to_current_index);
// Getting all characters after selected character.
let after_char_to_delete = self.input.chars().skip(current_index);
let after_char_to_delete = self.search_input.chars().skip(current_index);

// Put all characters together except the selected one.
// By leaving the selected one out, it is forgotten and therefore deleted.
self.input = before_char_to_delete.chain(after_char_to_delete).collect();
self.search_input = before_char_to_delete.chain(after_char_to_delete).collect();
self.move_cursor_left();
}
}

pub fn clamp_cursor(&self, new_cursor_pos: usize) -> usize {
new_cursor_pos.clamp(0, self.input.len())
new_cursor_pos.clamp(0, self.search_input.len())
}

pub fn reset_cursor(&mut self) {
self.cursor_position = 0;
}

// Send a network event to the network thread
pub fn dispatch(&mut self, action: NetworkEvent) {
// `is_loading` will be set to false again after the async action has finished in network.rs
self.is_loading = true;
if let Some(network_txn) = &self.network_txn {
if let Err(e) = network_txn.send(action) {
self.is_loading = false;
println!("Error from dispatch {}", e);
//#TODO: handle network error
};
}
}

pub fn submit_search(&mut self) -> String {
if let Ok(name_or_address) = self.search_input.parse::<NameOrAddress>() {
self.dispatch(NetworkEvent::GetENSAddressInfo {
name_or_address,
is_searching: true,
})
}

let message = self.search_input.to_owned();

self.search_input.clear();
self.reset_cursor();
message
}
}
76 changes: 56 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use ratatui::{
widgets::{Block, BorderType, Borders},
Terminal,
};
use routes::{ActiveBlock, Route, RouteId};
use tokio::sync::Mutex;
use widgets::search::render_search_block;
use widgets::welcome::render_welcome;
Expand Down Expand Up @@ -117,32 +118,67 @@ async fn main() -> Result<(), Box<dyn Error>> {
})?;

// #TODO: Move this to event handling

match rx.recv()? {
Event::Input(event) => match event.code {
KeyCode::Char('q') => {
disable_raw_mode()?;
terminal.show_cursor()?;
break;
}
KeyCode::Char('s') => {
app.input_mode = InputMode::Editing;
}
KeyCode::Down => {
table.next();
}
KeyCode::Up => {
table.previous();
Event::Input(event) => {
if let ActiveBlock::SearchBar = app.get_current_route().get_active_block() {
match app.input_mode {
InputMode::Normal => match event.code {
KeyCode::Char('e') => {
app.input_mode = InputMode::Editing;
}
_ => {}
},
InputMode::Editing => match event.code {
KeyCode::Esc => {
app.input_mode = InputMode::Normal;
}
KeyCode::Char(c) => {
app.enter_char(c);
}
KeyCode::Left => {
app.move_cursor_left();
}
KeyCode::Right => {
app.move_cursor_right();
}
KeyCode::Backspace => {
app.delete_char();
}
KeyCode::Enter => {
app.input_mode = InputMode::Normal;

let message = app.submit_search();
app.set_route(Route::new(
RouteId::Searching(message),
ActiveBlock::MyPosition,
));
}
_ => {}
},
}
} else {
match event.code {
KeyCode::Char('q') => {
disable_raw_mode()?;
terminal.clear()?;
terminal.show_cursor()?;
break;
}
KeyCode::Char('h') => {
app.show_help = true;
}
KeyCode::Esc => {
app.show_help = false;
}
_ => {}
}
}
KeyCode::Esc | KeyCode::Enter => {
break;
}
_ => {}
},
}
Event::Tick => {
token_chart.update();
}
}
}

Ok(())
}
4 changes: 2 additions & 2 deletions src/network/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ pub struct Network<'a> {
etherscan: &'a Option<Etherscan>,
}

pub enum IoEvent {
GetNameOrAddressInfo {
pub enum NetworkEvent {
GetENSAddressInfo {
name_or_address: NameOrAddress,
is_searching: bool,
},
Expand Down
4 changes: 2 additions & 2 deletions src/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub enum RouteId {
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum ActiveBlock {
SearchBar,
Main,
MyPosition,
}

#[derive(Clone)]
Expand All @@ -34,7 +34,7 @@ impl Default for Route {
fn default() -> Self {
Self {
id: RouteId::Welcome,
active_block: ActiveBlock::SearchBar,
active_block: ActiveBlock::MyPosition,
}
}
}
2 changes: 1 addition & 1 deletion src/widgets/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn render_search_block<'a>(
.borders(Borders::ALL)
.border_type(BorderType::Plain);

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

Expand Down

0 comments on commit 58fc897

Please sign in to comment.