Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better help page #94

Merged
merged 2 commits into from
Sep 2, 2024
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
25 changes: 21 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@ eula = false
better-panic = "0.3.0"
cfg-if = "1.0.0"
chrono = "0.4.38"
clap = { version = "4.5.16", features = ["derive", "cargo", "wrap_help", "unicode", "string", "unstable-styles", "color"] }
clap = { version = "4.5.16", features = [
"derive",
"cargo",
"wrap_help",
"unicode",
"string",
"unstable-styles",
"color",
] }
color-eyre = "0.6.3"
copypasta = "0.10.1"
crates_io_api = "0.11.0"
Expand Down Expand Up @@ -52,14 +60,18 @@ tracing = "0.1.40"
tracing-appender = "0.2.3"
tracing-error = "0.2.0"
tracing-log = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "serde", "serde_json"] }
tracing-subscriber = { version = "0.3.18", features = [
"env-filter",
"serde",
"serde_json",
] }
tui-input = "0.10.1"
unicode-width = "0.1.13"
uuid = "1.10.0"
webbrowser = "1.0.1"

[build-dependencies]
vergen = { version = "8.3.2", features = [ "build", "git", "git2", "cargo" ]}
vergen = { version = "8.3.2", features = ["build", "git", "git2", "cargo"] }

# The profile that 'cargo dist' will build with
[profile.dist]
Expand All @@ -75,6 +87,11 @@ ci = ["github"]
# The installers to generate for each app
installers = ["shell", "powershell"]
# Target platforms to build apps for (Rust target-triple syntax)
targets = ["aarch64-apple-darwin", "x86_64-apple-darwin", "x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc"]
targets = [
"aarch64-apple-darwin",
"x86_64-apple-darwin",
"x86_64-unknown-linux-gnu",
"x86_64-pc-windows-msvc",
]
# Publish jobs to run in CI
pr-run-mode = "plan"
51 changes: 39 additions & 12 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::path::PathBuf;

use clap::Parser;
use clap::{
builder::{styling::AnsiColor, Styles},
Parser,
};
use serde::Serialize;
use serde_with::{serde_as, skip_serializing_none, NoneAsEmptyString};
use tracing::level_filters::LevelFilter;
Expand All @@ -27,48 +30,72 @@ Authors: {author}"
)
}

const HELP_STYLES: Styles = Styles::styled()
.header(AnsiColor::Blue.on_default().bold())
.usage(AnsiColor::Blue.on_default().bold())
.literal(AnsiColor::White.on_default())
.placeholder(AnsiColor::Green.on_default());

/// Command line arguments.
///
/// Implements Serialize so that we can use it as a source for Figment
/// configuration.
#[serde_as]
#[skip_serializing_none]
#[derive(Debug, Default, Parser, Serialize)]
#[command(author, version = version(), about, long_about = None)]
#[command(author, version = version(), about, long_about = None, styles = HELP_STYLES)]
pub struct Cli {
/// Tick rate, i.e. number of ticks per second
#[arg(short, long, value_name = "FLOAT", default_value_t = 1.0)]
pub tick_rate: f64,

/// Print default configuration
#[arg(long)]
pub print_default_config: bool,

/// A path to a crates-tui configuration file.
#[arg(short, long, value_name = "FILE")]
#[arg(
short,
long,
value_name = "FILE",
default_value = get_default_config_path()
)]
pub config_file: Option<PathBuf>,

/// A path to a base16 color file.
#[arg(long, value_name = "FILE")]
#[arg(long, value_name = "FILE", default_value = get_default_color_file())]
pub color_file: Option<PathBuf>,

/// Frame rate, i.e. number of frames per second
#[arg(short, long, value_name = "FLOAT", default_value_t = 15.0)]
pub frame_rate: f64,

/// The directory to use for storing application data.
#[arg(long, value_name = "DIR")]
#[arg(long, value_name = "DIR", default_value = get_default_data_dir())]
pub data_dir: Option<PathBuf>,

/// The log level to use.
/// The log level to use. Valid values are: error, warn, info, debug, trace, off.
///
/// Valid values are: error, warn, info, debug, trace, off. The default is
/// info.
/// [default: info]
#[arg(long, value_name = "LEVEL", alias = "log")]
#[serde_as(as = "NoneAsEmptyString")]
pub log_level: Option<LevelFilter>,
}

fn get_default_config_path() -> String {
crate::config::default_config_file()
.to_string_lossy()
.into_owned()
}

fn get_default_color_file() -> String {
crate::config::default_color_file()
.to_string_lossy()
.into_owned()
}

fn get_default_data_dir() -> String {
crate::config::default_data_dir()
.to_string_lossy()
.into_owned()
}

pub fn parse() -> Cli {
Cli::parse()
}
12 changes: 6 additions & 6 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ impl Default for Base16Palette {
#[derive(Debug, Deserialize, Serialize)]
pub struct Config {
/// The directory to use for storing application data (logs etc.).
pub data_home: PathBuf,
pub data_dir: PathBuf,

/// The directory to use for storing application configuration (colors
/// etc.).
Expand Down Expand Up @@ -152,7 +152,7 @@ impl Default for Config {
let rose_pine = Base16Palette::default();

Self {
data_home: default_data_dir(),
data_dir: default_data_dir(),
config_home: default_config_dir(),
config_file: default_config_file(),
log_level: None,
Expand Down Expand Up @@ -192,7 +192,7 @@ pub fn init(cli: &Cli) -> Result<()> {
.extract::<Base16Palette>()?;
config.color = base16;
if let Some(data_dir) = cli.data_dir.clone() {
config.data_home = data_dir;
config.data_dir = data_dir;
}
CONFIG
.set(config)
Expand All @@ -211,12 +211,12 @@ pub fn get() -> &'static Config {
}

/// Returns the path to the default configuration file.
fn default_config_file() -> PathBuf {
pub fn default_config_file() -> PathBuf {
default_config_dir().join("config.toml")
}

/// Returns the path to the default configuration file.
fn default_color_file() -> PathBuf {
pub fn default_color_file() -> PathBuf {
default_config_dir().join("color.yaml")
}

Expand All @@ -229,7 +229,7 @@ fn default_config_dir() -> PathBuf {
}

/// Returns the directory to use for storing data files.
fn default_data_dir() -> PathBuf {
pub fn default_data_dir() -> PathBuf {
env::var("CRATES_TUI_DATA_HOME")
.map(PathBuf::from)
.or_else(|_| project_dirs().map(|dirs| dirs.data_local_dir().to_path_buf()))
Expand Down
2 changes: 1 addition & 1 deletion src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::config;

pub fn init() -> Result<()> {
let config = config::get();
let directory = config.data_home.clone();
let directory = config.data_dir.clone();
std::fs::create_dir_all(directory.clone())?;
let log_file = format!("{}.log", env!("CARGO_PKG_NAME"));
let log_path = directory.join(log_file);
Expand Down
Loading