Browse Source

Automatically find the config in XDG_CONFIG or whatever

Getty Ritter 4 years ago
parent
commit
fa25f23a38
4 changed files with 17 additions and 1 deletions
  1. 7 0
      Cargo.lock
  2. 1 0
      Cargo.toml
  3. 8 0
      src/config.rs
  4. 1 1
      src/main.rs

+ 7 - 0
Cargo.lock

@@ -141,6 +141,7 @@ dependencies = [
  "pangocairo 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "toml 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
@@ -328,6 +329,11 @@ dependencies = [
  "pkg-config 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "xdg"
+version = "2.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
 [metadata]
 "checksum autocfg 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a6d640bee2da49f60a4068a7fae53acde8982514ab7bae8b8cea9e88cbcfd799"
 "checksum backtrace 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "cd5a90e2b463010cd0e0ce9a11d4a9d5d58d9f41d4a6ba3dcaf9e68b466e88b4"
@@ -366,3 +372,4 @@ dependencies = [
 "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
 "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
 "checksum x11 2.18.1 (registry+https://github.com/rust-lang/crates.io-index)" = "39697e3123f715483d311b5826e254b6f3cfebdd83cf7ef3358f579c3d68e235"
+"checksum xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57"

+ 1 - 0
Cargo.toml

@@ -12,6 +12,7 @@ chrono = "*"
 libc = "0.2"
 failure = "*"
 toml = "0.5"
+xdg = "*"
 
 [dependencies.cairo-sys-rs]
 version = "0.8"

+ 8 - 0
src/config.rs

@@ -21,6 +21,7 @@ impl Config {
                 _ => (),
             }
         }
+        conf.right.reverse();
         Ok(conf)
     }
 
@@ -30,6 +31,13 @@ impl Config {
         Config::from_toml(val)
     }
 
+    pub fn find_config() -> Result<Config, failure::Error> {
+        if let Some(p) = xdg::BaseDirectories::new()?.find_config_file("knurling/knurling.toml") {
+            return Config::from_file(p);
+        }
+        Err(format_err!("Unable to find `knurling.toml`"))
+    }
+
     pub fn draw(&self, ctx: &cairo::Context, layout: &pango::Layout, stdin: &str, size: w::Size) -> Result<(), failure::Error>{
         // the background is... gray-ish? this'll be configurable eventually
         ctx.set_source_rgb(0.1, 0.1, 0.1);

+ 1 - 1
src/main.rs

@@ -13,7 +13,7 @@ use window::{Display,Event,Window};
 
 fn main() -> Result<(), failure::Error> {
     // set up the display and the window
-    let config = config::Config::from_file("sample.toml")?;
+    let config = config::Config::find_config()?;
     let mut d = Display::create()?;
     let mut ws = Vec::new();
     for (x_off, wd) in d.get_widths()? {