-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
117 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pub mod utils; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,8 @@ | ||
use ratatui::{ | ||
crossterm::event::{self, KeyCode, KeyEventKind}, | ||
style::Stylize, | ||
widgets::Paragraph, | ||
DefaultTerminal, | ||
}; | ||
fn main() -> Result<(), std::io::Error> { | ||
println!("Hello, world!"); | ||
let mut terminal = ratatui::init(); | ||
terminal.clear()?; | ||
let app_result = run(terminal); | ||
ratatui::restore(); | ||
app_result | ||
} | ||
|
||
fn run(mut terminal: DefaultTerminal) -> Result<(), std::io::Error> { | ||
loop { | ||
terminal.draw(|frame| { | ||
let greeting = Paragraph::new("Hello Ratatui! (press 'q' to quit)") | ||
.white() | ||
.on_blue(); | ||
frame.render_widget(greeting, frame.area()); | ||
})?; | ||
|
||
if let event::Event::Key(key) = event::read()? { | ||
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') { | ||
return Ok(()); | ||
} | ||
} | ||
} | ||
fn main() { | ||
// println!("Hello, world!"); | ||
// let mut terminal = ratatui::init(); | ||
// terminal.clear()?; | ||
// // let app_result = run(terminal); | ||
// ratatui::restore(); | ||
// app_result | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod command; | ||
pub mod tui; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod tui_base; | ||
pub mod wrapper; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
use ratatui::{DefaultTerminal, Frame}; | ||
|
||
pub trait TuiBase { | ||
fn run(&mut self, terminal: &mut DefaultTerminal) -> Result<(), std::io::Error> { | ||
Ok(()) | ||
} | ||
|
||
fn handle_event(&mut self) -> Result<(), std::io::Error> { | ||
Ok(()) | ||
} | ||
|
||
fn draw(&self, frame: &mut Frame) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use ratatui::{ | ||
layout::Alignment, | ||
style::Stylize, | ||
symbols::border, | ||
text::{Line, Text}, | ||
widgets::{ | ||
block::{Position, Title}, | ||
Block, Paragraph, Widget, | ||
}, | ||
}; | ||
|
||
use super::tui_base::TuiBase; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Wrapper { | ||
counter: u8, | ||
exit: bool, | ||
} | ||
|
||
impl Widget for &Wrapper { | ||
fn render(self, area: ratatui::prelude::Rect, buf: &mut ratatui::prelude::Buffer) { | ||
let title = Title::from(" aurora ui cli ".bold()); | ||
let instructions = Title::from(Line::from(vec![ | ||
" Decrement ".into(), | ||
"<Left>".blue().bold(), | ||
" Increment ".into(), | ||
"<Right>".blue().bold(), | ||
" Quit ".into(), | ||
"<Q> ".blue().bold(), | ||
])); | ||
|
||
let block = Block::bordered() | ||
.title(title.alignment(Alignment::Center)) | ||
.title( | ||
instructions | ||
.alignment(Alignment::Center) | ||
.position(Position::Bottom), | ||
) | ||
.border_set(border::THICK); | ||
|
||
let counter_text = Text::from(vec![Line::from(vec![ | ||
"Value: ".into(), | ||
self.counter.to_string().yellow(), | ||
])]); | ||
|
||
Paragraph::new(counter_text) | ||
.centered() | ||
.block(block) | ||
.render(area, buf); | ||
} | ||
} | ||
|
||
impl TuiBase for Wrapper { | ||
fn draw(&self, frame: &mut ratatui::Frame) { | ||
frame.render_widget(self, ratatui::layout::Rect::default()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use cli::utils::tui::wrapper::Wrapper; | ||
use ratatui::{ | ||
buffer::Buffer, | ||
layout::Rect, | ||
style::{Style, Stylize}, | ||
widgets::Widget, | ||
}; | ||
|
||
#[test] | ||
fn render() { | ||
let app = Wrapper::default(); | ||
let mut buf = Buffer::empty(Rect::new(0, 0, 50, 4)); | ||
|
||
app.render(buf.area, &mut buf); | ||
|
||
let mut expected = Buffer::with_lines(vec![ | ||
"βββββββββββββββββ aurora ui cli ββββββββββββββββββ", | ||
"β Value: 0 β", | ||
"β β", | ||
"ββ Decrement <Left> Increment <Right> Quit <Q> βββ", | ||
]); | ||
let title_style = Style::new().bold(); | ||
let counter_style = Style::new().yellow(); | ||
let key_style = Style::new().blue().bold(); | ||
expected.set_style(Rect::new(17, 0, 15, 1), title_style); | ||
expected.set_style(Rect::new(28, 1, 1, 1), counter_style); | ||
expected.set_style(Rect::new(13, 3, 6, 1), key_style); | ||
expected.set_style(Rect::new(30, 3, 7, 1), key_style); | ||
expected.set_style(Rect::new(43, 3, 4, 1), key_style); | ||
|
||
assert_eq!(buf, expected); | ||
} | ||
} |