Skip to content

Commit

Permalink
Printing and scrolling through output
Browse files Browse the repository at this point in the history
  • Loading branch information
pmikolajczyk41 committed Jun 20, 2023
1 parent 4c1214d commit 56eb529
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 8 deletions.
8 changes: 6 additions & 2 deletions drink-cli/src/app_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,22 @@ impl Default for Mode {
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct UiState {
pub pwd: PathBuf,
pub user_input: String,
pub mode: Mode,

pub user_input: String,

pub output: Vec<String>,
pub output_offset: u16,
}

impl Default for UiState {
fn default() -> Self {
UiState {
pwd: env::current_dir().expect("Failed to get current directory"),
user_input: Default::default(),
mode: Default::default(),
user_input: Default::default(),
output: Default::default(),
output_offset: 0,
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions drink-cli/src/executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use anyhow::Result;

use crate::app_state::AppState;

pub fn execute(app_state: &mut AppState) -> Result<()> {
let command = app_state.ui_state.user_input.clone();
app_state
.ui_state
.output
.push(format!("Executing: {}", command));
Ok(())
}
1 change: 1 addition & 0 deletions drink-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::ui::run_ui;

mod app_state;
mod cli;
mod executor;
mod ui;

const CONTRACT_DIR: &str = "../example/";
Expand Down
19 changes: 16 additions & 3 deletions drink-cli/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@ use crossterm::{
};
use layout::layout;
use ratatui::backend::CrosstermBackend;
use sp_runtime::Saturating;

use crate::app_state::{
AppState, Mode,
Mode::{Editing, Managing},
use crate::{
app_state::{
AppState, Mode,
Mode::{Editing, Managing},
},
executor::execute,
};

type Terminal = ratatui::Terminal<CrosstermBackend<Stdout>>;
Expand Down Expand Up @@ -67,6 +71,14 @@ fn run_ui_app(terminal: &mut Terminal) -> Result<()> {
(Managing, KeyCode::Char('i')) => {
*mode = Editing;
}
(Managing, KeyCode::Down) => {
let offset = &mut app_state.ui_state.output_offset;
*offset = offset.saturating_add(1);
}
(Managing, KeyCode::Up) => {
let offset = &mut app_state.ui_state.output_offset;
*offset = offset.saturating_sub(1);
}

(Editing, KeyCode::Char(c)) => {
app_state.ui_state.user_input.push(c);
Expand All @@ -75,6 +87,7 @@ fn run_ui_app(terminal: &mut Terminal) -> Result<()> {
app_state.ui_state.user_input.pop();
}
(Editing, KeyCode::Enter) => {
execute(&mut app_state)?;
app_state.ui_state.user_input.clear();
}

Expand Down
20 changes: 17 additions & 3 deletions drink-cli/src/ui/output.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
use ratatui::widgets::{Block, BorderType, Borders, Paragraph, Widget};
use ratatui::{
text::Line,
widgets::{Block, BorderType, Borders, Padding, Paragraph, Widget},
};

use crate::app_state::AppState;

pub(super) fn build(app_state: &AppState) -> impl Widget {
let block = Block::default()
.title("Output")
.borders(Borders::ALL)
.border_type(BorderType::Rounded);
.border_type(BorderType::Rounded)
.padding(Padding::horizontal(1));

Paragraph::new(app_state.ui_state.output.clone()).block(block)
let output = app_state
.ui_state
.output
.iter()
.rev()
.map(|s| Line::from(s.clone()))
.collect::<Vec<_>>();

Paragraph::new(output)
.block(block)
.scroll((app_state.ui_state.output_offset, 0))
}

0 comments on commit 56eb529

Please sign in to comment.