Skip to content

Commit

Permalink
Add docs; --no-log flag
Browse files Browse the repository at this point in the history
  • Loading branch information
oissevalt committed Nov 14, 2023
1 parent c61e8cc commit 45157a0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
- `--cwd` 工作路径,即解压到该路径下。默认为当前路径
- `--verbose` 打印更多信息
- `--skip-startup` 解压后,不启动海豹
- `--no-log` 不创建更新日志
3 changes: 3 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@ pub struct CliArgs {
/// Do not start SealDice after decompressing
#[arg(long = "skip-startup")]
pub skip_startup: bool,
/// Produce no log for the run
#[arg(long = "no-log")]
pub no_log: bool
}
44 changes: 27 additions & 17 deletions src/colorize.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
use once_cell::sync::Lazy;

/// Indicates whether the program is running on Windows Terminal,
/// thus ANSI escape codes supported. There is a chance it may fail.
static WIN_TERM: Lazy<bool> = Lazy::new(|| std::env::var("WT_SESSION").is_ok());

/// Contains the original string and a vector of ANSI color codes.
pub struct ColoredString {
original: String,
codes: Vec<i32>,
}

impl ColoredString {
/// Returns a new `ColoredString` with an intial color code.
/// Use 39 for default text color, 49 for default background color,
/// or 0 that represents 'reset'.
pub fn new_with(s: &str, code: i32) -> Self {
ColoredString {
original: s.to_owned(),
codes: vec![code],
}
}

fn push_color(&mut self, code: i32) {
/// Adds a new color code to `Self`.
fn push(&mut self, code: i32) {
self.codes.push(code);
}

/// Creates a new `ColoredString` from a `&str`.
fn from_str(s: &str, code: i32) -> ColoredString {
ColoredString::new_with(s, code)
}
Expand All @@ -41,6 +49,8 @@ impl std::fmt::Display for ColoredString {
}
}

/// Extends `&str` and `ColoredString` to support text wrapped with
/// color codes.
pub trait Colorize {
fn black(self) -> ColoredString
where
Expand Down Expand Up @@ -212,127 +222,127 @@ impl Colorize for ColoredString {
where
Self: Sized,
{
self.push_color(30);
self.push(30);
self
}

fn red(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(31);
self.push(31);
self
}

fn green(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(32);
self.push(32);
self
}

fn yellow(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(33);
self.push(33);
self
}

fn blue(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(34);
self.push(34);
self
}

fn magenta(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(35);
self.push(35);
self
}

fn cyan(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(36);
self.push(36);
self
}

fn white(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(37);
self.push(37);
self
}

fn on_black(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(40);
self.push(40);
self
}

fn on_red(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(41);
self.push(41);
self
}

fn on_green(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(42);
self.push(42);
self
}

fn on_yellow(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(43);
self.push(43);
self
}

fn on_blue(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(44);
self.push(44);
self
}

fn on_magenta(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(45);
self.push(45);
self
}

fn on_cyan(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(46);
self.push(46);
self
}

fn on_white(mut self) -> ColoredString
where
Self: Sized,
{
self.push_color(47);
self.push(47);
self
}
}
28 changes: 22 additions & 6 deletions src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
use crate::global::CMD_OPT;

/// Initialize the logger, making it create a new file and write to that file.
/// If `no_log` in [CMD_OPT] is set to `true` with `--no-log` flag,
/// all logging methods will be no-ops.
pub fn init_logger() -> Result<String, fern::InitError> {
let time = chrono::Local::now().format("%F_%H%M%S").to_string();
let file_name = format!("升级日志_{}.txt", time);
let mut file_name = String::new();
let log_level = if CMD_OPT.no_log {
log::LevelFilter::Off
} else {
log::LevelFilter::Info
};

fern::Dispatch::new()
let cfg = fern::Dispatch::new()
.format(|out, msg, rec| {
out.finish(format_args!(
"{} [{}] {}:{} {}",
Expand All @@ -13,9 +22,16 @@ pub fn init_logger() -> Result<String, fern::InitError> {
msg
))
})
.level(log::LevelFilter::Info)
.chain(fern::log_file(&file_name)?)
.apply()?;
.level(log_level);

if !CMD_OPT.no_log {
let date = chrono::Local::now().format("%F_%H%M%S").to_string();
file_name = format!("升级日志_{}.txt", date);

cfg.chain(fern::log_file(&file_name)?).apply()?;
} else {
cfg.apply()?;
}

Ok(file_name)
}
13 changes: 10 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ fn main() {
);

match init_logger() {
Ok(name) => println!("本次升级日志将被写入 {}", name.yellow()),
Ok(name) => {
if name != "" {
println!("本次升级日志将被写入 {}", name.yellow())
}
},
Err(e) => eprintln!("{}", format!("未能初始化升级日志: {}", e).red()),
}

Expand Down Expand Up @@ -61,13 +65,14 @@ fn run_command(path: impl AsRef<Path>) {
if CMD_OPT.verbose {
println!(
"Running `chmod` on {}",
&path.as_ref().join(SEAL_EXE).to_string_lossy().on_yellow()
&path.as_ref().join(SEAL_EXE).to_string_lossy().yellow()
);
info!(
"运行 `chmod` 于 {}",
&path.as_ref().join(SEAL_EXE).to_string_lossy()
);
}

let res = Command::new("chmod")
.args(["+x", &path.as_ref().join(SEAL_EXE).to_string_lossy()])
.output();
Expand Down Expand Up @@ -97,7 +102,8 @@ fn run_command(path: impl AsRef<Path>) {
}

println!("{}\n", "升级完毕,即将启动海豹核心…".black().on_yellow());

info!("准备运行海豹主程序,如果海豹没有启动,但下面没有出现报错信息,应该是 {} 的问题", SEAL_EXE);

std::thread::sleep(std::time::Duration::from_secs(2));
let err = Command::new(Path::new("./").join(SEAL_EXE))
.current_dir(path)
Expand All @@ -115,6 +121,7 @@ fn run_command(path: impl AsRef<Path>) {
}

println!("{}\n", "升级完毕,即将启动海豹核心…".black().on_yellow());
info!("准备运行海豹主程序,如果海豹没有启动,但下面没有出现报错信息,应该是 {} 的问题", SEAL_EXE);

std::thread::sleep(std::time::Duration::from_secs(2));
if let Err(err) = Command::new("cmd")
Expand Down

0 comments on commit 45157a0

Please sign in to comment.