summaryrefslogtreecommitdiffstats
path: root/src/structures
diff options
context:
space:
mode:
authorDenis Isidoro <denisidoro@users.noreply.github.com>2020-08-27 15:15:09 -0300
committerGitHub <noreply@github.com>2020-08-27 15:15:09 -0300
commite49ff1a18d0c9ff56a71648c9adcb0f2a551edbb (patch)
tree6406fe0343fe96eed402abb78325d5a0d9d9524d /src/structures
parentc5017b1a6350ebaeb33524c77c992e43dc81eb1f (diff)
Refactor code (#384)
Diffstat (limited to 'src/structures')
-rw-r--r--src/structures/cheat.rs12
-rw-r--r--src/structures/config.rs105
-rw-r--r--src/structures/error/command.rs22
-rw-r--r--src/structures/error/file_issue.rs23
-rw-r--r--src/structures/error/filesystem.rs27
-rw-r--r--src/structures/error/mod.rs3
-rw-r--r--src/structures/fnv.rs42
-rw-r--r--src/structures/mod.rs2
8 files changed, 100 insertions, 136 deletions
diff --git a/src/structures/cheat.rs b/src/structures/cheat.rs
index 3b5a7ce..bbddd01 100644
--- a/src/structures/cheat.rs
+++ b/src/structures/cheat.rs
@@ -1,5 +1,5 @@
+use crate::common::hash::fnv;
use crate::structures::finder::Opts;
-use crate::structures::fnv::HashLine;
use std::collections::HashMap;
pub type Suggestion = (String, Option<Opts>);
@@ -19,18 +19,18 @@ impl VariableMap {
}
pub fn insert_dependency(&mut self, tags: &str, tags_dependency: &str) {
- let k = tags.hash_line();
+ let k = fnv(&tags);
if let Some(v) = self.dependencies.get_mut(&k) {
- v.push(tags_dependency.hash_line());
+ v.push(fnv(&tags_dependency));
} else {
let mut v: Vec<u64> = Vec::new();
- v.push(tags_dependency.hash_line());
+ v.push(fnv(&tags_dependency));
self.dependencies.insert(k, v);
}
}
pub fn insert_suggestion(&mut self, tags: &str, variable: &str, value: Suggestion) {
- let k1 = tags.hash_line();
+ let k1 = fnv(&tags);
let k2 = String::from(variable);
if let Some(m) = self.variables.get_mut(&k1) {
m.insert(k2, value);
@@ -42,7 +42,7 @@ impl VariableMap {
}
pub fn get_suggestion(&self, tags: &str, variable: &str) -> Option<&Suggestion> {
- let k = tags.hash_line();
+ let k = fnv(&tags);
let res = self.variables.get(&k)?.get(variable);
if res.is_some() {
return res;
diff --git a/src/structures/config.rs b/src/structures/config.rs
index 6e89f4e..07b9451 100644
--- a/src/structures/config.rs
+++ b/src/structures/config.rs
@@ -18,9 +18,8 @@ EXAMPLES:
navi # default behavior
navi --print # doesn't execute the snippet
navi --path '/some/dir:/other/dir' # uses custom cheats
- navi search docker # uses online data
navi query git # filters results by "git"
- navi best 'sql create db' root mydb # uses a snippet as a CLI
+ navi best 'sql create db' # uses a snippet as a CLI
navi repo add denisidoro/cheats # imports cheats from github.com/denisidoro/cheats
source <(navi widget zsh) # loads the zsh widget
navi --finder 'skim' # set which finder is supposed to be used (fzf [default] / skim)
@@ -37,20 +36,32 @@ pub struct Config {
/// [Experimental] Instead of executing a snippet, saves it to a file
#[structopt(short, long)]
- pub save: Option<String>,
+ save: Option<String>,
/// Instead of executing a snippet, prints it to stdout
#[structopt(long)]
- pub print: bool,
+ print: bool,
/// Prevents autoselection in case of single entry
#[structopt(long)]
- pub no_autoselect: bool,
+ no_autoselect: bool,
/// Hides preview window
#[structopt(long)]
pub no_preview: bool,
+ /// Returns the best match
+ #[structopt(long)]
+ single: bool,
+
+ /// Search for cheatsheets using the tldr-pages repository
+ #[structopt(long)]
+ tldr: Option<String>,
+
+ /// Query
+ #[structopt(short, long)]
+ query: Option<String>,
+
/// finder overrides for cheat selection
#[structopt(long, env = "NAVI_FZF_OVERRIDES")]
pub fzf_overrides: Option<String>,
@@ -59,7 +70,7 @@ pub struct Config {
#[structopt(long, env = "NAVI_FZF_OVERRIDES_VAR")]
pub fzf_overrides_var: Option<String>,
- /// finder overrides for variable selection
+ /// which finder application to use
#[structopt(long, env = "NAVI_FINDER", default_value = "fzf", parse(try_from_str = parse_finder))]
pub finder: FinderChoice,
@@ -70,16 +81,13 @@ pub struct Config {
#[derive(Debug, StructOpt)]
pub enum Command {
/// Filters results
+ #[structopt(setting = AppSettings::Hidden)]
Query {
/// String used as filter (example: "git")
query: String,
},
- /// Uses online repositories for cheatsheets
- Search {
- /// String used as filter (example: "git")
- query: String,
- },
/// Autoselects the snippet that best matches the query
+ #[structopt(setting = AppSettings::Hidden)]
Best {
/// String used as filter (example: "git remove branch")
query: String,
@@ -140,6 +148,81 @@ pub enum AlfredCommand {
Check,
}
+fn deprecated(syntax: &str) {
+ eprintln!(
+ r"Warning: the following syntax has been DEPRECATED:
+navi {}
+
+Please check navi --help for more info on how to achieve the same result with the new syntax.
+
+The deprecated syntax will be removed in the first version released on 2021!",
+ syntax
+ );
+}
+
+pub enum Source {
+ FILESYSTEM(Option<String>),
+ TLDR(String),
+}
+
+pub enum Action {
+ SAVE(String),
+ PRINT,
+ EXECUTE,
+}
+
+impl Config {
+ pub fn source(&self) -> Source {
+ if let Some(query) = self.tldr.clone() {
+ Source::TLDR(query)
+ } else {
+ Source::FILESYSTEM(self.path.clone())
+ }
+ }
+
+ pub fn action(&self) -> Action {
+ if let Some(filepath) = self.save.clone() {
+ Action::SAVE(filepath)
+ } else if self.print {
+ Action::PRINT
+ } else {
+ Action::EXECUTE
+ }
+ }
+
+ pub fn get_query(&self) -> Option<String> {
+ match &self.cmd {
+ Some(Command::Query { query }) => {
+ deprecated("query <query>");
+ Some(query.clone())
+ }
+ Some(Command::Best { query, .. }) => {
+ deprecated("best <query>");
+ Some(query.clone())
+ }
+ _ => self.query.clone(),
+ }
+ }
+
+ pub fn get_single(&self) -> bool {
+ if let Some(Command::Best { .. }) = &self.cmd {
+ deprecated("best <query>");
+ true
+ } else {
+ self.single
+ }
+ }
+
+ pub fn get_no_autoselect(&self) -> bool {
+ if self.no_autoselect {
+ deprecated("--no-autoselect");
+ true
+ } else {
+ false
+ }
+ }
+}
+
pub fn config_from_env() -> Config {
Config::from_args()
}
diff --git a/src/structures/error/command.rs b/src/structures/error/command.rs
deleted file mode 100644
index 9af9165..0000000
--- a/src/structures/error/command.rs
+++ /dev/null
@@ -1,22 +0,0 @@
-use std::fmt::Debug;
-use thiserror::Error;
-
-#[derive(Error, Debug)]
-#[error("Failed to spawn child process `bash` to execute `{command}`")]
-pub struct BashSpawnError {
- command: String,
- #[source]
- source: anyhow::Error,
-}
-
-impl BashSpawnError {
- pub fn new<SourceError>(command: impl Into<String>, source: SourceError) -> Self
- where
- SourceError: std::error::Error + Sync + Send + 'static,
- {
- BashSpawnError {
- command: command.into(),
- source: source.into(),
- }
- }
-}
diff --git a/src/structures/error/file_issue.rs b/src/structures/error/file_issue.rs
deleted file mode 100644
index 40d468f..0000000
--- a/src/structures/error/file_issue.rs
+++ /dev/null
@@ -1,23 +0,0 @@
-use std::fmt::Debug;
-use thiserror::Error;
-
-#[derive(Error, Debug)]
-#[error(
- "\rHey, listen! navi encountered a problem.
-Do you think this is a bug? File an issue at https://github.com/denisidoro/navi."
-)]
-pub struct FileAnIssue {
- #[source]
- source: anyhow::Error,
-}
-
-impl FileAnIssue {
- pub fn new<SourceError>(source: SourceError) -> Self
- where
- SourceError: Into<anyhow::Error>,
- {
- FileAnIssue {
- source: source.into(),
- }
- }
-}
diff --git a/src/structures/error/filesystem.rs b/src/structures/error/filesystem.rs
deleted file mode 100644
index c14f682..0000000
--- a/src/structures/error/filesystem.rs
+++ /dev/null
@@ -1,27 +0,0 @@
-use std::{fmt::Debug, path::PathBuf};
-use thiserror::Error;
-
-#[derive(Error, Debug)]
-#[error("Invalid path `{0}`")]
-pub struct InvalidPath(pub PathBuf);
-
-#[derive(Error, Debug)]
-#[error("Unable to read directory `{dir}`")]
-pub struct UnreadableDir {
- dir: PathBuf,
- #[source]
- source: anyhow::Error,
-}
-
-impl UnreadableDir {
- pub fn new<DirT, SourceError>(dir: DirT, source: SourceError) -> Self
- where
- DirT: Into<PathBuf>,
- SourceError: std::error::Error + Sync + Send + 'static,
- {
- UnreadableDir {
- dir: dir.into(),
- source: source.into(),
- }
- }
-}
diff --git a/src/structures/error/mod.rs b/src/structures/error/mod.rs
deleted file mode 100644
index 4c4fadd..0000000
--- a/src/structures/error/mod.rs
+++ /dev/null
@@ -1,3 +0,0 @@
-pub mod command;
-pub mod file_issue;
-pub mod filesystem;
diff --git a/src/structures/fnv.rs b/src/structures/fnv.rs
deleted file mode 100644
index ce4f372..0000000
--- a/src/structures/fnv.rs
+++ /dev/null
@@ -1,42 +0,0 @@
-use std::hash::{Hash, Hasher};
-
-const MAGIC_INIT: u64 = 0x811C_9DC5;
-
-pub trait HashLine: Hash {
- fn hash_line(&self) -> u64;
-}
-
-impl<T> HashLine for T
-where
- T: Hash,
-{
- fn hash_line(&self) -> u64 {
- let mut hasher = FnvHasher::new();
- self.hash(&mut hasher);
- hasher.finish()
- }
-}
-
-pub(crate) struct FnvHasher(u64);
-
-impl FnvHasher {
- pub(crate) fn new() -> Self {
- FnvHasher(MAGIC_INIT)
- }
-}
-
-impl Hasher for FnvHasher {
- fn finish(&self) -> u64 {
- self.0
- }
- fn write(&mut self, bytes: &[u8]) {
- let FnvHasher(mut hash) = *self;
-
- for byte in bytes.iter() {
- hash ^= u64::from(*byte);
- hash = hash.wrapping_mul(0x0100_0000_01b3);
- }
-
- *self = FnvHasher(hash);
- }
-}
diff --git a/src/structures/mod.rs b/src/structures/mod.rs
index 8d8a779..2beae47 100644
--- a/src/structures/mod.rs
+++ b/src/structures/mod.rs
@@ -1,6 +1,4 @@
pub mod cheat;
pub mod config;
-pub mod error;
pub mod finder;
-pub mod fnv;
pub mod item;