-
-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(examples): add demo2 example (#500)
- Loading branch information
Showing
21 changed files
with
1,431 additions
and
55 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
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
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,43 @@ | ||
# 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-social.gif" | ||
Set Theme "OceanicMaterial" | ||
# Github social preview size (1280x640 with 80px padding) and must be < 1MB | ||
# This puts some constraints on the amount of interactivity we can do. | ||
Set Width 1280 | ||
Set Height 640 | ||
Set Padding 80 | ||
Hide | ||
Type "cargo run --example demo2 --features crossterm,widget-calendar" | ||
Enter | ||
Sleep 2s | ||
Show | ||
# About screen | ||
Sleep 3.5s | ||
Down # Red eye | ||
Sleep 0.4s | ||
Down # black eye | ||
Sleep 1s | ||
Tab | ||
# Recipe | ||
Sleep 1s | ||
Set TypingSpeed 500ms | ||
Down 7 | ||
Sleep 1s | ||
Tab | ||
Sleep 2s | ||
Down 4 | ||
Sleep 2s | ||
Tab | ||
# Trace route | ||
Sleep 1s | ||
Set TypingSpeed 200ms | ||
Down 10 | ||
Sleep 2s | ||
Tab | ||
# Weather | ||
Set TypingSpeed 100ms | ||
Down 40 | ||
Sleep 2s |
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,49 @@ | ||
# 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` | ||
# NOTE: Requires VHS 0.6.1 or later for Screenshot support | ||
Output "target/demo2.gif" | ||
Set Theme "OceanicMaterial" | ||
# The reason for this strange size is that the social preview image for this | ||
# demo is 1280x64 with 80 pixels of padding on each side. We want a version | ||
# without the padding for README.md, etc. | ||
Set Width 1120 | ||
Set Height 480 | ||
Set Padding 0 | ||
Hide | ||
Type "cargo run --example demo2 --features crossterm,widget-calendar" | ||
Enter | ||
Sleep 2s | ||
Show | ||
# About screen | ||
Screenshot "target/demo2-about.png" | ||
Sleep 3.5s | ||
Down # Red eye | ||
Sleep 0.4s | ||
Down # black eye | ||
Sleep 1s | ||
Tab | ||
# Recipe | ||
Screenshot "target/demo2-recipe.png" | ||
Sleep 1s | ||
Set TypingSpeed 500ms | ||
Down 7 | ||
Sleep 1s | ||
Tab | ||
Screenshot "target/demo2-email.png" | ||
Sleep 2s | ||
Down 4 | ||
Sleep 2s | ||
Tab | ||
# Trace route | ||
Screenshot "target/demo2-trace.png" | ||
Sleep 1s | ||
Set TypingSpeed 200ms | ||
Down 10 | ||
Sleep 2s | ||
Tab | ||
# Weather | ||
Screenshot "target/demo2-weather.png" | ||
Set TypingSpeed 100ms | ||
Down 40 | ||
Sleep 2s |
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,99 @@ | ||
use std::time::Duration; | ||
|
||
use anyhow::{Context, Result}; | ||
use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}; | ||
use ratatui::prelude::Rect; | ||
|
||
use crate::{Root, Term}; | ||
|
||
#[derive(Debug)] | ||
pub struct App { | ||
term: Term, | ||
should_quit: bool, | ||
context: AppContext, | ||
} | ||
|
||
#[derive(Debug, Default, Clone, Copy)] | ||
pub struct AppContext { | ||
pub tab_index: usize, | ||
pub row_index: usize, | ||
} | ||
|
||
impl App { | ||
fn new() -> Result<Self> { | ||
Ok(Self { | ||
term: Term::start()?, | ||
should_quit: false, | ||
context: AppContext::default(), | ||
}) | ||
} | ||
|
||
pub fn run() -> Result<()> { | ||
install_panic_hook(); | ||
let mut app = Self::new()?; | ||
while !app.should_quit { | ||
app.draw()?; | ||
app.handle_events()?; | ||
} | ||
Term::stop()?; | ||
Ok(()) | ||
} | ||
|
||
fn draw(&mut self) -> Result<()> { | ||
self.term | ||
.draw(|frame| frame.render_widget(Root::new(&self.context), frame.size())) | ||
.context("terminal.draw")?; | ||
Ok(()) | ||
} | ||
|
||
fn handle_events(&mut self) -> Result<()> { | ||
match Term::next_event(Duration::from_millis(16))? { | ||
Some(Event::Key(key)) => self.handle_key_event(key), | ||
Some(Event::Resize(width, height)) => { | ||
Ok(self.term.resize(Rect::new(0, 0, width, height))?) | ||
} | ||
_ => Ok(()), | ||
} | ||
} | ||
|
||
fn handle_key_event(&mut self, key: KeyEvent) -> Result<()> { | ||
if key.kind != KeyEventKind::Press { | ||
return Ok(()); | ||
} | ||
|
||
let context = &mut self.context; | ||
const TAB_COUNT: usize = 5; | ||
match key.code { | ||
KeyCode::Char('q') | KeyCode::Esc => { | ||
self.should_quit = true; | ||
} | ||
KeyCode::Tab | KeyCode::BackTab if key.modifiers.contains(KeyModifiers::SHIFT) => { | ||
let tab_index = context.tab_index + TAB_COUNT; // to wrap around properly | ||
context.tab_index = tab_index.saturating_sub(1) % TAB_COUNT; | ||
context.row_index = 0; | ||
} | ||
KeyCode::Tab | KeyCode::BackTab => { | ||
context.tab_index = context.tab_index.saturating_add(1) % TAB_COUNT; | ||
context.row_index = 0; | ||
} | ||
KeyCode::Up | KeyCode::Char('k') => { | ||
context.row_index = context.row_index.saturating_sub(1); | ||
} | ||
KeyCode::Down | KeyCode::Char('j') => { | ||
context.row_index = context.row_index.saturating_add(1); | ||
} | ||
_ => {} | ||
}; | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub fn install_panic_hook() { | ||
better_panic::install(); | ||
let hook = std::panic::take_hook(); | ||
std::panic::set_hook(Box::new(move |info| { | ||
let _ = Term::stop(); | ||
hook(info); | ||
std::process::exit(1); | ||
})); | ||
} |
Oops, something went wrong.