-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.rs
99 lines (95 loc) · 3.04 KB
/
main.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
use ayaka_runtime::{anyhow::Result, *};
use clap::Parser;
use flexi_logger::{LogSpecification, Logger};
use std::{
ffi::OsString,
io::{stdin, stdout, Write},
};
#[derive(Debug, Parser)]
#[clap(about, version, author)]
pub struct Options {
#[clap(required = true)]
input: Vec<OsString>,
#[clap(long)]
auto: bool,
#[clap(short, long)]
locale: Option<Locale>,
}
fn read_line() -> Result<String> {
stdout().flush()?;
let mut s = String::new();
stdin().read_line(&mut s)?;
Ok(s)
}
fn pause(auto: bool) -> Result<()> {
if auto {
println!();
} else {
read_line()?;
}
Ok(())
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
let opts = Options::parse();
let spec = LogSpecification::parse("warn,ayaka=debug")?;
let _log_handle = Logger::with(spec)
.log_to_stdout()
.set_palette("b1;3;2;4;6".to_string())
.use_utc()
.start()?;
let context = Context::open(&opts.input, FrontendType::Text);
pin_mut!(context);
while let Some(status) = context.next().await {
match status {
OpenStatus::LoadProfile => println!("Loading profile..."),
OpenStatus::CreateRuntime => println!("Creating runtime..."),
OpenStatus::LoadPlugin(name, i, len) => {
println!("Loading plugin {} ({}/{})", name, i + 1, len)
}
OpenStatus::GamePlugin => println!("Preprocessing game..."),
OpenStatus::LoadResource => println!("Loading resources..."),
OpenStatus::LoadParagraph => println!("Loading paragraphs..."),
}
}
let mut ctx = context.await?;
ctx.init_new();
let loc = opts.locale.unwrap_or_else(Locale::current);
while let Some(raw_ctx) = ctx.next_run() {
let action = ctx.get_action(&loc, &raw_ctx)?;
match action {
Action::Empty | Action::Custom(_) => {}
Action::Text(action) => {
if let Some(name) = &action.character {
print!("_{}_", name);
}
for s in &action.text {
print!("{}", s.as_str());
}
pause(opts.auto)?;
}
Action::Switches(switches) => {
for (i, s) in switches.iter().enumerate() {
if s.enabled {
print!("\n-{}- {}", i + 1, s.text);
} else {
print!("\n-x- {}", s.text);
}
}
println!();
loop {
let s = read_line()?;
if let Ok(i) = s.trim().parse::<usize>() {
let valid = i > 0 && i <= switches.len() && switches[i - 1].enabled;
if valid {
ctx.switch(i - 1);
break;
}
}
println!("Invalid switch, enter again!");
}
}
}
}
Ok(())
}