Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is example is aimed at creating a github social preview image and eventually replacing the current main demo. ![Made with VHS](https://vhs.charm.sh/vhs-77i6trU7Mw0JeO4CQ7HLIM.gif)
- Loading branch information
Showing
15 changed files
with
979 additions
and
39 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
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
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,34 @@ | ||
# This is a vhs script. See https://github.com/charmbracelet/vhs for more info. | ||
# To run this script, install vhs and run `vhs ./examples/demo.tape` | ||
Output "target/demo2.gif" | ||
Set Theme "OceanicMaterial" | ||
# Github social preview size (1280x640 with 80px padding) | ||
Set Width 1280 | ||
Set Height 640 | ||
Set Padding 80 | ||
Hide | ||
Type "cargo run --example demo2" | ||
Enter | ||
Sleep 2s | ||
Set TypingSpeed 200ms | ||
Show | ||
Sleep 1.5s | ||
Down 10 | ||
Sleep 1s | ||
Right | ||
Sleep 1.5s | ||
Down 20 | ||
Right | ||
Sleep 1.5s | ||
Down@50ms 40 | ||
Sleep 1s | ||
Right | ||
Sleep 1.5s | ||
Right | ||
Sleep 1.5s | ||
Down @2s 4 | ||
Sleep 2s | ||
Right | ||
Sleep 1.5s | ||
Down@0.5s 28 | ||
Sleep 5s |
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,131 @@ | ||
use std::{ | ||
io::{self, stdout, Stdout}, | ||
time::Duration, | ||
}; | ||
|
||
use crossterm::{ | ||
event::{self, KeyEventKind}, | ||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, | ||
ExecutableCommand, | ||
}; | ||
use ratatui::prelude::*; | ||
|
||
use crate::{main_view::MainView, *}; | ||
|
||
pub struct App { | ||
terminal: Terminal<CrosstermBackend<Stdout>>, | ||
should_quit: bool, | ||
selected_tab: usize, | ||
selected_row: usize, | ||
} | ||
|
||
impl App { | ||
pub fn new() -> Result<Self> { | ||
// this size is to match the size of the terminal when running the demo | ||
// using vhs in a 1280x640 sized window (github social preview size) | ||
let options = TerminalOptions { | ||
viewport: Viewport::Fixed(Rect::new(0, 0, 81, 18)), | ||
// viewport: Viewport::Fullscreen, | ||
}; | ||
let terminal = Terminal::with_options(CrosstermBackend::new(io::stdout()), options)?; | ||
Ok(Self { | ||
terminal, | ||
should_quit: false, | ||
selected_tab: 0, | ||
selected_row: 0, | ||
}) | ||
} | ||
|
||
pub fn run(&mut self) -> Result<()> { | ||
setup_terminal()?; | ||
while !self.should_quit { | ||
self.draw()?; | ||
self.handle_events()?; | ||
} | ||
restore_terminal()?; | ||
Ok(()) | ||
} | ||
|
||
fn draw(&mut self) -> Result<()> { | ||
self.terminal | ||
.draw(|frame| { | ||
let area = frame.size(); | ||
let widget = MainView { | ||
selected_tab: self.selected_tab, | ||
selected_row: self.selected_row, | ||
}; | ||
frame.render_widget(widget, area); | ||
}) | ||
.context("terminal.draw")?; | ||
Ok(()) | ||
} | ||
|
||
fn handle_events(&mut self) -> Result<()> { | ||
if !event::poll(Duration::from_millis(16))? { | ||
return Ok(()); | ||
} | ||
match event::read()? { | ||
event::Event::Key(key) => self.handle_key_event(key), | ||
_ => Ok(()), | ||
} | ||
} | ||
|
||
fn handle_key_event(&mut self, key: event::KeyEvent) -> std::result::Result<(), anyhow::Error> { | ||
if key.kind != KeyEventKind::Press { | ||
return Ok(()); | ||
} | ||
match key.code { | ||
event::KeyCode::Char('q') => { | ||
self.should_quit = true; | ||
} | ||
event::KeyCode::Left | event::KeyCode::Char('h') => { | ||
self.selected_tab = self.selected_tab.saturating_sub(1); | ||
self.selected_row = 0; | ||
} | ||
event::KeyCode::Right | event::KeyCode::Char('l') => { | ||
self.selected_tab = self.selected_tab.saturating_add(1).min(5); | ||
self.selected_row = 0; | ||
} | ||
event::KeyCode::Up | event::KeyCode::Char('k') => { | ||
self.selected_row = self.selected_row.saturating_sub(1); | ||
} | ||
event::KeyCode::Down | event::KeyCode::Char('j') => { | ||
self.selected_row = self.selected_row.saturating_add(1); | ||
} | ||
_ => {} | ||
}; | ||
Ok(()) | ||
} | ||
} | ||
|
||
impl Drop for App { | ||
fn drop(&mut self) { | ||
let _ = restore_terminal(); | ||
} | ||
} | ||
|
||
fn setup_terminal() -> Result<()> { | ||
enable_raw_mode().context("enable raw mode")?; | ||
stdout() | ||
.execute(EnterAlternateScreen) | ||
.context("enter alternate screen")?; | ||
Ok(()) | ||
} | ||
|
||
fn restore_terminal() -> Result<()> { | ||
disable_raw_mode().context("disable raw mode")?; | ||
stdout() | ||
.execute(LeaveAlternateScreen) | ||
.context("leave alternate screen")?; | ||
Ok(()) | ||
} | ||
|
||
pub fn install_panic_hook() { | ||
better_panic::install(); | ||
let hook = std::panic::take_hook(); | ||
std::panic::set_hook(Box::new(move |info| { | ||
let _ = restore_terminal(); | ||
hook(info); | ||
std::process::exit(1); | ||
})); | ||
} |
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,86 @@ | ||
use ratatui::{prelude::*, widgets::*}; | ||
|
||
pub fn render(area: Rect, buf: &mut Buffer) { | ||
let area = Layout::default() | ||
.direction(Direction::Horizontal) | ||
.constraints(vec![Constraint::Ratio(1, 3), Constraint::Ratio(2, 3)]) | ||
.split(area); | ||
|
||
render_simple_barchart(area[0], buf); | ||
render_horizontal_barchart(area[1], buf); | ||
} | ||
|
||
fn render_simple_barchart(area: Rect, buf: &mut Buffer) { | ||
let data = vec![ | ||
("Jan", 10), | ||
("Feb", 20), | ||
("Mar", 30), | ||
("Apr", 40), | ||
("May", 50), | ||
("Jun", 60), | ||
("Jul", 70), | ||
]; | ||
let block = Block::default() | ||
.title("BarChart") | ||
.borders(Borders::ALL) | ||
.border_type(BorderType::Rounded); | ||
BarChart::default() | ||
.data(&data) | ||
.block(block) | ||
.bar_width(3) | ||
.bar_gap(1) | ||
.value_style( | ||
Style::default() | ||
.fg(Color::Black) | ||
.bg(Color::Green) | ||
.add_modifier(Modifier::ITALIC), | ||
) | ||
.label_style(Style::default().fg(Color::Yellow)) | ||
.bar_style(Style::default().fg(Color::Green)) | ||
.render(area, buf); | ||
} | ||
|
||
fn render_horizontal_barchart(area: Rect, buf: &mut Buffer) { | ||
// https://www.videocardbenchmark.net/high_end_gpus.html | ||
let nvidia = Style::new().bg(Color::Green); | ||
let amd = Style::new().bg(Color::Red); | ||
let data = [ | ||
Bar::default() | ||
.text_value("GeForce RTX 4090 (38,978)".into()) | ||
.value_style(nvidia) | ||
.value(38978), | ||
Bar::default() | ||
.text_value("GeForce RTX 4080 (34,879)".into()) | ||
.value_style(nvidia) | ||
.value(34879), | ||
Bar::default() | ||
.text_value("Radeon PRO W7800 (32,146)".into()) | ||
.value_style(amd) | ||
.value(32146), | ||
Bar::default() | ||
.text_value("GeForce RTX 4070 Ti (31,659)".into()) | ||
.value_style(nvidia) | ||
.value(31659), | ||
Bar::default() | ||
.text_value("Radeon RX 7900 XTX (31,180)".into()) | ||
.value_style(amd) | ||
.value(31180), | ||
]; | ||
let group = BarGroup::default().label("GPU".into()).bars(&data); | ||
let block = Block::default() | ||
.title("Passmark") | ||
.borders(Borders::ALL) | ||
.border_type(BorderType::Rounded); | ||
BarChart::default() | ||
.direction(Direction::Horizontal) | ||
.block(block) | ||
.data(group) | ||
.value_style( | ||
Style::default() | ||
.fg(Color::Black) | ||
.add_modifier(Modifier::ITALIC), | ||
) | ||
.label_style(Style::default().fg(Color::Yellow)) | ||
.bar_style(Style::default().light_blue()) | ||
.render(area, buf); | ||
} |
Oops, something went wrong.