Add bytesize and chrono, improve console output formatting

- Added `bytesize` and `chrono` dependencies in `Cargo.toml` and `Cargo.lock` for enhanced size and time formatting.
- Enhanced console output in `list.rs`, `index.rs`, `clean.rs`, and `config.rs` with improved styles for readability.
- Updated file size and modified time formatting across commands using `ByteSize` and `chrono`.
This commit is contained in:
elipeter 2025-06-23 19:37:19 +02:00
parent 47d850843c
commit 17d327a572
6 changed files with 196 additions and 45 deletions

View file

@ -1,4 +1,5 @@
use std::{env, fs};
use console::style;
use crate::utils::get_project_info;
pub fn handle(
@ -7,19 +8,19 @@ pub fn handle(
config_dir: &std::path::Path,
) -> Result<(), Box<dyn std::error::Error>> {
if all {
println!("Cleaning all indexes...");
println!("{}", style("Cleaning all indexes...").cyan().bold());
if config_dir.exists() {
fs::remove_dir_all(config_dir)?;
fs::create_dir_all(config_dir)?;
}
println!("All indexes cleaned.");
println!("{}", style("✔ All indexes cleaned").green().bold());
} else if let Some(proj_name) = project {
let db_path = config_dir.join(format!("{}.sqlite", proj_name));
if db_path.exists() {
fs::remove_file(&db_path)?;
println!("Cleaned index for: {}", proj_name);
println!("{} {}", style("✔ Cleaned index for").green(), style(&proj_name).white().bold());
} else {
println!("No index found for: {}", proj_name);
println!("{} {}", style("✖ No index found for").red(), style(&proj_name).white().bold());
}
} else {
let current_dir = env::current_dir()?;
@ -27,11 +28,11 @@ pub fn handle(
if db_path.exists() {
fs::remove_file(&db_path)?;
println!("Cleaned index for: {}", project_name);
println!("{} {}", style("✔ Cleaned index for").green(), style(&project_name).white().bold());
} else {
println!("No index found for current project: {}", project_name);
println!("{} {}", style("✖ No index found for current project").red(), style(&project_name).white().bold());
}
}
Ok(())
std::process::exit(0);
}

View file

@ -1,4 +1,8 @@
use std::fs;
use std::process::exit;
use bytesize::ByteSize;
use chrono::{DateTime, Local};
use console::style;
use crate::cli::IndexAction;
use crate::database::index::{Indexer, IssueRow};
use crate::patterns::Severity;
@ -19,24 +23,29 @@ pub fn handle(
if force || !db_path.exists() {
build_index(&project_name, &build_path, &db_path, config)?;
println!("Index built: {}", db_path.display());
println!("{} {} {}", "", style("Index built:" ).green(), style(db_path.display()).white().bold());
} else {
println!("Index already exists. Use --force to rebuild.");
println!("{} {}", style("↩ Index already exists").yellow(), style("(use --force to rebuild)").dim());
}
}
IndexAction::Status { path } => {
let status_path = std::path::Path::new(&path).canonicalize()?;
let (project_name, db_path) = get_project_info(&status_path, database_dir)?;
println!("Project: {}", project_name);
println!("Index path: {}", db_path.display());
println!("Index exists: {}", db_path.exists());
println!("{}", style("Project status").blue().bold().underlined());
println!(" {:14} {}", style("Project"), style(&project_name).white().bold());
println!(" {:14} {}", style("Index path"), style(db_path.display()).underlined());
println!(" {:14} {}", style("Exists"), style(db_path.exists()).bold());
if db_path.exists() {
let metadata = fs::metadata(&db_path)?;
println!("Index size: {} bytes", metadata.len());
println!("Last modified: {:?}", metadata.modified()?);
let meta = fs::metadata(&db_path)?;
let size = ByteSize::b(meta.len());
let mtime: DateTime<Local> = meta.modified()?.into();
println!(" {:14} {}", style("Size"), size);
println!(" {:14} {}", style("Modified"), mtime.format("%Y-%m-%d %H:%M:%S"));
}
exit(0);
}
}
Ok(())

View file

@ -1,35 +1,37 @@
use std::fs;
use bytesize::ByteSize;
use chrono::{DateTime, Local};
use console::style;
pub fn handle(
verbose: bool,
database_dir: &std::path::Path,
) -> Result<(), Box<dyn std::error::Error>> {
println!("Indexed projects:");
println!("{}", style("Indexed projects").blue().bold().underlined());
if database_dir.exists() {
for entry in fs::read_dir(database_dir)? {
let entry = entry?;
let path = entry.path();
if path.extension().and_then(|s| s.to_str()) == Some("sqlite") {
let project_name = path
.file_stem()
.and_then(|s| s.to_str())
.unwrap_or("unknown");
println!(" {}", project_name);
if verbose {
let metadata = fs::metadata(&path)?;
println!(" Path: {}", path.display());
println!(" Size: {} bytes", metadata.len());
println!(" Modified: {:?}", metadata.modified()?);
}
}
}
} else {
println!(" No indexed projects found.");
if !database_dir.exists() {
println!(" {}", style("∅ No indexed projects found").dim());
std::process::exit(0);
}
Ok(())
for entry in fs::read_dir(database_dir)? {
let path = entry?.path();
if path.extension().and_then(|s| s.to_str()) != Some("sqlite") {
continue;
}
let name = path.file_stem().and_then(|s| s.to_str()).unwrap_or("unknown");
println!(" {}", style(name).white().bold());
if verbose {
let meta = fs::metadata(&path)?;
let size = ByteSize::b(meta.len());
let mtime: DateTime<Local> = meta.modified()?.into();
println!(" {:10} {}", style("Path"), style(path.display()).underlined());
println!(" {:10} {}", style("Size"), size);
println!(" {:10} {}", style("Modified"), mtime.format("%Y-%m-%d %H:%M:%S"));
}
}
std::process::exit(0);
}

View file

@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};
use std::path::{Path};
use std::fs;
use console::style;
use toml;
use crate::patterns::Severity;
@ -187,10 +188,15 @@ impl Config {
let user_config: Config = toml::from_str(&user_config_content)?;
config = merge_configs(config, user_config);
println!("Loaded user config from: {}", user_config_path.display());
println!("{}: Loaded user config from: {}\n",
style("note").green().bold(),
style(user_config_path.display()).underlined().white().bold());
} else {
println!("Using default configuration. Create {} to customize.", user_config_path.display());
println!("{}: Using {} configuration.\n Create file in '{}'to customize.\n",
style("note").green().bold(),
style("default").bold(),
style(user_config_path.display()).underlined().white().bold());
}
Ok(config)