Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@ use crate::{dns, tweak, utils};

use clap::{Args, Parser, Subcommand};

pub struct CLI;
pub struct ConsoleUi;

impl CLI {
impl ConsoleUi {
pub fn new() -> Self {
Self {}
}
}

impl UI for CLI {
impl UI for ConsoleUi {
fn show_message(&self, message_type: MessageType, message: &str, title: String) {
let type_str = match message_type {
MessageType::Info => "INFO",
MessageType::Warning => "WARNING",
MessageType::Error => "ERROR",
};
println!("[{type_str}] {title}: {message}");
println!("{}", format_message(message_type, message, &title));
}
}

fn format_message(message_type: MessageType, message: &str, title: &str) -> String {
let type_str = match message_type {
MessageType::Info => "INFO",
MessageType::Warning => "WARNING",
MessageType::Error => "ERROR",
};
format!("[{type_str}] {title}: {message}")
}

pub fn run_command(command: &str, escalate: bool) -> bool {
let status = utils::run_cmd(command.into(), escalate).expect("failed to run cmd");
status.success()
Expand Down Expand Up @@ -123,3 +127,25 @@ pub enum AppToLaunch {
/// Launch the `CachyOS` Kernel Manager
KernelManager,
}

#[cfg(test)]
mod test {
use super::*;
use crate::ui::MessageType;

#[test]
fn show_message_formats_correctly() {
assert_eq!(
format_message(MessageType::Info, "test message", "Title"),
"[INFO] Title: test message"
);
assert_eq!(
format_message(MessageType::Warning, "test message", "Title"),
"[WARNING] Title: test message"
);
assert_eq!(
format_message(MessageType::Error, "test message", "Title"),
"[ERROR] Title: test message"
);
}
}
4 changes: 2 additions & 2 deletions src/cli_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn handle_fix_command(action: FixAction) -> Result<()> {
}

while let Ok(msg) = rx.try_recv() {
let ui_comp = crate::cli::CLI::new();
let ui_comp = crate::cli::ConsoleUi::new();
ui_comp.show_message(msg.msg_type, &msg.msg, msg.msg_type.to_string());
}
Ok(())
Expand Down Expand Up @@ -254,7 +254,7 @@ pub fn handle_dns_command(action: DnsAction) -> Result<()> {
},
}
while let Ok(msg) = rx.try_recv() {
let ui_comp = crate::cli::CLI::new();
let ui_comp = crate::cli::ConsoleUi::new();
ui_comp.show_message(msg.msg_type, &msg.msg, msg.msg_type.to_string());
}
Ok(())
Expand Down
Loading