From 5b0ac5d793704aff2f1540a78a1a6634e2a72a5a Mon Sep 17 00:00:00 2001 From: Alex Zepeda Date: Thu, 31 Mar 2022 16:22:50 -0700 Subject: [PATCH] Fix default configuration settings. - The current config file layout cannot be serialized to TOML with serde, so switch to YAML. This necessitates updating to an unreleased version of confy. See: #85, #77, alexcrichton/toml-rs#145, and alexcrichton/toml-rs#407. - Print out the location of the default config file upon creation fixes: #73 and #78. - Set the default left prompt to *something* so the user isn't left scratching their head. - Change the default icon set to Unicode so that this is usable out of the box with as few dependencies as possible. --- Cargo.lock | 162 ++++++++++++++++++++++++++++++++++++++++---------- Cargo.toml | 2 +- readme.md | 2 +- src/config.rs | 44 +++++++++++++- src/main.rs | 9 ++- 5 files changed, 178 insertions(+), 41 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9cccc69..7e6cc87 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -90,6 +90,12 @@ version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "chrono" version = "0.4.13" @@ -136,12 +142,12 @@ dependencies = [ [[package]] name = "confy" version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2913470204e9e8498a0f31f17f90a0de801ae92c8c5ac18c49af4819e6786697" +source = "git+https://github.com/rust-cli/confy#642822413139ce38a96b916190e8c7fd5b88e814" dependencies = [ - "directories", + "directories-next", "serde", - "toml", + "serde_yaml", + "thiserror", ] [[package]] @@ -168,7 +174,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 0.1.10", "crossbeam-utils", "lazy_static", "maybe-uninit", @@ -182,7 +188,7 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "crossbeam-utils", "maybe-uninit", ] @@ -194,18 +200,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" dependencies = [ "autocfg", - "cfg-if", + "cfg-if 0.1.10", "lazy_static", ] [[package]] -name = "directories" -version = "2.0.2" +name = "directories-next" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "551a778172a450d7fc12e629ca3b0428d00f6afa9a43da1b630d54604e97371c" +checksum = "339ee130d97a610ea5a5872d2bbb130fdf68884ff09d3028b81bec8a1ac23bbc" dependencies = [ - "cfg-if", - "dirs-sys", + "cfg-if 1.0.0", + "dirs-sys-next", ] [[package]] @@ -214,7 +220,7 @@ version = "2.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "dirs-sys", ] @@ -225,7 +231,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e93d7f5705de3e49895a2b5e0b8855a1c27f080192ae9c32a6432d50741a57a" dependencies = [ "libc", - "redox_users", + "redox_users 0.3.4", + "winapi", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users 0.4.3", "winapi", ] @@ -256,9 +273,20 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7abc8dd8451921606d809ba32e95b6111925cd2906060d2dcc29c070220503eb" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "libc", - "wasi", + "wasi 0.9.0+wasi-snapshot-preview1", +] + +[[package]] +name = "getrandom" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d39cd93900197114fa1fcb7ae84ca742095eed9442088988ae74fa744e930e77" +dependencies = [ + "cfg-if 1.0.0", + "libc", + "wasi 0.10.2+wasi-snapshot-preview1", ] [[package]] @@ -419,13 +447,19 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "linked-hash-map" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fb9b38af92608140b86b693604b9ffcc5824240a484d1ecd4795bacb2fe88f3" + [[package]] name = "log" version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4fabed175da42fed1fa0746b0ea71f412aa9d35e76e95e59b192c64b9dc2bf8b" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", ] [[package]] @@ -606,7 +640,7 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" dependencies = [ - "getrandom", + "getrandom 0.1.14", "libc", "rand_chacha", "rand_core", @@ -629,7 +663,7 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" dependencies = [ - "getrandom", + "getrandom 0.1.14", ] [[package]] @@ -672,17 +706,37 @@ version = "0.1.57" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce" +[[package]] +name = "redox_syscall" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62f25bc4c7e55e0b0b7a1d43fb893f4fa1361d0abe38b9ce4f323c2adfe6ef42" +dependencies = [ + "bitflags", +] + [[package]] name = "redox_users" version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "09b23093265f8d200fa7b4c2c76297f47e681c655f6f1285a8780d6a022f7431" dependencies = [ - "getrandom", - "redox_syscall", + "getrandom 0.1.14", + "redox_syscall 0.1.57", "rust-argon2", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom 0.2.5", + "redox_syscall 0.2.13", + "thiserror", +] + [[package]] name = "regex" version = "1.3.9" @@ -719,10 +773,16 @@ version = "0.15.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a3679dd538c876a7b606f3bb951c8a20fc281a0ff7795f59f7cb490e3f979e1" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "ordered-multimap", ] +[[package]] +name = "ryu" +version = "1.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73b4b750c782965c211b42f022f59af1fbceabdd026623714f104152f1ec149f" + [[package]] name = "scopeguard" version = "1.1.0" @@ -749,6 +809,18 @@ dependencies = [ "syn", ] +[[package]] +name = "serde_yaml" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4a521f2940385c165a24ee286aa8599633d162077a54bdcae2a6fd5a7bfa7a0" +dependencies = [ + "indexmap", + "ryu", + "serde", + "yaml-rust", +] + [[package]] name = "shellexpand" version = "2.0.0" @@ -808,7 +880,7 @@ version = "0.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f597c00e6548b9ddd54c11b8e8534fc11223f28509d57e021f01797b50ca5ca1" dependencies = [ - "cfg-if", + "cfg-if 0.1.10", "doc-comment", "libc", "ntapi", @@ -835,6 +907,26 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "thiserror" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "854babe52e4df1653706b98fcfc05843010039b406875930a70e4d9644e5c417" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "thread_local" version = "1.0.1" @@ -860,15 +952,6 @@ version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "53953d2d3a5ad81d9f844a32f14ebb121f50b650cd59d0ee2a07cf13c617efed" -[[package]] -name = "toml" -version = "0.5.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a" -dependencies = [ - "serde", -] - [[package]] name = "unicode-bidi" version = "0.3.4" @@ -950,6 +1033,12 @@ version = "0.9.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" + [[package]] name = "winapi" version = "0.3.9" @@ -980,3 +1069,12 @@ name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] diff --git a/Cargo.toml b/Cargo.toml index dcffc20..9ef6264 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ serde = { version = "1.0", features = ["derive"] } chrono = "0.4" clap = "=3.0.0-beta.2" clap_derive = "=3.0.0-beta.2" -confy = "0.4" +confy = { git = "https://github.com/rust-cli/confy", features = [ "yaml_conf" ], default-features = false } dirs = "2" git2 = "0.13" hostname = "0.3" diff --git a/readme.md b/readme.md index 2272110..0a1dbca 100644 --- a/readme.md +++ b/readme.md @@ -19,7 +19,7 @@ Since silver is not written in shell script, it should theoretically be compatib ### Icons -To be able to use the custom icons (which are enabled by default), you must patch your font or install a pre-patched font from [Nerd Fonts](https://github.com/ryanoasis/nerd-fonts). +To be able to use the custom icons (which are disabled by default), you must patch your font or install a pre-patched font from [Nerd Fonts](https://github.com/ryanoasis/nerd-fonts). ### Installation diff --git a/src/config.rs b/src/config.rs index 6a59c39..d32c51c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -3,7 +3,7 @@ use std::{collections::HashMap, fmt, time::Duration}; type Prompt = Vec; -#[derive(Debug, Serialize, Deserialize, Default)] +#[derive(Debug, Serialize, Deserialize)] pub struct Config { #[serde(default)] pub separator: Separators, @@ -82,6 +82,46 @@ pub struct Git { pub ignore_dirs: Vec, } +fn default_left_prompt() -> Prompt { + vec![ + Segment { + name: "dir".into(), + color: Colors { + background: Color::Name("7".into()), + foreground: Color::Name("0".into()), + }, + args: vec![], + } + ] +} + +// Implement defaults that will render a +// usable prompt. +impl std::default::Default for Config { + fn default() -> Self { + let config_dir = confy::get_configuration_file_path("silver", None) + .expect("Failed to get configuration path") + .into_os_string() + .into_string(); + + match config_dir { + Ok(config_dir) => eprintln!("Creating default configuration at: {}", config_dir), + Err(_) => eprintln!("Couldn't format the path to the configuration file. Oops"), + } + + Self { + left: default_left_prompt(), + separator: Separators::default(), + right: Prompt::default(), + icons: HashMap::default(), + icon_set: IconSet::default(), + cmdtime_threshold: Duration::default(), + dir: Dir::default(), + git: Git::default(), + } + } +} + impl Default for Separators { fn default() -> Self { Self { @@ -105,7 +145,7 @@ impl Default for Color { impl Default for IconSet { fn default() -> Self { - Self::Nerd + Self::Unicode } } diff --git a/src/main.rs b/src/main.rs index 9b5d68c..3eb549a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,12 +13,11 @@ use sysinfo::{get_current_pid, ProcessExt, System, SystemExt}; static CONFIG_PATH: OnceCell = OnceCell::new(); static CONFIG: Lazy = Lazy::new(|| { - if let Some(path) = CONFIG_PATH.get() { - confy::load_path(path) - } else { - confy::load("silver") + match CONFIG_PATH.get() { + Some(path) => confy::load_path(path), + None => confy::load("silver", None), } - .expect("Failed to read config") + .expect("Problem loading or serializing default config") }); #[derive(Clone, Debug)]