Add user interface
This commit is contained in:
153
src/userinterface.rs
Normal file
153
src/userinterface.rs
Normal file
@@ -0,0 +1,153 @@
|
||||
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
|
||||
use crossterm::terminal::{
|
||||
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
|
||||
};
|
||||
use ratatui::backend::CrosstermBackend;
|
||||
use ratatui::layout::{Constraint, Layout};
|
||||
use ratatui::style::{Color, Style};
|
||||
use ratatui::widgets::{Block, Borders};
|
||||
use ratatui::Terminal;
|
||||
use std::io;
|
||||
use tui_textarea::{Input, Key, TextArea};
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
fn validate_user(textarea: &mut TextArea) -> bool {
|
||||
if let Err(_) = textarea.lines()[0].parse::<u64>() {
|
||||
textarea.set_style(Style::default().fg(Color::LightRed));
|
||||
textarea.set_block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(format!("ERROR: Please enter a valid user number")),
|
||||
);
|
||||
false
|
||||
} else {
|
||||
textarea.set_style(Style::default().fg(Color::LightGreen));
|
||||
textarea.set_block(Block::default().borders(Borders::ALL).title("OK"));
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_rating(textarea: &mut TextArea) -> bool {
|
||||
match textarea.lines()[0].parse::<u64>() {
|
||||
Err(_) => {
|
||||
textarea.set_style(Style::default().fg(Color::LightRed));
|
||||
textarea.set_block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(format!("ERROR: Rating must be between 1-5.")),
|
||||
);
|
||||
false
|
||||
}
|
||||
Ok(value) => {
|
||||
if value > 0 && value < 6 {
|
||||
textarea.set_style(Style::default().fg(Color::LightGreen));
|
||||
textarea.set_block(Block::default().borders(Borders::ALL).title("OK"));
|
||||
true
|
||||
} else {
|
||||
textarea.set_block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.title(format!("ERROR: Rating must be between 1-5.")),
|
||||
);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_user_rating() -> Result<(String, String)> {
|
||||
let stdout = io::stdout();
|
||||
let mut stdout = stdout.lock();
|
||||
|
||||
enable_raw_mode()?;
|
||||
crossterm::execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
||||
let backend = CrosstermBackend::new(stdout);
|
||||
let mut term = Terminal::new(backend)?;
|
||||
|
||||
let mut textarea = TextArea::default();
|
||||
textarea.set_cursor_line_style(Style::default());
|
||||
textarea.set_placeholder_text("USER: Enter a valid user number (e.g. 156)");
|
||||
let layout =
|
||||
Layout::default().constraints([Constraint::Length(3), Constraint::Min(1)].as_slice());
|
||||
let mut is_valid = validate_user(&mut textarea);
|
||||
|
||||
loop {
|
||||
term.draw(|f| {
|
||||
let chunks = layout.split(f.size());
|
||||
let widget = textarea.widget();
|
||||
f.render_widget(widget, chunks[0]);
|
||||
})?;
|
||||
|
||||
match crossterm::event::read()?.into() {
|
||||
//Input { key: Key::Esc, .. } => break,
|
||||
Input {
|
||||
key: Key::Enter, ..
|
||||
} if is_valid => break,
|
||||
Input {
|
||||
key: Key::Char('m'),
|
||||
ctrl: true,
|
||||
..
|
||||
}
|
||||
| Input {
|
||||
key: Key::Enter, ..
|
||||
} => {}
|
||||
input => {
|
||||
// TextArea::input returns if the input modified its text
|
||||
if textarea.input(input) {
|
||||
is_valid = validate_user(&mut textarea);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let user_number = textarea.lines()[0].clone();
|
||||
|
||||
let mut textarea = TextArea::default();
|
||||
textarea.set_cursor_line_style(Style::default());
|
||||
textarea.set_placeholder_text("RATING: Enter a valid rating (between 1-5)");
|
||||
let layout =
|
||||
Layout::default().constraints([Constraint::Length(3), Constraint::Min(1)].as_slice());
|
||||
let mut is_valid = validate_rating(&mut textarea);
|
||||
|
||||
loop {
|
||||
term.draw(|f| {
|
||||
let chunks = layout.split(f.size());
|
||||
let widget = textarea.widget();
|
||||
f.render_widget(widget, chunks[0]);
|
||||
})?;
|
||||
|
||||
match crossterm::event::read()?.into() {
|
||||
//Input { key: Key::Esc, .. } => break,
|
||||
Input {
|
||||
key: Key::Enter, ..
|
||||
} if is_valid => break,
|
||||
Input {
|
||||
key: Key::Char('m'),
|
||||
ctrl: true,
|
||||
..
|
||||
}
|
||||
| Input {
|
||||
key: Key::Enter, ..
|
||||
} => {}
|
||||
input => {
|
||||
// TextArea::input returns if the input modified its text
|
||||
if textarea.input(input) {
|
||||
is_valid = validate_rating(&mut textarea);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let user_rating = textarea.lines()[0].clone();
|
||||
|
||||
disable_raw_mode()?;
|
||||
crossterm::execute!(
|
||||
term.backend_mut(),
|
||||
LeaveAlternateScreen,
|
||||
DisableMouseCapture
|
||||
)?;
|
||||
term.show_cursor()?;
|
||||
|
||||
Ok((user_number, user_rating))
|
||||
}
|
Reference in New Issue
Block a user