Skip to content

Commit

Permalink
Move common helpers to src/ (#465)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro authored Apr 4, 2021
1 parent 347c19d commit 23be67d
Show file tree
Hide file tree
Showing 24 changed files with 90 additions and 52 deletions.
26 changes: 25 additions & 1 deletion src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,29 @@
extern crate navi;

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| navi::FileAnIssue::new(e).into())
navi::handle_config(navi::config_from_env()).map_err(|e| FileAnIssue::new(e).into())
}
42 changes: 42 additions & 0 deletions src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
2 changes: 1 addition & 1 deletion src/cmds/alfred.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/core.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/func.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::common::url;
use crate::handler;
use crate::structures::config;
use crate::url;
use anyhow::Error;

#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/info.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/repo.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/cmds/shell.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::shell::Shell;
use crate::shell::Shell;
use anyhow::Error;

pub fn main(shell: &Shell) -> Result<(), Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/common/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::shell::BashSpawnError;
use crate::shell::BashSpawnError;
use anyhow::Error;
use std::process::Command;

Expand Down
23 changes: 0 additions & 23 deletions src/common/file_issue.rs

This file was deleted.

8 changes: 0 additions & 8 deletions src/common/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion src/display/terminal.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/fetcher/filesystem.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 1 addition & 3 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/common/git.rs → src/git.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::shell::BashSpawnError;
use crate::shell::BashSpawnError;
use anyhow::{Context, Error};
use std::process::Command;

Expand Down
File renamed without changes.
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
2 changes: 1 addition & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/structures/cheat.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::hash::fnv;
use crate::hash::fnv;
use crate::structures::finder::Opts;
use std::collections::HashMap;

Expand Down
2 changes: 1 addition & 1 deletion src/structures/config.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/common/url.rs → src/url.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::common::shell::BashSpawnError;
use crate::shell::BashSpawnError;
use anyhow::Error;
use std::process::Command;

Expand Down

0 comments on commit 23be67d

Please sign in to comment.