summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRomain Porte <microjoe@microjoe.org>2019-02-04 17:41:49 +0100
committerRomain Porte <microjoe@microjoe.org>2019-02-04 17:42:46 +0100
commitf990f049e84a6692bf0814d2e74572aca2dfee5d (patch)
tree50b26cdd37f4a0faefce3961329348a4da4d1632
parent45d401968b35c6aaa89fa326c7088dcbcee52b82 (diff)
downloadsuperbar-master.tar.gz
superbar-master.zip
add configuration file systemHEADmaster
-rw-r--r--Cargo.lock17
-rw-r--r--Cargo.toml2
-rw-r--r--src/bar.rs18
-rw-r--r--src/config.rs71
-rw-r--r--src/item_brightness.rs9
-rw-r--r--src/item_update.rs22
-rw-r--r--src/main.rs46
7 files changed, 141 insertions, 44 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 9d73daa..b55f18e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -380,6 +380,8 @@ dependencies = [
"serde 1.0.83 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.83 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.33 (registry+https://github.com/rust-lang/crates.io-index)",
+ "toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)",
+ "xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@@ -440,6 +442,14 @@ dependencies = [
]
[[package]]
+name = "toml"
+version = "0.4.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "serde 1.0.83 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
name = "ucd-util"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -495,6 +505,11 @@ dependencies = [
"winapi-util 0.1.1 (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 aho-corasick 0.6.9 (registry+https://github.com/rust-lang/crates.io-index)" = "1e9a933f4e58658d7b12defcf96dc5c720f20832deebe3e0a19efd3b6aaeeb9e"
"checksum argon2rs 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "3f67b0b6a86dae6e67ff4ca2b6201396074996379fba2b92ff649126f37cb392"
@@ -550,6 +565,7 @@ dependencies = [
"checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096"
"checksum thread_local 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c6b53e329000edc2b34dbe8545fd20e55a333362d0a321909685a19bd28c3f1b"
"checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f"
+"checksum toml 0.4.10 (registry+https://github.com/rust-lang/crates.io-index)" = "758664fc71a3a69038656bee8b6be6477d2a6c315a6b81f7081f591bffa4111f"
"checksum ucd-util 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "535c204ee4d8434478593480b8f86ab45ec9aae0e83c568ca81abf0fd0e88f86"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737"
@@ -559,3 +575,4 @@ dependencies = [
"checksum winapi-util 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "afc5508759c5bf4285e61feb862b6083c8480aec864fa17a81fdec6f69b461ab"
"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
"checksum wincolor 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "561ed901ae465d6185fa7864d63fbd5720d0ef718366c9a4dc83cf6170d7e9ba"
+"checksum xdg 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57"
diff --git a/Cargo.toml b/Cargo.toml
index 8f42767..39f29a3 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,3 +12,5 @@ log = "0.4"
pretty_env_logger = "0.3"
dirs = "1.0.4"
lazy_static = "1.2.0"
+toml = "0.4.10"
+xdg = "^2.1"
diff --git a/src/bar.rs b/src/bar.rs
index e4d4b23..78106d0 100644
--- a/src/bar.rs
+++ b/src/bar.rs
@@ -1,6 +1,8 @@
use serde_json;
use serde_json::Value;
+use config;
+
pub enum BarColor {
Normal,
Success,
@@ -8,13 +10,17 @@ pub enum BarColor {
Failure,
}
-fn color_to_str(color: BarColor) -> &'static str {
+fn color_to_str(color: BarColor) -> String {
+ let colors = &config::CONFIG.colors;
+
match color {
- BarColor::Normal => "#C5C8C6",
- BarColor::Success => "#82B414",
- BarColor::Degraded => "#FD971F",
- BarColor::Failure => "#F92672",
+ BarColor::Normal => colors.get("normal"),
+ BarColor::Success => colors.get("success"),
+ BarColor::Degraded => colors.get("degraded"),
+ BarColor::Failure => colors.get("failure"),
}
+ .unwrap()
+ .clone()
}
/// JSON structure that represents a bar entry
@@ -22,7 +28,7 @@ fn color_to_str(color: BarColor) -> &'static str {
pub struct BarEntry {
name: String,
full_text: String,
- color: &'static str, // only static colors, no dynamic
+ color: String,
}
impl BarEntry {
diff --git a/src/config.rs b/src/config.rs
new file mode 100644
index 0000000..6ba7fa4
--- /dev/null
+++ b/src/config.rs
@@ -0,0 +1,71 @@
+use std::collections::HashMap;
+use std::fs::File;
+use std::io::{BufReader, Read};
+
+use xdg;
+
+lazy_static! {
+ pub static ref XDG: xdg::BaseDirectories = base_directories();
+ pub static ref CONFIG: Config = read_config();
+ pub static ref DEFAULT_COLORS: HashMap<String, String> = {
+ let mut colors = HashMap::new();
+ colors.insert("normal".into(), "#ffffff".into());
+ colors.insert("success".into(), "#00ff00".into());
+ colors.insert("degraded".into(), "#ffff00".into());
+ colors.insert("failure".into(), "#ff0000".into());
+
+ colors
+ };
+}
+
+fn base_directories() -> xdg::BaseDirectories {
+ xdg::BaseDirectories::with_prefix("superbar").expect("Cannot open XDG directories")
+}
+
+#[derive(Deserialize, Clone)]
+pub struct Config {
+ pub colors: HashMap<String, String>,
+ pub update_timeout: Option<u64>,
+}
+
+impl Config {
+ pub fn new() -> Config {
+ Config {
+ colors: HashMap::new(),
+ update_timeout: None,
+ }
+ }
+
+ pub fn fill_defaults(mut self) -> Config {
+ for (key, value) in DEFAULT_COLORS.iter() {
+ if !self.colors.contains_key(key) {
+ self.colors.insert(key.clone(), value.clone());
+ }
+ }
+ self
+ }
+}
+
+pub fn read_config() -> Config {
+ let path = XDG
+ .place_config_file("superbar.toml")
+ .expect("cannot create configuration directory");
+
+ let file = match File::open(path) {
+ Ok(file) => file,
+ Err(_) => {
+ error!("Could not find config file, using defaults");
+ return Config::new();
+ }
+ };
+
+ let mut buf_reader = BufReader::new(file);
+ let mut contents = String::new();
+
+ buf_reader
+ .read_to_string(&mut contents)
+ .expect("Error while reading config");
+
+ let config: Config = toml::from_str(&contents).expect("Could not parse config file");
+ config.fill_defaults()
+}
diff --git a/src/item_brightness.rs b/src/item_brightness.rs
index c4fde66..c391431 100644
--- a/src/item_brightness.rs
+++ b/src/item_brightness.rs
@@ -1,10 +1,5 @@
use std::error::Error;
-use std::fs;
-use std::io;
-use std::io::BufReader;
-use std::path::Path;
use std::process::Command;
-use std::time::{Duration, Instant};
use psutil::process;
@@ -40,9 +35,7 @@ fn get_max_brightness() -> Result<usize, Box<Error>> {
}
fn is_swayidle_running() -> std::io::Result<bool> {
- Ok(process::all()?
- .iter()
- .any(|p| p.comm == "swayidle"))
+ Ok(process::all()?.iter().any(|p| p.comm == "swayidle"))
}
pub struct Brightness {
diff --git a/src/item_update.rs b/src/item_update.rs
index 61bec3c..cf973b4 100644
--- a/src/item_update.rs
+++ b/src/item_update.rs
@@ -1,18 +1,24 @@
use std::fs;
use std::io;
-use std::path::{Path, PathBuf};
+use std::path::PathBuf;
use std::process::Command;
use std::time::{Duration, Instant};
use bar::{BarColor, BarItem};
+use config;
lazy_static! {
static ref FORCE_UPDATE_PATH: PathBuf = {
- let path = dirs::home_dir().unwrap().clone().join(".updated");
+ let path = config::XDG
+ .place_runtime_file("updated")
+ .expect("cannot create runtime file");
+
path
};
}
+const DEFAULT_TIMEOUT: Duration = Duration::from_secs(60 * 15);
+
/// Format text for the bar
fn bar_format(arg: &str) -> String {
format!("⬇️ {}", arg).into()
@@ -39,11 +45,14 @@ pub struct UpdateChecker {
impl UpdateChecker {
/// Create a new uninitialized instance.
- pub fn new(timeout: Duration) -> UpdateChecker {
+ pub fn new() -> UpdateChecker {
UpdateChecker {
last_check: None,
updates_count: None,
- timeout,
+ timeout: config::CONFIG
+ .update_timeout
+ .and_then(|i| Some(Duration::from_secs(i)))
+ .unwrap_or(DEFAULT_TIMEOUT),
error: false,
}
}
@@ -55,7 +64,7 @@ impl UpdateChecker {
/// once the update has been displayed.
fn force_update(&self) -> bool {
let res = FORCE_UPDATE_PATH.is_file();
- debug!("force_update: {}", res);
+ debug!("force_update: {} ({:?})", res, FORCE_UPDATE_PATH.to_str());
res
}
@@ -113,8 +122,7 @@ impl BarItem for UpdateChecker {
self.last_check = Some(Instant::now());
if force_update {
- fs::remove_file(&*FORCE_UPDATE_PATH)
- .expect("unable to delete force update file");
+ fs::remove_file(&*FORCE_UPDATE_PATH).expect("unable to delete force update file");
}
}
}
diff --git a/src/main.rs b/src/main.rs
index b99174f..a9e5c9b 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,5 @@
use std::io;
use std::io::{BufRead, BufReader, Write};
-use std::time::Duration;
extern crate psutil;
extern crate serde;
@@ -12,45 +11,46 @@ extern crate serde_derive;
#[macro_use]
extern crate log;
-extern crate pretty_env_logger;
extern crate dirs;
+extern crate pretty_env_logger;
+
+#[macro_use]
+extern crate lazy_static;
-#[macro_use] extern crate lazy_static;
+extern crate xdg;
mod bar;
-use bar::BarItem;
+mod config;
mod item_brightness;
mod item_update;
+
+use bar::BarItem;
use item_brightness::Brightness;
use item_update::UpdateChecker;
-struct Item {
+struct PositionalItem {
position: usize,
- item: Box<BarItem>
+ item: Box<BarItem>,
}
-fn add_item<T>(items: &mut Vec<Item>, position: usize, item: T)
- where T: 'static + BarItem
-{
- let item = Item {
- position,
- item: Box::new(item)
- };
- items.push(item);
+impl PositionalItem {
+ fn new<T>(position: usize, item: T) -> PositionalItem
+ where
+ T: 'static + BarItem,
+ {
+ PositionalItem {
+ position,
+ item: Box::new(item),
+ }
+ }
}
fn main() {
pretty_env_logger::init();
- let mut items: Vec<Item> = Vec::new();
-
- add_item(&mut items, 0, UpdateChecker::new(Duration::from_secs(60 * 15)));
- add_item(&mut items, 4, Brightness::new());
-
- /*
- items.push(Box::new((0, )));
- items.push(Box::new((4, Brightness::new())));
- */
+ let mut items: Vec<PositionalItem> = Vec::new();
+ items.push(PositionalItem::new(0, UpdateChecker::new()));
+ items.push(PositionalItem::new(4, Brightness::new()));
let mut reader = BufReader::new(io::stdin());
let mut buf = String::new();