From 0b29e9b14607f3226c46f75a20f11abd49c8f4a6 Mon Sep 17 00:00:00 2001 From: "Julian T." Date: Sun, 28 Apr 2024 00:05:33 +0200 Subject: Start with tui instead --- src/commands/mod.rs | 2 -- src/commands/parser.rs | 77 -------------------------------------------------- src/commands/start.rs | 12 -------- src/main.rs | 10 +++++-- src/tui.rs | 24 ++++++++++++++++ 5 files changed, 31 insertions(+), 94 deletions(-) delete mode 100644 src/commands/mod.rs delete mode 100644 src/commands/parser.rs delete mode 100644 src/commands/start.rs create mode 100644 src/tui.rs (limited to 'src') diff --git a/src/commands/mod.rs b/src/commands/mod.rs deleted file mode 100644 index 5a61ce8..0000000 --- a/src/commands/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -mod start; -mod parser; diff --git a/src/commands/parser.rs b/src/commands/parser.rs deleted file mode 100644 index e75840c..0000000 --- a/src/commands/parser.rs +++ /dev/null @@ -1,77 +0,0 @@ - -pub enum ParseResult<'a, T> { - Success(T, &'a str), - Failure(String), -} - -pub trait Parse<'a> { - type Res; - - fn parse(self, input: &'a str) -> ParseResult<'a, Self::Res>; -} - -pub struct Text(String); - -impl<'a> Parse<'a> for Text { - type Res = Text; - - fn parse(self, input: &'a str) -> ParseResult<'a, Text> { - let mut value = String::new(); - - let mut it = input.chars(); - let mut escape_next = false; - while let Some(c) = it.next() { - if c == '\\' && !escape_next { - escape_next = true; - continue; - } else if c == ';' { - break; - } - value.push(c); - - escape_next = false; - } - - ParseResult::Success(Text(value), it.as_str()) - } -} - -pub struct Branch { - choices: Vec<(&'static str, Option, T)>, -} - -impl<'a, T> Parse<'a> for Branch { - type Res = T; - - fn parse(self, input: &'a str) -> ParseResult<'a, T> { - let choices: Vec<&'static str> = self.choices.iter().map(|x| x.0).collect(); - - for (long, short_op, res) in self.choices { - let rest = if input.starts_with(long) { - Some(&input[long.len()..]) - } else if let Some(short) = short_op { - if input.starts_with(short) { - Some(&input[1..]) - } else { - None - } - } else { None }; - - if let Some(rest) = rest { - return ParseResult::Success(res, rest); - } - } - - return ParseResult::Failure(format!("Expected one of [{}]", choices.join(", "))); - } -} - -impl Branch { - pub fn new() -> Branch { - Branch { choices: Vec::new() } - } - - pub fn add(&mut self, long: &'static str, short: Option, res: T) { - self.choices.push((long, short, res)); - } -} diff --git a/src/commands/start.rs b/src/commands/start.rs deleted file mode 100644 index a122d5c..0000000 --- a/src/commands/start.rs +++ /dev/null @@ -1,12 +0,0 @@ -use crate::commands::parser::{Text, Parse}; - -pub enum Commands { - Start(Text), -} - -impl<'a> Parse<'a> for Commands { - type Res = Commands; - - fn parse(self -} - diff --git a/src/main.rs b/src/main.rs index b97e52d..e5a8fa2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,9 @@ -mod commands; +use std::io::Result; -fn main() { - println!("Hello, world!"); +mod tui; + +fn main() -> Result<()> { + let mut terminal = tui::init(); + tui::restore(); + Ok(()) } diff --git a/src/tui.rs b/src/tui.rs new file mode 100644 index 0000000..7bae1ab --- /dev/null +++ b/src/tui.rs @@ -0,0 +1,24 @@ +use crossterm::{execute, terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}}; + +use ratatui::{ + prelude::CrosstermBackend, + Terminal, +}; + +use std::io::{self, stdout, Stdout}; + + +pub type Tui = Terminal>; + +pub fn init() -> io::Result { + execute!(stdout(), EnterAlternateScreen)?; + enable_raw_mode()?; + Terminal::new(CrosstermBackend::new(stdout())) +} + +pub fn restore() -> io::Result<()> { + execute!(stdout(), LeaveAlternateScreen)?; + disable_raw_mode()?; + + Ok(()) +} -- cgit v1.2.3