Skip to content

Commit

Permalink
Add load config file
Browse files Browse the repository at this point in the history
  • Loading branch information
dalance committed Jan 30, 2019
1 parent 1ca0f3f commit 0e41d4c
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 3 deletions.
90 changes: 90 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ readme = "README.md"
description = "A modern replacement for ps"
edition = "2018"

[badges]
travis-ci = { repository = "dalance/procs" }
codecov = { repository = "dalance/procs", branch = "master", service = "github" }

[dependencies]
console = "0.7.5"
chrono = "0.4.6"
dirs = "1.0.4"
failure = "0.1.5"
lazy_static = "1.2.0"
procfs = "0.4.3"
Expand Down
28 changes: 25 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ mod util;
use column::Column;
use columns::*;
use console::{Style, StyledObject, Term};
use failure::Error;
use failure::{Error, ResultExt};
use lazy_static::lazy_static;
use procfs::Process;
use serde_derive::{Deserialize, Serialize};
use std::fs;
use std::io::Read;
use std::thread;
use std::time::{Duration, Instant};
use structopt::{clap, StructOpt};
Expand Down Expand Up @@ -437,14 +439,34 @@ fn main() {
fn run() -> Result<(), Error> {
let opt = Opt::from_args();

let config: Config = toml::from_str(CONFIG_DEFAULT).unwrap();

if opt.config {
let config: Config = toml::from_str(CONFIG_DEFAULT).unwrap();
let toml = toml::to_string(&config)?;
println!("{}", toml);
return Ok(());
}

let cfg_path = match dirs::home_dir() {
Some(mut path) => {
path.push(".procs.toml");
if path.exists() {
Some(path)
} else {
None
}
}
None => None,
};

let config: Config = if let Some(path) = cfg_path {
let mut f = fs::File::open(&path).context(format!("failed to open file ({:?})", path))?;
let mut s = String::new();
let _ = f.read_to_string(&mut s);
toml::from_str(&s).context(format!("failed to parse toml ({:?})", path))?
} else {
toml::from_str(CONFIG_DEFAULT).unwrap()
};

let mut cols = Vec::new();

for c in config.columns {
Expand Down

0 comments on commit 0e41d4c

Please sign in to comment.