Move common helpers to src/ (#465)

This commit is contained in:
Denis Isidoro 2021-04-04 17:46:33 -03:00 committed by GitHub
parent 347c19ddca
commit 23be67d7ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 91 additions and 53 deletions

View File

@ -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<SourceError>(source: SourceError) -> Self
where
SourceError: Into<anyhow::Error>,
{
FileAnIssue {
source: source.into(),
}
}
}
fn main() -> Result<(), anyhow::Error> {
navi::handle_config(navi::config_from_env()).map_err(|e| FileAnIssue::new(e).into())
}

42
src/clipboard.rs Normal file
View File

@ -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(())
}

View File

@ -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;

View File

@ -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;

View File

@ -1,6 +1,6 @@
use crate::common::url;
use crate::handler;
use crate::structures::config;
use crate::url;
use anyhow::Error;
#[derive(Debug)]

View File

@ -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)]

View File

@ -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;

View File

@ -1,4 +1,4 @@
use crate::common::shell::Shell;
use crate::shell::Shell;
use anyhow::Error;
pub fn main(shell: &Shell) -> Result<(), Error> {

View File

@ -1,4 +1,4 @@
use crate::common::shell::BashSpawnError;
use crate::shell::BashSpawnError;
use anyhow::Error;
use std::process::Command;

View File

@ -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(),
}
}
}

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -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;

View File

@ -1,4 +1,4 @@
use crate::common::shell::BashSpawnError;
use crate::shell::BashSpawnError;
use anyhow::{Context, Error};
use std::process::Command;

View File

@ -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};

View File

@ -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;

View File

@ -1,4 +1,4 @@
use crate::common::hash::fnv;
use crate::hash::fnv;
use crate::structures::finder::Opts;
use std::collections::HashMap;

View File

@ -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;

View File

@ -1,4 +1,4 @@
use crate::common::shell::BashSpawnError;
use crate::shell::BashSpawnError;
use anyhow::Error;
use std::process::Command;