Skip to content

Commit ce33c3d

Browse files
committed
Optimized the log output.
1 parent bac922c commit ce33c3d

File tree

6 files changed

+72
-12
lines changed

6 files changed

+72
-12
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"cSpell.words": ["maplit", "popen"]
2+
"cSpell.words": ["maplit", "popen", "serde"]
33
}

Cargo.lock

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "n"
3-
version = "0.0.3"
3+
version = "0.0.7"
44
edition = "2021"
55

66
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@@ -11,3 +11,4 @@ maplit = "1.0.2"
1111
regex = "1.9.1"
1212
serde_json = "1.0.103"
1313
subprocess = "0.2.9"
14+
colored = "2.0.4"

src/main.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::utils::read_package_manager;
55
use clap::{command, Parser, Subcommand};
66

77
#[derive(Parser)]
8-
#[command(name= "n",author, version, about, long_about = None,disable_help_subcommand=true )]
8+
#[command(name= "n",author = "ityuany", version, about, long_about = None,disable_help_subcommand=true )]
99
struct Cli {
1010
#[command(subcommand)]
1111
commands: Commands,
@@ -44,11 +44,14 @@ enum Commands {
4444
}
4545

4646
fn main() {
47+
info!("Welcome to use 'n'");
48+
49+
info!("Parsing the packageManager in the package.json file.");
4750
let pkg_manager = read_package_manager();
4851
if let [package_manager, package_manager_version] = &pkg_manager[..2] {
49-
println!(
50-
"🥳 The current package manager being used is : '{}@{}' ",
51-
package_manager, package_manager_version
52+
info!(
53+
"The current package manager being used is: '{}@{}'",
54+
package_manager, package_manager_version,
5255
);
5356
let cli = Cli::parse();
5457
match cli.commands {

src/utils/macros.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#[macro_export]
2+
macro_rules! info {
3+
() => {
4+
$crate::print!("\n")
5+
};
6+
($($arg:tt)*) => {{
7+
let message = format!("🌴 {}", format_args!($($arg)*));
8+
print!("{}\n", message);
9+
}};
10+
}
11+
12+
#[macro_export]
13+
macro_rules! error {
14+
() => {
15+
$crate::print!("\n")
16+
};
17+
($($arg:tt)*) => {{
18+
let message = format!("👻 {}", format_args!($($arg)*));
19+
print!("{}\n", message.red());
20+
std::process::exit(1);
21+
}}
22+
}

src/utils/mod.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
#[macro_use]
2+
pub mod macros;
3+
14
use regex::Regex;
25
use serde_json::Value;
36
use std::fs;
47
use std::path::Path;
58
use subprocess::Exec;
9+
extern crate colored;
10+
use colored::*;
611

712
pub fn read_package_manager() -> Vec<String> {
813
let path = Path::new("./package.json");
@@ -15,18 +20,29 @@ pub fn read_package_manager() -> Vec<String> {
1520
if let Some(caps) = re.captures(manager) {
1621
return vec![caps[1].to_string(), caps[2].to_string()];
1722
} else {
18-
panic!("😢 PackageManager parsing failed, possibly due to incorrect format. ");
23+
error!("PackageManager parsing failed, possibly due to incorrect format. ");
1924
}
2025
}
2126
None => {
22-
panic!("😢 Sorry, you must to be configure packageManager in package.json file ");
27+
error!("Sorry, you must to be configure packageManager in package.json file ");
2328
}
2429
}
2530
}
26-
panic!("🔎 Could not found package.json");
31+
error!("Could not found package.json");
2732
}
2833

2934
pub fn run_shell(cmd: String) {
30-
println!("🎯 The instruction to be executed is : '{}' ", cmd);
31-
Exec::cmd("sh").arg("-c").arg(cmd).popen().unwrap();
35+
info!("The instruction to be executed is : '{}' ", cmd);
36+
let popen = Exec::cmd("sh")
37+
.arg("-c")
38+
.arg(cmd)
39+
// .stderr(Redirection::None)
40+
// .stdout(Redirection::None)
41+
.popen();
42+
match popen {
43+
Ok(_) => (),
44+
Err(err) => {
45+
error!("Failed to execute the command: '{}'.", err);
46+
}
47+
}
3248
}

0 commit comments

Comments
 (0)