Compare commits
10 Commits
dependabot
...
v0.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 51c2c5a14a | |||
| 12793893c3 | |||
| 946289544d | |||
| 4e88ea5677 | |||
| b2a9b6df36 | |||
| 5a707df725 | |||
| 097fca6ed3 | |||
| 5a0ff3f5cc | |||
| 4035f2dc23 | |||
| 000823457d |
10
.github/workflows/release.yml
vendored
10
.github/workflows/release.yml
vendored
@@ -5,6 +5,9 @@ on:
|
||||
tags:
|
||||
- 'v*.*.*'
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
|
||||
env:
|
||||
CARGO_TERM_COLOR: always
|
||||
RUST_BACKTRACE: 1
|
||||
@@ -148,11 +151,14 @@ jobs:
|
||||
if: matrix.os == 'macos-latest'
|
||||
run: strip target/${{ matrix.target }}/release/piglet
|
||||
|
||||
- name: Rename binary
|
||||
run: |
|
||||
cp target/${{ matrix.target }}/release/piglet piglet-${{ matrix.target }}
|
||||
|
||||
- name: Upload release binary
|
||||
uses: softprops/action-gh-release@v1
|
||||
with:
|
||||
files: target/${{ matrix.target }}/release/piglet
|
||||
name: piglet-${{ matrix.target }}
|
||||
files: piglet-${{ matrix.target }}
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
|
||||
|
||||
@@ -21,8 +21,8 @@ palette = "0.7"
|
||||
# Terminal manipulation
|
||||
crossterm = "0.27"
|
||||
|
||||
# Async runtime (for timing)
|
||||
tokio = { version = "1.35", features = ["time", "rt-multi-thread", "macros"] }
|
||||
# Async runtime (for timing and signal handling)
|
||||
tokio = { version = "1.35", features = ["time", "rt-multi-thread", "macros", "signal", "sync"] }
|
||||
|
||||
# Process execution
|
||||
which = "5.0"
|
||||
|
||||
@@ -43,7 +43,7 @@ impl AnimationEngine {
|
||||
self
|
||||
}
|
||||
|
||||
pub async fn run(&self, terminal: &mut TerminalManager) -> Result<()> {
|
||||
pub async fn run(&self, terminal: &mut TerminalManager) -> Result<bool> {
|
||||
let renderer = renderer::Renderer::new(
|
||||
&self.ascii_art,
|
||||
self.duration_ms,
|
||||
|
||||
@@ -2,6 +2,12 @@ use crate::animation::{easing::EasingFunction, effects::Effect, timeline::Timeli
|
||||
use crate::color::{apply, ColorEngine};
|
||||
use crate::utils::{ansi, ascii::AsciiArt, terminal::TerminalManager};
|
||||
use anyhow::Result;
|
||||
use crossterm::event::{self, Event, KeyCode, KeyModifiers};
|
||||
use std::sync::{
|
||||
atomic::{AtomicBool, Ordering},
|
||||
Arc,
|
||||
};
|
||||
use std::time::Duration;
|
||||
use tokio::time::sleep;
|
||||
|
||||
pub struct Renderer<'a> {
|
||||
@@ -30,17 +36,52 @@ impl<'a> Renderer<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn render(&self, terminal: &mut TerminalManager) -> Result<()> {
|
||||
pub async fn render(&self, terminal: &mut TerminalManager) -> Result<bool> {
|
||||
let mut timeline = Timeline::new(self.timeline.duration_ms(), self.timeline.fps());
|
||||
timeline.start();
|
||||
|
||||
// Spawn background thread to listen for exit keys
|
||||
let should_exit = Arc::new(AtomicBool::new(false));
|
||||
let should_exit_clone = should_exit.clone();
|
||||
|
||||
std::thread::spawn(move || loop {
|
||||
if let Ok(true) = event::poll(Duration::from_millis(100)) {
|
||||
if let Ok(Event::Key(key)) = event::read() {
|
||||
match key.code {
|
||||
KeyCode::Char('q') | KeyCode::Esc => {
|
||||
should_exit_clone.store(true, Ordering::Relaxed);
|
||||
break;
|
||||
}
|
||||
KeyCode::Char('c') if key.modifiers.contains(KeyModifiers::CONTROL) => {
|
||||
should_exit_clone.store(true, Ordering::Relaxed);
|
||||
break;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
if should_exit_clone.load(Ordering::Relaxed) {
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
loop {
|
||||
// Check for exit FIRST
|
||||
if should_exit.load(Ordering::Relaxed) {
|
||||
return Ok(true); // User requested exit
|
||||
}
|
||||
|
||||
let frame_start = std::time::Instant::now();
|
||||
|
||||
// Calculate progress with easing
|
||||
let linear_progress = timeline.progress();
|
||||
let eased_progress = self.easing.ease(linear_progress);
|
||||
|
||||
// Check again before rendering
|
||||
if should_exit.load(Ordering::Relaxed) {
|
||||
return Ok(true); // User requested exit
|
||||
}
|
||||
|
||||
// Apply effect
|
||||
let effect_result = self.effect.apply(self.ascii_art, eased_progress);
|
||||
|
||||
@@ -51,6 +92,11 @@ impl<'a> Renderer<'a> {
|
||||
effect_result.text.clone()
|
||||
};
|
||||
|
||||
// Check before terminal operations
|
||||
if should_exit.load(Ordering::Relaxed) {
|
||||
return Ok(true); // User requested exit
|
||||
}
|
||||
|
||||
// Render to terminal
|
||||
terminal.clear()?;
|
||||
terminal.refresh_size()?;
|
||||
@@ -82,9 +128,14 @@ impl<'a> Renderer<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
// Check if user wants to exit
|
||||
if should_exit.load(Ordering::Relaxed) {
|
||||
return Ok(true); // User requested exit
|
||||
}
|
||||
|
||||
// Check if animation is complete before advancing
|
||||
if timeline.is_complete() {
|
||||
break;
|
||||
return Ok(false); // Animation completed naturally
|
||||
}
|
||||
|
||||
// Advance to next frame and wait
|
||||
@@ -93,11 +144,21 @@ impl<'a> Renderer<'a> {
|
||||
let elapsed = frame_start.elapsed();
|
||||
|
||||
if elapsed < frame_duration {
|
||||
sleep(frame_duration - elapsed).await;
|
||||
let sleep_duration = frame_duration - elapsed;
|
||||
// Break sleep into small chunks to check should_exit frequently
|
||||
let chunk_duration = Duration::from_millis(5);
|
||||
let mut remaining = sleep_duration;
|
||||
|
||||
while remaining > Duration::ZERO {
|
||||
if should_exit.load(Ordering::Relaxed) {
|
||||
return Ok(true); // User requested exit during sleep
|
||||
}
|
||||
let sleep_time = remaining.min(chunk_duration);
|
||||
sleep(sleep_time).await;
|
||||
remaining = remaining.saturating_sub(sleep_time);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn apply_colors(&self, text: &str, progress: f64) -> String {
|
||||
|
||||
@@ -61,8 +61,14 @@ async fn run_piglet(args: PigletCli) -> Result<()> {
|
||||
|
||||
// Run animation
|
||||
loop {
|
||||
animation_engine.run(&mut terminal).await?;
|
||||
let user_exited = animation_engine.run(&mut terminal).await?;
|
||||
|
||||
// If user pressed exit key, stop looping
|
||||
if user_exited {
|
||||
break;
|
||||
}
|
||||
|
||||
// If not looping, stop after one animation
|
||||
if !args.loop_animation {
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user