From 45157a0aa2f7883617b2d9e28bcd0a6dfb44b8bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=AA=80=E8=BD=B6=E6=AD=A5=E6=A3=8B?= <57583509+Verplitic@users.noreply.github.com> Date: Tue, 14 Nov 2023 23:48:24 +0800 Subject: [PATCH] Add docs; `--no-log` flag --- README.md | 1 + src/cli.rs | 3 +++ src/colorize.rs | 44 +++++++++++++++++++++++++++----------------- src/logger.rs | 28 ++++++++++++++++++++++------ src/main.rs | 13 ++++++++++--- 5 files changed, 63 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 5b953b5..e04eead 100644 --- a/README.md +++ b/README.md @@ -9,3 +9,4 @@ - `--cwd` 工作路径,即解压到该路径下。默认为当前路径 - `--verbose` 打印更多信息 - `--skip-startup` 解压后,不启动海豹 +- `--no-log` 不创建更新日志 diff --git a/src/cli.rs b/src/cli.rs index 7ccf73a..b34917d 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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 } diff --git a/src/colorize.rs b/src/colorize.rs index 2c8ab05..0866677 100644 --- a/src/colorize.rs +++ b/src/colorize.rs @@ -1,13 +1,19 @@ 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 = 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, } 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(), @@ -15,10 +21,12 @@ impl ColoredString { } } - 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) } @@ -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 @@ -212,7 +222,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(30); + self.push(30); self } @@ -220,7 +230,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(31); + self.push(31); self } @@ -228,7 +238,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(32); + self.push(32); self } @@ -236,7 +246,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(33); + self.push(33); self } @@ -244,7 +254,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(34); + self.push(34); self } @@ -252,7 +262,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(35); + self.push(35); self } @@ -260,7 +270,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(36); + self.push(36); self } @@ -268,7 +278,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(37); + self.push(37); self } @@ -276,7 +286,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(40); + self.push(40); self } @@ -284,7 +294,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(41); + self.push(41); self } @@ -292,7 +302,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(42); + self.push(42); self } @@ -300,7 +310,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(43); + self.push(43); self } @@ -308,7 +318,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(44); + self.push(44); self } @@ -316,7 +326,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(45); + self.push(45); self } @@ -324,7 +334,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(46); + self.push(46); self } @@ -332,7 +342,7 @@ impl Colorize for ColoredString { where Self: Sized, { - self.push_color(47); + self.push(47); self } } diff --git a/src/logger.rs b/src/logger.rs index 9224902..82d2171 100644 --- a/src/logger.rs +++ b/src/logger.rs @@ -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 { - 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!( "{} [{}] {}:{} {}", @@ -13,9 +22,16 @@ pub fn init_logger() -> Result { 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) } diff --git a/src/main.rs b/src/main.rs index 8c47fe6..147f541 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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()), } @@ -61,13 +65,14 @@ fn run_command(path: impl AsRef) { 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(); @@ -97,7 +102,8 @@ fn run_command(path: impl AsRef) { } 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) @@ -115,6 +121,7 @@ fn run_command(path: impl AsRef) { } println!("{}\n", "升级完毕,即将启动海豹核心…".black().on_yellow()); + info!("准备运行海豹主程序,如果海豹没有启动,但下面没有出现报错信息,应该是 {} 的问题", SEAL_EXE); std::thread::sleep(std::time::Duration::from_secs(2)); if let Err(err) = Command::new("cmd")