aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEllie Huxtable <e@elm.sh>2020-10-05 11:57:09 +0100
committerGitHub <noreply@github.com>2020-10-05 11:57:09 +0100
commit9232ac27ce6d820c87d240061c1525866d69b2b2 (patch)
tree12a5a69126fac0ca0f056f5139f3177af267a661 /src
parentadd readme (diff)
parentchore: use structopt wrapper instead of building clap by hand (diff)
downloadatuin-9232ac27ce6d820c87d240061c1525866d69b2b2.zip
Merge pull request #1 from conradludgate/main
chore: use structopt wrapper instead of building clap by hand
Diffstat (limited to 'src')
-rw-r--r--src/main.rs123
1 files changed, 70 insertions, 53 deletions
diff --git a/src/main.rs b/src/main.rs
index a9b08c00..769c3235 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,6 +1,6 @@
use std::env;
-use clap::{Arg, App, SubCommand};
+use structopt::StructOpt;
use eyre::Result;
#[macro_use] extern crate log;
@@ -11,65 +11,82 @@ mod local;
use local::history::History;
use local::database::{Database, SqliteDatabase};
-fn main() -> Result<()> {
- pretty_env_logger::init();
+#[derive(StructOpt)]
+#[structopt(
+ author="Ellie Huxtable <e@elm.sh>",
+ version="0.1.0",
+ about="Keep your shell history in sync"
+)]
+enum Shync {
+ #[structopt(
+ about="manipulate shell history",
+ aliases=&["h", "hi", "his", "hist", "histo", "histor"],
+ )]
+ History(HistoryCmd),
- let db = SqliteDatabase::new("~/.history.db")?;
+ #[structopt(
+ about="import shell history from file",
+ )]
+ Import,
- let matches = App::new("Shync")
- .version("0.1.0")
- .author("Ellie Huxtable <e@elm.sh>")
- .about("Keep your shell history in sync")
- .subcommand(
- SubCommand::with_name("history")
- .aliases(&["h", "hi", "his", "hist", "histo", "histor"])
- .about("manipulate shell history")
- .subcommand(
- SubCommand::with_name("add")
- .aliases(&["a", "ad"])
- .about("add a new command to the history")
- .arg(
- Arg::with_name("command")
- .multiple(true)
- .required(true)
- )
- )
- .subcommand(
- SubCommand::with_name("list")
- .aliases(&["l", "li", "lis"])
- .about("list all items in history")
- )
- )
- .subcommand(
- SubCommand::with_name("import")
- .about("import shell history from file")
- )
- .subcommand(
- SubCommand::with_name("server")
- .about("start a shync server")
- )
- .get_matches();
+ #[structopt(
+ about="start a shync server",
+ )]
+ Server,
+}
+impl Shync {
+ fn run(self, db: SqliteDatabase) -> Result<()> {
+ match self {
+ Shync::History(history) => history.run(db),
+ _ => Ok(())
+ }
+ }
+}
- if let Some(m) = matches.subcommand_matches("history") {
- if let Some(m) = m.subcommand_matches("add") {
- let words: Vec<&str> = m.values_of("command").unwrap().collect();
- let command = words.join(" ");
+#[derive(StructOpt)]
+enum HistoryCmd {
+ #[structopt(
+ about="add a new command to the history",
+ aliases=&["a", "ad"],
+ )]
+ Add {
+ command: Vec<String>,
+ },
- let cwd = env::current_dir()?;
- let h = History::new(
- command.as_str(),
- cwd.display().to_string().as_str(),
- );
+ #[structopt(
+ about="list all items in history",
+ aliases=&["l", "li", "lis"],
+ )]
+ List,
+}
- debug!("adding history: {:?}", h);
- db.save(h)?;
- debug!("saved history to sqlite");
- }
- else if let Some(_m) = m.subcommand_matches("list") {
- db.list()?;
+impl HistoryCmd {
+ fn run(self, db: SqliteDatabase) -> Result<()> {
+ match self {
+ HistoryCmd::Add{command: words} => {
+ let command = words.join(" ");
+
+ let cwd = env::current_dir()?;
+ let h = History::new(
+ command.as_str(),
+ cwd.display().to_string().as_str(),
+ );
+
+ debug!("adding history: {:?}", h);
+ db.save(h)?;
+ debug!("saved history to sqlite");
+ Ok(())
+ }
+
+ HistoryCmd::List => db.list()
}
}
+}
- Ok(())
+fn main() -> Result<()> {
+ pretty_env_logger::init();
+
+ let db = SqliteDatabase::new("~/.history.db")?;
+ Shync::from_args().run(db)
}