Skip to content

Commit

Permalink
Loop visually
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 committed Jun 22, 2023
1 parent f18cff9 commit c9f8f63
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 27 deletions.
6 changes: 4 additions & 2 deletions drink-cli/src/app_state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::VecDeque, env, path::PathBuf};
use std::{env, path::PathBuf};

use contract_transcode::ContractMessageTranscoder;
use drink::Sandbox;
Expand All @@ -25,6 +25,7 @@ pub struct UiState {
pub mode: Mode,

pub user_input: String,
pub current_contract: usize,

pub show_help: bool,
pub output: Vec<Line<'static>>,
Expand All @@ -39,6 +40,7 @@ impl Default for UiState {
contract_project_name: "".to_string(),
mode: Default::default(),
user_input: Default::default(),
current_contract: 0,
show_help: false,
output: Default::default(),
output_offset: 0,
Expand All @@ -59,5 +61,5 @@ pub struct AppState {
pub sandbox: Sandbox,
pub chain_info: ChainInfo,
pub ui_state: UiState,
pub contracts: VecDeque<Contract>,
pub contracts: Vec<Contract>,
}
5 changes: 3 additions & 2 deletions drink-cli/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn deploy_contract(app_state: &mut AppState, constructor: String, salt: Vec<u8>)

let contract_name = app_state.ui_state.contract_project_name.clone();

app_state.contracts.push_front(Contract {
app_state.contracts.push(Contract {
name: contract_name.clone(),
address: account_id,
base_path: app_state.ui_state.pwd.clone(),
Expand All @@ -131,10 +131,11 @@ fn deploy_contract(app_state: &mut AppState, constructor: String, salt: Vec<u8>)
)
.expect("Failed to load contract transcoder"),
});
app_state.ui_state.current_contract = app_state.contracts.len() - 1;
}

fn call_contract(app_state: &mut AppState, message: String) {
let contract = match app_state.contracts.get(0) {
let contract = match app_state.contracts.get(app_state.ui_state.current_contract) {
Some(c) => c,
None => {
app_state.print_error("No deployed contract");
Expand Down
23 changes: 6 additions & 17 deletions drink-cli/src/ui/contracts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ratatui::{
style::{Modifier, Style},
style::{Color, Style},
text::{Line, Span},
widgets::{Block, BorderType, Borders, List, ListItem, Padding, Widget},
};
Expand All @@ -18,25 +18,14 @@ pub(super) fn build(app_state: &mut AppState) -> impl Widget {
.iter()
.enumerate()
.map(|(idx, contract)| {
let style = match idx {
0 => Style::default()
.add_modifier(Modifier::BOLD)
.add_modifier(Modifier::ITALIC),
_ => Style::default(),
};

let hint = match idx {
0 => " (active)",
_ => "",
let style = if idx == app_state.ui_state.current_contract {
Style::default().bg(Color::White).fg(Color::Black)
} else {
Style::default()
};

ListItem::new(Line::from(Span::styled(
format!(
"{} / {}{}",
contract.name,
&contract.address.to_string()[..8],
hint
),
format!("{} / {}", contract.name, &contract.address.to_string()[..8],),
style,
)))
})
Expand Down
2 changes: 1 addition & 1 deletion drink-cli/src/ui/current_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(super) fn build(app_state: &mut AppState) -> impl Widget {
.border_type(BorderType::Rounded)
.padding(Padding::horizontal(1));

let current_contract_info = match app_state.contracts.get(0) {
let current_contract_info = match app_state.contracts.get(app_state.ui_state.current_contract) {
Some(contract) => format!("name: {} | address: {}", contract.name, contract.address,),
None => "No deployed contract".to_string(),
};
Expand Down
11 changes: 6 additions & 5 deletions drink-cli/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,14 @@ fn run_ui_app(terminal: &mut Terminal) -> Result<()> {
app_state.ui_state.user_input.pop();
}
(Drinking, KeyCode::Tab) if app_state.contracts.len() > 1 => {
let prev = app_state.contracts.pop_front().unwrap();
let prev_base_path = prev.base_path.clone();
app_state.contracts.push_back(prev);
let prev_base_path =
&app_state.contracts[app_state.ui_state.current_contract].base_path;

let current = app_state.contracts.front().unwrap();
app_state.ui_state.current_contract =
(app_state.ui_state.current_contract + 1) % app_state.contracts.len();
let current = &app_state.contracts[app_state.ui_state.current_contract];

if current.base_path != prev_base_path {
if current.base_path != *prev_base_path {
let base_path = current.base_path.to_str().unwrap();
app_state.ui_state.user_input = format!("cd {base_path}");
execute(&mut app_state)?;
Expand Down

0 comments on commit c9f8f63

Please sign in to comment.