feat: add logger and config structure
This commit is contained in:
1 parent
15abd84501
commit
d9c827b3ef
11 files changed
+906
-19
No files matched your search
+9
-9
@@ -1,5 +1,5 @@
|
||||
use clap::{Arg, ArgAction, Command};
|
||||
use crate::tui;
|
||||
use crate::{debug, tui};
|
||||
|
||||
pub fn init() {
|
||||
let matches = Command::new("mcl")
|
||||
@@ -104,24 +104,24 @@ pub fn init() {
|
||||
.unwrap_or_else(|| "None".to_string());
|
||||
|
||||
if launch_matches.get_flag("offline") {
|
||||
println!("Launching profile '{}' in offline mode...", profile);
|
||||
debug!("Launching profile '{}' in offline mode...", profile);
|
||||
} else {
|
||||
println!("Launching profile '{}' in online mode...", profile);
|
||||
debug!("Launching profile '{}' in online mode...", profile);
|
||||
}
|
||||
|
||||
println!("Memory: {}", memory);
|
||||
println!("Resolution: {}", resolution);
|
||||
println!("JVM Args: {}", jvm_args);
|
||||
debug!("Memory: {}", memory);
|
||||
debug!("Resolution: {}", resolution);
|
||||
debug!("JVM Args: {}", jvm_args);
|
||||
|
||||
if launch_matches.get_flag("no-window") {
|
||||
println!("Running in headless mode...");
|
||||
debug!("Running in headless mode...");
|
||||
}
|
||||
}
|
||||
Some(("profiles", profiles_matches)) => {
|
||||
if profiles_matches.get_flag("list") {
|
||||
println!("Listing all profiles...");
|
||||
debug!("Listing all profiles...");
|
||||
} else if let Some(profile) = profiles_matches.get_one::<String>("delete") {
|
||||
println!("Deleting profile '{}'...", profile);
|
||||
debug!("Deleting profile '{}'...", profile);
|
||||
}
|
||||
}
|
||||
_ => {},
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
use config::{Config as ConfigLoader, ConfigError, File};
|
||||
use dirs_next::config_dir;
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
use obj::Config;
|
||||
|
||||
pub mod obj;
|
||||
|
||||
fn get_config_path() -> PathBuf {
|
||||
let base_dir = config_dir().unwrap();
|
||||
base_dir.join("mcl/config.toml")
|
||||
}
|
||||
|
||||
pub fn ensure_config_exists(default_path: &str) -> PathBuf {
|
||||
let config_path = get_config_path();
|
||||
|
||||
if !config_path.exists() {
|
||||
if let Some(parent_dir) = config_path.parent() {
|
||||
fs::create_dir_all(parent_dir).expect("Failed to create configuration directory");
|
||||
}
|
||||
|
||||
fs::copy(default_path, &config_path).expect("Failed to copy default configuration file");
|
||||
println!("Default configuration copied to '{}'", config_path.display());
|
||||
}
|
||||
|
||||
config_path
|
||||
}
|
||||
|
||||
pub fn load_config(config_path: &PathBuf) -> Result<Config, ConfigError> {
|
||||
ConfigLoader::builder()
|
||||
.add_source(File::from(config_path.clone()))
|
||||
.build()?
|
||||
.try_deserialize()
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Variables {
|
||||
pub debug: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Paths {
|
||||
pub log_dir: String,
|
||||
pub config_dir: String,
|
||||
pub save_dir: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Colors {
|
||||
pub background: String,
|
||||
pub foreground: String,
|
||||
pub highlight: String,
|
||||
pub focused: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Config {
|
||||
pub variables: Variables,
|
||||
pub paths: Paths,
|
||||
pub colors: Colors,
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
use std::sync::Mutex;
|
||||
use chrono::Local;
|
||||
use color_eyre::owo_colors::OwoColorize;
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
pub struct Logger {
|
||||
debug: bool,
|
||||
}
|
||||
|
||||
impl Logger {
|
||||
pub fn init(debug: bool) {
|
||||
let mut logger = LOGGER.lock().unwrap();
|
||||
logger.debug = debug;
|
||||
}
|
||||
|
||||
fn timestamp() -> String {
|
||||
Local::now().format("%Y-%m-%d %H:%M:%S").to_string()
|
||||
}
|
||||
|
||||
pub fn debug(&self, message: &str) {
|
||||
if self.debug {
|
||||
println!(
|
||||
"{} {} {}",
|
||||
Self::timestamp().dimmed(),
|
||||
"[DEBUG]".cyan(),
|
||||
message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn info(&self, message: &str) {
|
||||
println!(
|
||||
"{} {} {}",
|
||||
Self::timestamp().dimmed(),
|
||||
"[INFO]".green(),
|
||||
message
|
||||
);
|
||||
}
|
||||
|
||||
pub fn error(&self, message: &str) {
|
||||
eprintln!(
|
||||
"{} {} {}",
|
||||
Self::timestamp().dimmed(),
|
||||
"[ERROR]".red().bold(),
|
||||
message
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref LOGGER: Mutex<Logger> = Mutex::new(Logger { debug: false });
|
||||
}
|
||||
|
||||
pub fn get_logger() -> std::sync::MutexGuard<'static, Logger> {
|
||||
LOGGER.lock().unwrap()
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#[macro_export]
|
||||
macro_rules! debug {
|
||||
($msg:expr) => {
|
||||
$crate::logger::get_logger().debug($msg);
|
||||
};
|
||||
($fmt:expr, $($arg:tt)*) => {
|
||||
$crate::logger::get_logger().debug(&format!($fmt, $($arg)*));
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! info {
|
||||
($msg:expr) => {
|
||||
$crate::logger::get_logger().info($msg);
|
||||
};
|
||||
($fmt:expr, $($arg:tt)*) => {
|
||||
$crate::logger::get_logger().info(&format!($fmt, $($arg)*));
|
||||
};
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! error {
|
||||
($msg:expr) => {
|
||||
$crate::logger::get_logger().error($msg);
|
||||
};
|
||||
($fmt:expr, $($arg:tt)*) => {
|
||||
$crate::logger::get_logger().error(&format!($fmt, $($arg)*));
|
||||
};
|
||||
}
|
||||
+21
-1
@@ -1,7 +1,27 @@
|
||||
use std::path::PathBuf;
|
||||
use crate::config::{ensure_config_exists, load_config};
|
||||
use logger::Logger;
|
||||
|
||||
mod cli;
|
||||
pub mod logger;
|
||||
pub mod macros;
|
||||
pub mod tui;
|
||||
pub mod config;
|
||||
|
||||
|
||||
fn main() {
|
||||
cli::init()
|
||||
let default = "assets/default.toml";
|
||||
let path: PathBuf = ensure_config_exists(default);
|
||||
|
||||
match load_config(&path) {
|
||||
Ok(config) => {
|
||||
Logger::init(config.variables.debug);
|
||||
cli::init()
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to load config file: {}", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+2
-2
@@ -5,7 +5,7 @@ use ratatui::{
|
||||
layout::{Constraint, Direction, Layout},
|
||||
Frame,
|
||||
};
|
||||
use crate::tui::widgets;
|
||||
use super::widgets;
|
||||
use super::Tui;
|
||||
|
||||
|
||||
@@ -66,7 +66,7 @@ impl App {
|
||||
widgets::render_status(frame, bottom_chunks[2]);
|
||||
}
|
||||
|
||||
/// updates the application's state based on user input
|
||||
/// updates the applications state based on user input
|
||||
fn handle_events(&mut self) -> color_eyre::Result<()> {
|
||||
match event::read()? {
|
||||
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => self
|
||||
|
||||
+7
-6
@@ -3,39 +3,40 @@ use ratatui::{
|
||||
widgets::{Block, Borders, Paragraph},
|
||||
Frame,
|
||||
};
|
||||
use ratatui::widgets::BorderType;
|
||||
|
||||
pub fn render_instances(frame: &mut Frame, area: Rect) {
|
||||
let sidebar = Block::default()
|
||||
.title(" Instances ")
|
||||
.borders(Borders::ALL);
|
||||
.borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded);
|
||||
frame.render_widget(sidebar, area);
|
||||
}
|
||||
|
||||
pub fn render_title(frame: &mut Frame, area: Rect) {
|
||||
let top_bar = Paragraph::new(" TEST INSTANCE NAME").block(Block::default().borders(Borders::ALL)).centered();
|
||||
let top_bar = Paragraph::new(" TEST INSTANCE NAME").block(Block::default().borders(Borders::ALL).border_type(BorderType::Rounded)).centered();
|
||||
frame.render_widget(top_bar, area);
|
||||
}
|
||||
|
||||
pub fn render_content(frame: &mut Frame, area: Rect) {
|
||||
let file_list = Paragraph::new(" TEST CONTENT LIBRARY PAGE").block(Block::default().borders(Borders::ALL));
|
||||
let file_list = Paragraph::new(" TEST CONTENT LIBRARY PAGE").block(Block::default().borders(Borders::ALL).border_type(BorderType::Rounded));
|
||||
frame.render_widget(file_list, area);
|
||||
}
|
||||
|
||||
pub fn render_account(frame: &mut Frame, area: Rect) {
|
||||
let metadata = Paragraph::new(" ")
|
||||
.block(Block::default().borders(Borders::ALL).title(" Account "));
|
||||
.block(Block::default().borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded).title(" Account "));
|
||||
frame.render_widget(metadata, area);
|
||||
}
|
||||
|
||||
pub fn render_info(frame: &mut Frame, area: Rect) {
|
||||
let clipboard = Paragraph::new(" ")
|
||||
.block(Block::default().borders(Borders::ALL).title(" Info "));
|
||||
.block(Block::default().borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded).title(" Info "));
|
||||
frame.render_widget(clipboard, area);
|
||||
}
|
||||
|
||||
pub fn render_status(frame: &mut Frame, area: Rect) {
|
||||
let processes = Paragraph::new(" ")
|
||||
.block(Block::default().borders(Borders::ALL).title(" Status "));
|
||||
.block(Block::default().borders(Borders::ALL).borders(Borders::ALL).border_type(BorderType::Rounded).title(" Status "));
|
||||
frame.render_widget(processes, area);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user