Skip to content

Commit

Permalink
Add support for config file (#518)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro authored Apr 17, 2021
1 parent d8d0d81 commit 7fb2b53
Show file tree
Hide file tree
Showing 28 changed files with 718 additions and 331 deletions.
55 changes: 55 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ thiserror = "1.0.24"
strip-ansi-escapes = "0.1.0"
edit = "0.1.3"
remove_dir_all = "0.7.0"
serde = { version = "1.0", features = ["derive"] }
serde_yaml = "0.8"

[lib]
name = "navi"
Expand Down
32 changes: 12 additions & 20 deletions src/actor.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::clipboard;
use crate::config::Action;

use crate::config::CONFIG;
use crate::env_var;
use crate::extractor;
use crate::finder::structures::{Opts as FinderOpts, SuggestionType};
use crate::finder::Finder;
use crate::shell;
use crate::shell::ShellSpawnError;
use crate::structures::cheat::{Suggestion, VariableMap};
use crate::structures::config::Action;
use crate::structures::config::Config;
use crate::writer;
use anyhow::Context;
use anyhow::Result;
Expand All @@ -17,7 +18,6 @@ use std::process::Stdio;

fn prompt_finder(
variable_name: &str,
config: &Config,
suggestion: Option<&Suggestion>,
variable_count: usize,
) -> Result<String> {
Expand Down Expand Up @@ -66,7 +66,7 @@ fn prompt_finder(
};

let overrides = {
let mut o = config.fzf_overrides.clone();
let mut o = CONFIG.fzf_overrides_var();
if let Some(io) = initial_opts {
if io.overrides.is_some() {
o = io.overrides.clone()
Expand Down Expand Up @@ -110,8 +110,8 @@ NAVIEOF
opts.suggestion_type = SuggestionType::Disabled;
};

let (output, _, _) = config
.finder
let (output, _, _) = CONFIG
.finder()
.call(opts, |stdin, _| {
stdin
.write_all(suggestions.as_bytes())
Expand All @@ -130,12 +130,7 @@ fn unique_result_count(results: &[&str]) -> usize {
vars.len()
}

fn replace_variables_from_snippet(
snippet: &str,
tags: &str,
variables: VariableMap,
config: &Config,
) -> Result<String> {
fn replace_variables_from_snippet(snippet: &str, tags: &str, variables: VariableMap) -> Result<String> {
let mut interpolated_snippet = String::from(snippet);
let variables_found: Vec<&str> = writer::VAR_REGEX.find_iter(snippet).map(|m| m.as_str()).collect();
let variable_count = unique_result_count(&variables_found);
Expand All @@ -150,11 +145,10 @@ fn replace_variables_from_snippet(
e
} else if let Some(suggestion) = variables.get_suggestion(&tags, &variable_name) {
let mut new_suggestion = suggestion.clone();
new_suggestion.0 =
replace_variables_from_snippet(&new_suggestion.0, tags, variables.clone(), config)?;
prompt_finder(variable_name, &config, Some(&new_suggestion), variable_count)?
new_suggestion.0 = replace_variables_from_snippet(&new_suggestion.0, tags, variables.clone())?;
prompt_finder(variable_name, Some(&new_suggestion), variable_count)?
} else {
prompt_finder(variable_name, &config, None, variable_count)?
prompt_finder(variable_name, None, variable_count)?
};

env_var::set(env_variable_name, &value);
Expand All @@ -172,15 +166,14 @@ fn replace_variables_from_snippet(
// TODO: make it depend on less inputs
pub fn act(
extractions: Result<extractor::Output>,
config: Config,
files: Vec<String>,
variables: Option<VariableMap>,
) -> Result<()> {
let (key, tags, comment, snippet, file_index) = extractions.unwrap();

if key == "ctrl-o" {
edit::edit_file(Path::new(&files[file_index.expect("No files found")]))
.expect("Cound not open file in external editor");
.expect("Could not open file in external editor");
return Ok(());
}

Expand All @@ -193,12 +186,11 @@ pub fn act(
snippet,
tags,
variables.expect("No variables received from finder"),
&config,
)
.context("Failed to replace variables from snippet")?,
);

match config.action() {
match CONFIG.action() {
Action::Print => {
println!("{}", interpolated_snippet);
}
Expand Down
2 changes: 1 addition & 1 deletion src/bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ impl FileAnIssue {
}

fn main() -> Result<(), anyhow::Error> {
navi::handle_config(navi::config_from_env()).map_err(|e| FileAnIssue::new(e).into())
navi::handle().map_err(|e| FileAnIssue::new(e).into())
}
14 changes: 14 additions & 0 deletions src/cheat_variable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use crate::shell::{self, ShellSpawnError};

use anyhow::Result;

pub fn map_expand() -> Result<()> {
let cmd = r#"sed -e 's/^.*$/"&"/' | tr '\n' ' '"#;
shell::command()
.arg("-c")
.arg(cmd)
.spawn()
.map_err(|e| ShellSpawnError::new(cmd, e))?
.wait()?;
Ok(())
}
Loading

0 comments on commit 7fb2b53

Please sign in to comment.