From 23be67d7ce001a8a4af2f84bdf7b5616b6f631ec Mon Sep 17 00:00:00 2001 From: Denis Isidoro Date: Sun, 4 Apr 2021 17:46:33 -0300 Subject: [PATCH] Move common helpers to src/ (#465) --- src/bin/main.rs | 28 +++++++++++++++++-- src/clipboard.rs | 42 +++++++++++++++++++++++++++++ src/cmds/alfred.rs | 2 +- src/cmds/core.rs | 4 +-- src/cmds/func.rs | 2 +- src/cmds/info.rs | 2 +- src/cmds/repo.rs | 2 +- src/cmds/shell.rs | 2 +- src/common/clipboard.rs | 2 +- src/common/file_issue.rs | 23 ---------------- src/common/mod.rs | 8 ------ src/display/terminal.rs | 2 +- src/fetcher/filesystem.rs | 2 +- src/filesystem.rs | 4 +-- src/{common/filesystem.rs => fs.rs} | 0 src/{common => }/git.rs | 2 +- src/{common => }/hash.rs | 0 src/lib.rs | 9 +++++-- src/parser.rs | 2 +- src/{common => }/shell.rs | 0 src/structures/cheat.rs | 2 +- src/structures/config.rs | 2 +- src/{common => }/terminal_width.rs | 0 src/{common => }/url.rs | 2 +- 24 files changed, 91 insertions(+), 53 deletions(-) create mode 100644 src/clipboard.rs delete mode 100644 src/common/file_issue.rs delete mode 100644 src/common/mod.rs rename src/{common/filesystem.rs => fs.rs} (100%) rename src/{common => }/git.rs (97%) rename src/{common => }/hash.rs (100%) rename src/{common => }/shell.rs (100%) rename src/{common => }/terminal_width.rs (100%) rename src/{common => }/url.rs (94%) diff --git a/src/bin/main.rs b/src/bin/main.rs index 4abb65c..08cb4d0 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,5 +1,29 @@ extern crate navi; -fn main() -> Result<(), anyhow::Error> { - navi::handle_config(navi::config_from_env()).map_err(|e| navi::FileAnIssue::new(e).into()) +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(source: SourceError) -> Self + where + SourceError: Into, + { + FileAnIssue { + source: source.into(), + } + } +} + +fn main() -> Result<(), anyhow::Error> { + navi::handle_config(navi::config_from_env()).map_err(|e| FileAnIssue::new(e).into()) } diff --git a/src/clipboard.rs b/src/clipboard.rs new file mode 100644 index 0000000..addc97b --- /dev/null +++ b/src/clipboard.rs @@ -0,0 +1,42 @@ +use crate::shell::BashSpawnError; +use anyhow::Error; +use std::process::Command; + +pub fn copy(text: String) -> Result<(), Error> { + let cmd = r#" +exst() { + type "$1" &>/dev/null +} + +_copy() { + if exst pbcopy; then + pbcopy + elif exst xclip; then + xclip -selection clipboard + elif exst clip.exe; then + clip.exe + else + exit 55 + fi +}"#; + + Command::new("bash") + .arg("-c") + .arg( + format!( + r#"{} + read -r -d '' x <<'NAVIEOF' +{} +NAVIEOF + +echo -n "$x" | _copy"#, + cmd, text + ) + .as_str(), + ) + .spawn() + .map_err(|e| BashSpawnError::new(cmd, e))? + .wait()?; + + Ok(()) +} diff --git a/src/cmds/alfred.rs b/src/cmds/alfred.rs index a3db0c5..489393e 100644 --- a/src/cmds/alfred.rs +++ b/src/cmds/alfred.rs @@ -1,7 +1,7 @@ -use crate::common::shell::BashSpawnError; use crate::display; use crate::fetcher::Fetcher; use crate::filesystem; +use crate::shell::BashSpawnError; use crate::structures::cheat::Suggestion; use crate::structures::config::Config; use anyhow::Context; diff --git a/src/cmds/core.rs b/src/cmds/core.rs index 8f77951..3ca5648 100644 --- a/src/cmds/core.rs +++ b/src/cmds/core.rs @@ -1,11 +1,11 @@ use crate::cheatsh; -use crate::common::clipboard; -use crate::common::shell::{BashSpawnError, IS_FISH}; +use crate::clipboard; use crate::display; use crate::env_vars; use crate::fetcher::Fetcher; use crate::filesystem; use crate::finder::Finder; +use crate::shell::{BashSpawnError, IS_FISH}; use crate::structures::cheat::{Suggestion, VariableMap}; use crate::structures::config::Action; use crate::structures::config::Config; diff --git a/src/cmds/func.rs b/src/cmds/func.rs index 666908e..3fb910a 100644 --- a/src/cmds/func.rs +++ b/src/cmds/func.rs @@ -1,6 +1,6 @@ -use crate::common::url; use crate::handler; use crate::structures::config; +use crate::url; use anyhow::Error; #[derive(Debug)] diff --git a/src/cmds/info.rs b/src/cmds/info.rs index ebe6238..c7f7942 100644 --- a/src/cmds/info.rs +++ b/src/cmds/info.rs @@ -1,5 +1,5 @@ -use crate::common::filesystem::pathbuf_to_string; use crate::filesystem::default_cheat_pathbuf; +use crate::fs::pathbuf_to_string; use anyhow::Error; #[derive(Debug)] diff --git a/src/cmds/repo.rs b/src/cmds/repo.rs index 6dbc50d..f881a4b 100644 --- a/src/cmds/repo.rs +++ b/src/cmds/repo.rs @@ -1,6 +1,6 @@ -use crate::common::git; use crate::filesystem; use crate::finder::{Finder, FinderChoice}; +use crate::git; use crate::structures::finder::{Opts as FinderOpts, SuggestionType}; use anyhow::Context; use anyhow::Error; diff --git a/src/cmds/shell.rs b/src/cmds/shell.rs index 9dfb9ba..774cc5d 100644 --- a/src/cmds/shell.rs +++ b/src/cmds/shell.rs @@ -1,4 +1,4 @@ -use crate::common::shell::Shell; +use crate::shell::Shell; use anyhow::Error; pub fn main(shell: &Shell) -> Result<(), Error> { diff --git a/src/common/clipboard.rs b/src/common/clipboard.rs index 12b7c58..addc97b 100644 --- a/src/common/clipboard.rs +++ b/src/common/clipboard.rs @@ -1,4 +1,4 @@ -use crate::common::shell::BashSpawnError; +use crate::shell::BashSpawnError; use anyhow::Error; use std::process::Command; diff --git a/src/common/file_issue.rs b/src/common/file_issue.rs deleted file mode 100644 index 40d468f..0000000 --- a/src/common/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(source: SourceError) -> Self - where - SourceError: Into, - { - FileAnIssue { - source: source.into(), - } - } -} diff --git a/src/common/mod.rs b/src/common/mod.rs deleted file mode 100644 index aaf3771..0000000 --- a/src/common/mod.rs +++ /dev/null @@ -1,8 +0,0 @@ -pub mod clipboard; -pub mod file_issue; -pub mod filesystem; -pub mod git; -pub mod hash; -pub mod shell; -pub mod terminal_width; -pub mod url; diff --git a/src/display/terminal.rs b/src/display/terminal.rs index 1eba023..649b86a 100644 --- a/src/display/terminal.rs +++ b/src/display/terminal.rs @@ -1,8 +1,8 @@ -use crate::common::terminal_width; use crate::display; use crate::env_vars; use crate::finder; use crate::structures::item::Item; +use crate::terminal_width; use std::cmp::max; use std::collections::HashSet; use std::env; diff --git a/src/fetcher/filesystem.rs b/src/fetcher/filesystem.rs index 0622637..4c942b5 100644 --- a/src/fetcher/filesystem.rs +++ b/src/fetcher/filesystem.rs @@ -1,5 +1,5 @@ -use crate::common::filesystem::{pathbuf_to_string, read_lines}; use crate::display::Writer; +use crate::fs::{pathbuf_to_string, read_lines}; use crate::parser; use crate::structures::cheat::VariableMap; use anyhow::Error; diff --git a/src/filesystem.rs b/src/filesystem.rs index 4977938..50ee7a6 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -1,9 +1,7 @@ -pub use crate::common::filesystem::{ - create_dir, exe_string, pathbuf_to_string, remove_dir, InvalidPath, UnreadableDir, -}; use crate::display::Writer; use crate::fetcher; pub use crate::fetcher::filesystem::{all_cheat_files, default_cheat_pathbuf, read_all}; +pub use crate::fs::{create_dir, exe_string, pathbuf_to_string, remove_dir, InvalidPath, UnreadableDir}; use crate::structures::cheat::VariableMap; use anyhow::Error; diff --git a/src/common/filesystem.rs b/src/fs.rs similarity index 100% rename from src/common/filesystem.rs rename to src/fs.rs diff --git a/src/common/git.rs b/src/git.rs similarity index 97% rename from src/common/git.rs rename to src/git.rs index 2a942e8..08defe6 100644 --- a/src/common/git.rs +++ b/src/git.rs @@ -1,4 +1,4 @@ -use crate::common::shell::BashSpawnError; +use crate::shell::BashSpawnError; use anyhow::{Context, Error}; use std::process::Command; diff --git a/src/common/hash.rs b/src/hash.rs similarity index 100% rename from src/common/hash.rs rename to src/hash.rs diff --git a/src/lib.rs b/src/lib.rs index 637a826..13e3e6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,19 +4,24 @@ extern crate lazy_static; extern crate anyhow; mod cheatsh; +mod clipboard; mod cmds; -mod common; mod display; mod env_vars; mod fetcher; mod filesystem; mod finder; +mod fs; +mod git; mod handler; +mod hash; mod parser; +mod shell; mod structures; +mod terminal_width; mod tldr; +mod url; mod welcome; -pub use common::file_issue::FileAnIssue; pub use handler::handle_config; pub use structures::config::{config_from_env, config_from_iter}; diff --git a/src/parser.rs b/src/parser.rs index 01d3f8b..02e0d74 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -1,5 +1,5 @@ -use crate::common::hash::fnv; use crate::display::{self, Writer}; +use crate::hash::fnv; use crate::structures::cheat::VariableMap; use crate::structures::finder::{Opts as FinderOpts, SuggestionType}; use crate::structures::item::Item; diff --git a/src/common/shell.rs b/src/shell.rs similarity index 100% rename from src/common/shell.rs rename to src/shell.rs diff --git a/src/structures/cheat.rs b/src/structures/cheat.rs index e5b55c8..a49da3e 100644 --- a/src/structures/cheat.rs +++ b/src/structures/cheat.rs @@ -1,4 +1,4 @@ -use crate::common::hash::fnv; +use crate::hash::fnv; use crate::structures::finder::Opts; use std::collections::HashMap; diff --git a/src/structures/config.rs b/src/structures/config.rs index cc54201..6afbe58 100644 --- a/src/structures/config.rs +++ b/src/structures/config.rs @@ -1,8 +1,8 @@ use crate::cmds::func::Func; use crate::cmds::info::Info; -use crate::common::shell::Shell; use crate::env_vars; use crate::finder::FinderChoice; +use crate::shell::Shell; use clap::{crate_version, AppSettings, Clap}; use std::str::FromStr; diff --git a/src/common/terminal_width.rs b/src/terminal_width.rs similarity index 100% rename from src/common/terminal_width.rs rename to src/terminal_width.rs diff --git a/src/common/url.rs b/src/url.rs similarity index 94% rename from src/common/url.rs rename to src/url.rs index 23b61ca..1499d7d 100644 --- a/src/common/url.rs +++ b/src/url.rs @@ -1,4 +1,4 @@ -use crate::common::shell::BashSpawnError; +use crate::shell::BashSpawnError; use anyhow::Error; use std::process::Command;