-
Notifications
You must be signed in to change notification settings - Fork 0
/
animator.rs
284 lines (239 loc) · 8.23 KB
/
animator.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use crossterm::{
event::{self, Event, KeyCode},
terminal::{disable_raw_mode, enable_raw_mode, Clear, ClearType},
ExecutableCommand,
};
use serde::{Deserialize, Serialize};
use std::fs;
use std::io::{stdout, Write};
use std::time::Duration;
#[derive(Clone, Serialize, Deserialize)]
struct Frame {
content: Vec<String>,
}
#[derive(Serialize, Deserialize)]
struct Animation {
frames: Vec<Frame>,
current_frame: usize,
speed: u64,
}
impl Animation {
fn new() -> Self {
Animation {
frames: vec![],
current_frame: 0,
speed: 500,
}
}
fn add_frame(&mut self, frame: Frame) {
self.frames.push(frame);
}
fn next_frame(&mut self) -> &Frame {
let frame = &self.frames[self.current_frame];
self.current_frame = (self.current_frame + 1) % self.frames.len();
frame
}
fn insert_frame(&mut self, index: usize, frame: Frame) {
self.frames.insert(index, frame);
}
fn delete_frame(&mut self, index: usize) {
if self.frames.len() > 1 {
self.frames.remove(index);
self.current_frame = self.current_frame.min(self.frames.len() - 1);
}
}
fn move_frame(&mut self, from: usize, to: usize) {
if from < self.frames.len() && to < self.frames.len() {
let frame = self.frames.remove(from);
self.frames.insert(to, frame);
}
}
}
fn main() -> crossterm::Result<()> {
let mut animation = Animation::new();
let mut stdout = stdout();
// Example animation
animation.add_frame(Frame {
content: vec![
" o ".to_string(),
" /|\\ ".to_string(),
" / \\ ".to_string(),
],
});
animation.add_frame(Frame {
content: vec![
" o ".to_string(),
" /|\\ ".to_string(),
" | | ".to_string(),
],
});
enable_raw_mode()?;
loop {
stdout.execute(Clear(ClearType::All))?;
let frame = animation.next_frame();
for line in &frame.content {
println!("{}", line);
}
println!("\nMain Menu:");
println!("1. Play animation");
println!("2. Edit current frame");
println!("3. Add new frame");
println!("4. Delete current frame");
println!("5. Reorder frames");
println!("6. Adjust speed (current: {}ms)", animation.speed);
println!("7. Save animation");
println!("8. Load animation");
println!("q. Quit");
stdout.flush()?;
if let Event::Key(key_event) = event::read()? {
match key_event.code {
KeyCode::Char('1') => play_animation(&animation)?,
KeyCode::Char('2') => edit_frame(&mut animation)?,
KeyCode::Char('3') => add_new_frame(&mut animation)?,
KeyCode::Char('4') => delete_current_frame(&mut animation)?,
KeyCode::Char('5') => reorder_frames(&mut animation)?,
KeyCode::Char('6') => adjust_speed(&mut animation)?,
KeyCode::Char('7') => save_animation(&animation)?,
KeyCode::Char('8') => {
animation = load_animation()?;
}
KeyCode::Char('q') => break,
_ => {}
}
}
}
disable_raw_mode()?;
Ok(())
}
fn play_animation(animation: &Animation) -> crossterm::Result<()> {
let mut stdout = stdout();
enable_raw_mode()?;
for _ in 0..50 {
stdout.execute(Clear(ClearType::All))?;
let frame = &animation.frames[animation.current_frame];
for line in &frame.content {
println!("{}", line);
}
stdout.flush()?;
if event::poll(Duration::from_millis(animation.speed))? {
break;
}
}
disable_raw_mode()?;
Ok(())
}
fn edit_frame(animation: &mut Animation) -> crossterm::Result<()> {
let mut stdout = stdout();
stdout.execute(Clear(ClearType::All))?;
let frame = &mut animation.frames[animation.current_frame];
println!("Editing current frame. Commands:");
println!("'a' to add a line, 'e <line_number>' to edit a line, 'd <line_number>' to delete a line");
println!("'q' to finish editing");
loop {
for (i, line) in frame.content.iter().enumerate() {
println!("{}: {}", i, line);
}
let mut command = String::new();
std::io::stdin().read_line(&mut command)?;
let parts: Vec<&str> = command.trim().split_whitespace().collect();
match parts.get(0) {
Some(&"a") => {
println!("Enter new line:");
let mut new_line = String::new();
std::io::stdin().read_line(&mut new_line)?;
frame.content.push(new_line.trim().to_string());
}
Some(&"e") => {
if let Some(line_num) = parts.get(1).and_then(|s| s.parse::<usize>().ok()) {
if line_num < frame.content.len() {
println!("Enter new content for line {}:", line_num);
let mut new_line = String::new();
std::io::stdin().read_line(&mut new_line)?;
frame.content[line_num] = new_line.trim().to_string();
}
}
}
Some(&"d") => {
if let Some(line_num) = parts.get(1).and_then(|s| s.parse::<usize>().ok()) {
if line_num < frame.content.len() {
frame.content.remove(line_num);
}
}
}
Some(&"q") => break,
_ => println!("Invalid command"),
}
stdout.execute(Clear(ClearType::All))?;
}
Ok(())
}
fn add_new_frame(animation: &mut Animation) -> crossterm::Result<()> {
let mut stdout = stdout();
stdout.execute(Clear(ClearType::All))?;
println!("Adding a new frame. Enter content (empty line to finish):");
let mut new_content = vec![];
loop {
let mut line = String::new();
std::io::stdin().read_line(&mut line)?;
if line.trim().is_empty() {
break;
}
new_content.push(line.trim_end().to_string());
}
if !new_content.is_empty() {
animation.add_frame(Frame { content: new_content });
}
Ok(())
}
fn delete_current_frame(animation: &mut Animation) -> crossterm::Result<()> {
animation.delete_frame(animation.current_frame);
Ok(())
}
fn reorder_frames(animation: &mut Animation) -> crossterm::Result<()> {
let mut stdout = stdout();
stdout.execute(Clear(ClearType::All))?;
println!("Current frame order:");
for (i, frame) in animation.frames.iter().enumerate() {
println!("{}. {} lines", i, frame.content.len());
}
println!("\nEnter the frame number to move, followed by its new position:");
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
let parts: Vec<usize> = input
.split_whitespace()
.filter_map(|s| s.parse().ok())
.collect();
if parts.len() == 2 {
animation.move_frame(parts[0], parts[1]);
}
Ok(())
}
fn adjust_speed(animation: &mut Animation) -> crossterm::Result<()> {
println!("Enter new speed in milliseconds:");
let mut input = String::new();
std::io::stdin().read_line(&mut input)?;
if let Ok(new_speed) = input.trim().parse() {
animation.speed = new_speed;
}
Ok(())
}
fn save_animation(animation: &Animation) -> crossterm::Result<()> {
println!("Enter filename to save:");
let mut filename = String::new();
std::io::stdin().read_line(&mut filename)?;
let filename = filename.trim();
let serialized = serde_json::to_string(animation).unwrap();
fs::write(filename, serialized)?;
println!("Animation saved to {}", filename);
Ok(())
}
fn load_animation() -> crossterm::Result<Animation> {
println!("Enter filename to load:");
let mut filename = String::new();
std::io::stdin().read_line(&mut filename)?;
let filename = filename.trim();
let contents = fs::read_to_string(filename)?;
let animation: Animation = serde_json::from_str(&contents).unwrap();
println!("Animation loaded from {}", filename);
Ok(animation)
}