Added commands to basic mode

This commit is contained in:
Capucho 2020-09-02 12:47:17 +01:00 committed by Marcel Märtens
parent 6e24ff31ee
commit 5fd0e0a5b7
2 changed files with 111 additions and 100 deletions

View File

@ -78,9 +78,7 @@ fn main() -> io::Result<()> {
subscriber.with_writer(|| LOG.clone()).init();
}
if !basic {
tui.run();
}
tui.run(basic);
info!("Starting server...");

View File

@ -93,43 +93,7 @@ impl Tui {
input.pop();
},
KeyCode::Enter => {
let mut args = input.as_str().split_whitespace();
if let Some(cmd_name) = args.next() {
if let Some(cmd) = COMMANDS.iter().find(|cmd| cmd.name == cmd_name) {
let args = args.collect::<Vec<_>>();
let (arg_len, args) = if cmd.split_spaces {
(
args.len(),
args.into_iter()
.map(|s| s.to_string())
.collect::<Vec<String>>(),
)
} else {
(1, vec![args.into_iter().collect::<String>()])
};
match arg_len.cmp(&cmd.args) {
std::cmp::Ordering::Less => {
error!("{} takes {} arguments", cmd_name, cmd.args)
},
std::cmp::Ordering::Greater => {
warn!("{} only takes {} arguments", cmd_name, cmd.args);
let cmd = cmd.cmd;
cmd(args, msg_s)
},
std::cmp::Ordering::Equal => {
let cmd = cmd.cmd;
cmd(args, msg_s)
},
}
} else {
error!("{} not found", cmd_name);
}
}
parse_command(input, msg_s);
*input = String::new();
},
@ -138,12 +102,7 @@ impl Tui {
}
}
pub fn run(&mut self) {
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture).unwrap();
enable_raw_mode().unwrap();
pub fn run(&mut self, basic: bool) {
let hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
Self::shutdown();
@ -153,66 +112,82 @@ impl Tui {
let mut msg_s = self.msg_s.take().unwrap();
let running = self.running.clone();
self.background = Some(std::thread::spawn(move || {
// Start the tui
let stdout = io::stdout();
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend).unwrap();
if basic {
std::thread::spawn(move || {
while running.load(Ordering::Relaxed) {
let mut buf = String::new();
let mut input = String::new();
io::stdin().read_line(&mut buf).unwrap();
if let Err(e) = terminal.clear() {
error!(?e, "clouldn't clean terminal");
};
parse_command(&buf, &mut msg_s);
}
});
} else {
self.background = Some(std::thread::spawn(move || {
// Start the tui
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture).unwrap();
while running.load(Ordering::Relaxed) {
if let Err(e) = terminal.draw(|f| {
let (log_rect, input_rect) = if f.size().height > 6 {
let mut log_rect = f.size();
log_rect.height -= 3;
enable_raw_mode().unwrap();
let mut input_rect = f.size();
input_rect.y = input_rect.height - 3;
input_rect.height = 3;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend).unwrap();
(log_rect, input_rect)
} else {
(f.size(), Rect::default())
let mut input = String::new();
if let Err(e) = terminal.clear() {
error!(?e, "clouldn't clean terminal");
};
while running.load(Ordering::Relaxed) {
if let Err(e) = terminal.draw(|f| {
let (log_rect, input_rect) = if f.size().height > 6 {
let mut log_rect = f.size();
log_rect.height -= 3;
let mut input_rect = f.size();
input_rect.y = input_rect.height - 3;
input_rect.height = 3;
(log_rect, input_rect)
} else {
(f.size(), Rect::default())
};
let block = Block::default().borders(Borders::ALL);
let mut wrap = Wrap::default();
wrap.scroll_callback = Some(Box::new(|text_area, lines| {
LOG.resize(text_area.height as usize);
let len = lines.len() as u16;
(len.saturating_sub(text_area.height), 0)
}));
let logger = Paragraph::new(LOG.inner.lock().unwrap().clone())
.block(block)
.wrap(wrap);
f.render_widget(logger, log_rect);
let text: Text = input.as_str().into();
let block = Block::default().borders(Borders::ALL);
let size = block.inner(input_rect);
let x = (size.x + text.width() as u16).min(size.width);
let input_field = Paragraph::new(text).block(block);
f.render_widget(input_field, input_rect);
f.set_cursor(x, size.y);
}) {
warn!(?e, "couldn't draw frame");
};
let block = Block::default().borders(Borders::ALL);
let mut wrap = Wrap::default();
wrap.scroll_callback = Some(Box::new(|text_area, lines| {
LOG.resize(text_area.height as usize);
let len = lines.len() as u16;
(len.saturating_sub(text_area.height), 0)
}));
let logger = Paragraph::new(LOG.inner.lock().unwrap().clone())
.block(block)
.wrap(wrap);
f.render_widget(logger, log_rect);
let text: Text = input.as_str().into();
let block = Block::default().borders(Borders::ALL);
let size = block.inner(input_rect);
let x = (size.x + text.width() as u16).min(size.width);
let input_field = Paragraph::new(text).block(block);
f.render_widget(input_field, input_rect);
f.set_cursor(x, size.y);
}) {
warn!(?e, "couldn't draw frame");
};
if crossterm::event::poll(Duration::from_millis(10)).unwrap() {
Self::handle_events(&mut input, &mut msg_s);
};
}
}));
if crossterm::event::poll(Duration::from_millis(10)).unwrap() {
Self::handle_events(&mut input, &mut msg_s);
};
}
}));
}
}
fn shutdown() {
@ -230,3 +205,41 @@ impl Drop for Tui {
Self::shutdown();
}
}
fn parse_command(input: &str, msg_s: &mut mpsc::Sender<Message>) {
let mut args = input.split_whitespace();
if let Some(cmd_name) = args.next() {
if let Some(cmd) = COMMANDS.iter().find(|cmd| cmd.name == cmd_name) {
let args = args.collect::<Vec<_>>();
let (arg_len, args) = if cmd.split_spaces {
(
args.len(),
args.into_iter()
.map(|s| s.to_string())
.collect::<Vec<String>>(),
)
} else {
(1, vec![args.into_iter().collect::<String>()])
};
match arg_len.cmp(&cmd.args) {
std::cmp::Ordering::Less => error!("{} takes {} arguments", cmd_name, cmd.args),
std::cmp::Ordering::Greater => {
warn!("{} only takes {} arguments", cmd_name, cmd.args);
let cmd = cmd.cmd;
cmd(args, msg_s)
},
std::cmp::Ordering::Equal => {
let cmd = cmd.cmd;
cmd(args, msg_s)
},
}
} else {
error!("{} not found", cmd_name);
}
}
}