|
| 1 | +use std::time::Duration; |
1 | 2 | use xfetch_plugin_api::{ |
2 | | - AnimationFrame, read_logo_animation_request, write_logo_animation_frames, |
| 3 | + AnimationFrame, read_logo_animation_request, with_timeout, write_logo_animation_frames, |
3 | 4 | }; |
4 | 5 |
|
| 6 | +/// Frame generation is CPU-bound; the user-controlled duration can demand |
| 7 | +/// thousands of frames, so the budget is generous. |
| 8 | +const BUDGET: Duration = Duration::from_secs(10); |
| 9 | + |
5 | 10 | fn main() { |
6 | | - let request = match read_logo_animation_request() { |
7 | | - Ok(value) => value, |
8 | | - Err(err) => { |
9 | | - eprintln!("{}", err); |
10 | | - std::process::exit(1); |
11 | | - } |
12 | | - }; |
| 11 | + let frames: Vec<AnimationFrame> = match with_timeout(BUDGET, || { |
| 12 | + let request = match read_logo_animation_request() { |
| 13 | + Ok(value) => value, |
| 14 | + Err(err) => { |
| 15 | + eprintln!("{}", err); |
| 16 | + std::process::exit(1); |
| 17 | + } |
| 18 | + }; |
13 | 19 |
|
14 | | - let args = request.args; |
15 | | - let fps = clamp(args.fps.unwrap_or(12), 1, 60); |
16 | | - let frame_delay = 1000 / fps; |
17 | | - let style = args.style.as_deref().unwrap_or("sweep"); |
| 20 | + let args = request.args; |
| 21 | + let fps = clamp(args.fps.unwrap_or(12), 1, 60); |
| 22 | + let frame_delay = 1000 / fps; |
| 23 | + let style = args.style.as_deref().unwrap_or("sweep"); |
18 | 24 |
|
19 | | - let frame_count = if args.duration_ms.is_none() |
20 | | - && style == "frame" |
21 | | - && request.frames.as_ref().is_some_and(|sets| !sets.is_empty()) |
22 | | - { |
23 | | - request.frames.as_ref().unwrap().len() as u64 |
24 | | - } else { |
25 | | - let duration_ms = std::cmp::max(frame_delay, args.duration_ms.unwrap_or(1200)); |
26 | | - std::cmp::max(1, duration_ms / frame_delay) |
27 | | - }; |
| 25 | + let frame_count = if args.duration_ms.is_none() |
| 26 | + && style == "frame" |
| 27 | + && request.frames.as_ref().is_some_and(|sets| !sets.is_empty()) |
| 28 | + { |
| 29 | + request.frames.as_ref().unwrap().len() as u64 |
| 30 | + } else { |
| 31 | + let duration_ms = std::cmp::max(frame_delay, args.duration_ms.unwrap_or(1200)); |
| 32 | + std::cmp::max(1, duration_ms / frame_delay) |
| 33 | + }; |
28 | 34 |
|
29 | | - let frame_sets = request.frames.unwrap_or_default(); |
| 35 | + let frame_sets = request.frames.unwrap_or_default(); |
30 | 36 |
|
31 | | - let frames: Vec<AnimationFrame> = match style { |
32 | | - "frame" if !frame_sets.is_empty() => { |
33 | | - generate_ascii_frame_animation(&frame_sets, frame_count, frame_delay) |
| 37 | + match style { |
| 38 | + "frame" if !frame_sets.is_empty() => { |
| 39 | + generate_ascii_frame_animation(&frame_sets, frame_count, frame_delay) |
| 40 | + } |
| 41 | + "wave" => generate_wave_animation(&request.lines, frame_count, frame_delay), |
| 42 | + "rainbow" => generate_rainbow_animation(&request.lines, frame_count, frame_delay), |
| 43 | + "sparkle" => generate_sparkle_animation(&request.lines, frame_count, frame_delay), |
| 44 | + "breathing" => generate_breathing_animation(&request.lines, frame_count, frame_delay), |
| 45 | + "none" => generate_static_animation(&request.lines, frame_count, frame_delay), |
| 46 | + _ => generate_sweep_animation(&request.lines, frame_count, frame_delay), |
| 47 | + } |
| 48 | + }) { |
| 49 | + Ok(frames) => frames, |
| 50 | + Err(_) => { |
| 51 | + eprintln!("animate-logo: timed out"); |
| 52 | + std::process::exit(1); |
34 | 53 | } |
35 | | - "wave" => generate_wave_animation(&request.lines, frame_count, frame_delay), |
36 | | - "rainbow" => generate_rainbow_animation(&request.lines, frame_count, frame_delay), |
37 | | - "sparkle" => generate_sparkle_animation(&request.lines, frame_count, frame_delay), |
38 | | - "breathing" => generate_breathing_animation(&request.lines, frame_count, frame_delay), |
39 | | - "none" => generate_static_animation(&request.lines, frame_count, frame_delay), |
40 | | - _ => generate_sweep_animation(&request.lines, frame_count, frame_delay), |
41 | 54 | }; |
42 | 55 |
|
43 | 56 | if let Err(err) = write_logo_animation_frames(frames) { |
|
0 commit comments