Skip to content

Commit

Permalink
Add config file support (atuinsh#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ellie authored Mar 10, 2021
1 parent b42d93d commit 61607e0
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 39 deletions.
86 changes: 61 additions & 25 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ hostname = "0.3.1"
rocket = "0.4.7"
chrono-english = "0.1.4"
cli-table = "0.4"
config = "0.9"
config = "0.10"
serde_derive = "1.0.124"
serde = "1.0.124"

[dependencies.rusqlite]
version = "0.24"
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,20 @@ $ atuin stats all
+---------------------+-------+
```

## Config

A'tuin is configurable via TOML. The file lives at ` ~/.config/atuin/config.toml`,
and looks like this:

```
[local]
dialect = "uk" # or us. sets the date format used by stats
server_address = "https://atuin.elliehuxtable.com/" # the server to sync with
[local.db]
path = "~/.local/share/atuin/history.db" # the local database for history
```

## ...what's with the name?

A'tuin is named after "The Great A'tuin", a giant turtle from Terry Pratchett's
Expand Down
5 changes: 3 additions & 2 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use structopt::StructOpt;
use uuid::Uuid;

use crate::local::database::Database;
use crate::settings::Settings;

mod history;
mod import;
Expand Down Expand Up @@ -39,12 +40,12 @@ pub fn uuid_v4() -> String {
}

impl AtuinCmd {
pub fn run(self, db: &mut impl Database) -> Result<()> {
pub fn run(self, db: &mut impl Database, settings: &Settings) -> Result<()> {
match self {
Self::History(history) => history.run(db),
Self::Import(import) => import.run(db),
Self::Server(server) => server.run(),
Self::Stats(stats) => stats.run(db),
Self::Stats(stats) => stats.run(db, settings),
Self::Init => init::init(),

Self::Uuid => {
Expand Down
8 changes: 6 additions & 2 deletions src/command/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use structopt::StructOpt;

use crate::local::database::Database;
use crate::local::history::History;
use crate::settings::Settings;

#[derive(StructOpt)]
pub enum Cmd {
Expand Down Expand Up @@ -70,7 +71,7 @@ fn compute_stats(history: &[History]) -> Result<()> {
}

impl Cmd {
pub fn run(&self, db: &mut impl Database) -> Result<()> {
pub fn run(&self, db: &mut impl Database, settings: &Settings) -> Result<()> {
match self {
Self::Day { words } => {
let words = if words.is_empty() {
Expand All @@ -79,7 +80,10 @@ impl Cmd {
words.join(" ")
};

let start = parse_date_string(&words, Local::now(), Dialect::Us)?;
let start = match settings.local.dialect.to_lowercase().as_str() {
"uk" => parse_date_string(&words, Local::now(), Dialect::Uk)?,
_ => parse_date_string(&words, Local::now(), Dialect::Us)?,
};
let end = start + Duration::days(1);

let history = db.range(start.into(), end.into())?;
Expand Down
19 changes: 10 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#![feature(str_split_once)]
#![feature(proc_macro_hygiene)]
#![feature(decl_macro)]
#![warn(clippy::pedantic, clippy::nursery)]
#![allow(clippy::use_self)] // not 100% reliable

use std::path::PathBuf;

use directories::ProjectDirs;
use eyre::{eyre, Result};
use structopt::StructOpt;

Expand All @@ -15,12 +14,17 @@ extern crate log;
#[macro_use]
extern crate rocket;

#[macro_use]
extern crate serde_derive;

use command::AtuinCmd;
use local::database::Sqlite;
use settings::Settings;

mod command;
mod local;
mod remote;
mod settings;

#[derive(StructOpt)]
#[structopt(
Expand All @@ -38,24 +42,21 @@ struct Atuin {

impl Atuin {
fn run(self) -> Result<()> {
let settings = Settings::new()?;

let db_path = if let Some(db_path) = self.db {
let path = db_path
.to_str()
.ok_or_else(|| eyre!("path {:?} was not valid UTF-8", db_path))?;
let path = shellexpand::full(path)?;
PathBuf::from(path.as_ref())
} else {
ProjectDirs::from("com", "elliehuxtable", "atuin")
.ok_or_else(|| {
eyre!("could not determine db file location\nspecify one using the --db flag")
})?
.data_dir()
.join("history.db")
PathBuf::from(settings.local.db.path.as_str())
};

let mut db = Sqlite::new(db_path)?;

self.atuin.run(&mut db)
self.atuin.run(&mut db, &settings)
}
}

Expand Down
Loading

0 comments on commit 61607e0

Please sign in to comment.