6 releases
0.1.6 | Apr 4, 2024 |
---|---|
0.1.5 | Apr 3, 2024 |
0.1.2 | Mar 12, 2024 |
#305 in Configuration
31 downloads per month
Used in wallpaper-dl
14KB
202 lines
apputils
A lightweight Rust crate to help you build awesome tools
Check out the docs to know what it can do. To add it to your dependencies, either run:
cargo add apputils
Or update your Cargo.toml
:
[dependencies]
apputils = "0.1.5"
Categories
dirs
: User directories using environment variablesconfig
: Config file helpers
An example
use apputils::config::local_file;
use apputils::Colors;
use apputils::paintln;
fn main() {
paintln!(Colors::Rgb(42, 164, 69), "Attempting to read alacritty config file...");
match local_file("alacritty", "alacritty.toml") {
Ok(data) => println!("Your alacritty config:\n{}", data),
Err(_) => paintln!(Colors::Red, "You don't seem to have an alacritty config!")
}
// You can also print with bold colors
paintln!(Colors::MagentaBold, "I use Gentoo, btw.");
}
lib.rs
:
A lightweight Rust crate to help you build awesome tools
It's designed to be framework-less and relatively simple while providing awesome helper functions for basic tasks that almost any program needs to do. These tasks include reading a config file with multiple paths (user and global), printing with color similar to println!()
and getting user directories cross-platform.
To add it to your dependencies, either run:
cargo add apputils
Or update your Cargo.toml
:
[dependencies]
apputils = "0.1.0"
Printing with color
use apputils::Colors;
use apputils::paintln;
paintln!(Colors::White, "I'm white.");
paintln!(Colors::Black, "I'm black.");
paintln!(Colors::Yellow, "I'm yellow.");
paintln!(Colors::Red, "I'm red.");
paintln!(Colors::Rgb(35, 170, 242), "I'm #23AAF2.");
Reading config and data files
An example with wallpaper-dl's file structure:
use apputils::config::{Cfg, Appdata};
use apputils::dirs::{config_home, data_home};
const APP_NAME: &str = "wallpaper-dl";
const CONFIG_FILE: &str = "config.toml";
const DB_FILE: &str = "wallpapers.toml";
let cfg_path = Cfg::path(APP_NAME, CONFIG_FILE);
let db_path = Appdata::path(APP_NAME, DB_FILE);
assert_eq!(cfg_path, config_home().unwrap().join(APP_NAME).join(CONFIG_FILE)); // e.g. ~/.config/wallpaper-dl/config.toml
assert_eq!(db_path, data_home().unwrap().join(APP_NAME).join(DB_FILE)); // e.g. ~/.local/share/wallpaper-dl/wallpapers.toml