Skip to content

Commit

Permalink
Minor code base improvements (#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro committed Jul 29, 2022
1 parent 8b78d54 commit 9d86234
Show file tree
Hide file tree
Showing 19 changed files with 242 additions and 233 deletions.
2 changes: 1 addition & 1 deletion src/clients/tldr.rs
@@ -1,5 +1,5 @@
use crate::prelude::*;
use std::process::{self, Command, Stdio};
use std::process::{Command, Stdio};

lazy_static! {
pub static ref VAR_TLDR_REGEX: Regex = Regex::new(r"\{\{(.*?)\}\}").expect("Invalid regex");
Expand Down
28 changes: 17 additions & 11 deletions src/commands/core/actor.rs
@@ -1,14 +1,14 @@
use super::extractor;
use crate::common::clipboard;
use crate::common::fs;
use crate::common::shell;
use crate::common::shell::ShellSpawnError;
use crate::config::Action;
use crate::deser;
use crate::env_var;
use crate::finder::structures::{Opts as FinderOpts, SuggestionType};
use crate::prelude::*;
use crate::serializer;
use crate::structures::cheat::{Suggestion, VariableMap};
use crate::structures::item::Item;
use shell::EOF;
use std::process::Stdio;

Expand Down Expand Up @@ -145,10 +145,7 @@ fn unique_result_count(results: &[&str]) -> usize {

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> = serializer::VAR_REGEX
.find_iter(snippet)
.map(|m| m.as_str())
.collect();
let variables_found: Vec<&str> = deser::VAR_REGEX.find_iter(snippet).map(|m| m.as_str()).collect();
let variable_count = unique_result_count(&variables_found);

for bracketed_variable_name in variables_found {
Expand Down Expand Up @@ -187,11 +184,20 @@ pub fn with_absolute_path(snippet: String) -> String {
}

pub fn act(
extractions: Result<extractor::Output>,
extractions: Result<(&str, Item)>,
files: Vec<String>,
variables: Option<VariableMap>,
) -> Result<()> {
let (key, tags, comment, snippet, file_index) = extractions.unwrap();
let (
key,
Item {
tags,
comment,
snippet,
file_index,
..
},
) = extractions.unwrap();

if key == "ctrl-o" {
edit::edit_file(Path::new(&files[file_index.expect("No files found")]))
Expand All @@ -205,13 +211,13 @@ pub fn act(

let interpolated_snippet = {
let mut s = replace_variables_from_snippet(
snippet,
tags,
&snippet,
&tags,
variables.expect("No variables received from finder"),
)
.context("Failed to replace variables from snippet")?;
s = with_absolute_path(s);
s = serializer::with_new_lines(s);
s = deser::with_new_lines(s);
s
};

Expand Down
27 changes: 0 additions & 27 deletions src/commands/core/extractor.rs

This file was deleted.

8 changes: 4 additions & 4 deletions src/commands/core/mod.rs
@@ -1,8 +1,8 @@
mod actor;
mod extractor;

use crate::clients::cheatsh;
use crate::clients::{cheatsh, tldr};
use crate::config::Source;
use crate::deser;
use crate::filesystem;
use crate::finder::structures::Opts as FinderOpts;
use crate::parser::Parser;
Expand Down Expand Up @@ -32,7 +32,7 @@ pub fn init(fetcher: Box<dyn Fetcher>) -> Result<()> {
})
.context("Failed getting selection and variables from finder")?;

let extractions = extractor::extract_from_selections(&raw_selection, config.best_match());
let extractions = deser::terminal::read(&raw_selection, config.best_match());

if extractions.is_err() {
return init(fetcher);
Expand All @@ -51,7 +51,7 @@ pub fn get_fetcher() -> Result<Box<dyn Fetcher>> {
Ok(fetcher)
}
Source::Tldr(query) => {
let lines = cheatsh::call(&query)?;
let lines = tldr::call(&query)?;
let fetcher = Box::new(StaticFetcher::new(lines));
Ok(fetcher)
}
Expand Down
11 changes: 9 additions & 2 deletions src/commands/func/mod.rs
Expand Up @@ -8,7 +8,7 @@ use crate::prelude::*;
use clap::Args;
use clap::Parser;

const FUNC_POSSIBLE_VALUES: &[&str] = &[
const POSSIBLE_VALUES: &[&str] = &[
"url::open",
"welcome",
"widget::last_command",
Expand Down Expand Up @@ -43,7 +43,7 @@ pub enum Func {
#[derive(Debug, Clone, Args)]
pub struct Input {
/// Function name (example: "url::open")
#[clap(possible_values = FUNC_POSSIBLE_VALUES, ignore_case = true)]
#[clap(possible_values = POSSIBLE_VALUES, ignore_case = true)]
pub func: Func,
/// List of arguments (example: "https://google.com")
pub args: Vec<String>,
Expand All @@ -63,3 +63,10 @@ impl Runnable for Input {
}
}
}

#[test]
fn test_possible_values() {
for v in POSSIBLE_VALUES {
assert!(Func::from_str(v).is_ok())
}
}
11 changes: 9 additions & 2 deletions src/commands/info.rs
Expand Up @@ -3,7 +3,7 @@ use clap::Args;
use crate::filesystem;
use crate::prelude::*;

const INFO_POSSIBLE_VALUES: &[&str] = &["cheats-example", "cheats-path", "config-path", "config-example"];
const POSSIBLE_VALUES: &[&str] = &["cheats-example", "cheats-path", "config-path", "config-example"];

impl FromStr for Info {
type Err = &'static str;
Expand All @@ -21,7 +21,7 @@ impl FromStr for Info {

#[derive(Debug, Clone, Args)]
pub struct Input {
#[clap(possible_values = INFO_POSSIBLE_VALUES, ignore_case = true)]
#[clap(possible_values = POSSIBLE_VALUES, ignore_case = true)]
pub info: Info,
}

Expand All @@ -46,3 +46,10 @@ impl Runnable for Input {
Ok(())
}
}

#[test]
fn test_possible_values() {
for v in POSSIBLE_VALUES {
assert!(Info::from_str(v).is_ok())
}
}
6 changes: 3 additions & 3 deletions src/commands/preview/mod.rs
@@ -1,5 +1,5 @@
use crate::deser;
use crate::prelude::*;
use crate::serializer;
use clap::Args;
use crossterm::style::{style, Stylize};
use std::process;
Expand All @@ -14,7 +14,7 @@ pub struct Input {
}

fn extract_elements(argstr: &str) -> Result<(&str, &str, &str)> {
let mut parts = argstr.split(serializer::DELIMITER).skip(3);
let mut parts = argstr.split(deser::terminal::DELIMITER).skip(3);
let tags = parts.next().context("No `tags` element provided.")?;
let comment = parts.next().context("No `comment` element provided.")?;
let snippet = parts.next().context("No `snippet` element provided.")?;
Expand All @@ -31,7 +31,7 @@ impl Runnable for Input {
"{comment} {tags} \n{snippet}",
comment = style(comment).with(CONFIG.comment_color()),
tags = style(format!("[{}]", tags)).with(CONFIG.tag_color()),
snippet = style(serializer::fix_newlines(snippet)).with(CONFIG.snippet_color()),
snippet = style(deser::fix_newlines(snippet)).with(CONFIG.snippet_color()),
);

process::exit(0)
Expand Down
9 changes: 3 additions & 6 deletions src/commands/preview/var.rs
@@ -1,7 +1,7 @@
use crate::deser;
use crate::env_var;
use crate::finder;
use crate::prelude::*;
use crate::serializer;
use clap::Args;
use crossterm::style::style;
use crossterm::style::Stylize;
Expand Down Expand Up @@ -49,10 +49,7 @@ impl Runnable for Input {

let bracketed_variables: Vec<&str> = {
if snippet.contains(&bracketed_current_variable) {
serializer::VAR_REGEX
.find_iter(&snippet)
.map(|m| m.as_str())
.collect()
deser::VAR_REGEX.find_iter(&snippet).map(|m| m.as_str()).collect()
} else {
iter::once(&bracketed_current_variable)
.map(|s| s.as_str())
Expand Down Expand Up @@ -98,7 +95,7 @@ impl Runnable for Input {
);
}

println!("{snippet}", snippet = serializer::fix_newlines(&colored_snippet));
println!("{snippet}", snippet = deser::fix_newlines(&colored_snippet));
println!("{variables}", variables = variables);

process::exit(0)
Expand Down
11 changes: 9 additions & 2 deletions src/commands/shell.rs
Expand Up @@ -3,7 +3,7 @@ use clap::Args;
use crate::common::shell::Shell;
use crate::prelude::*;

const WIDGET_POSSIBLE_VALUES: &[&str] = &["bash", "zsh", "fish", "elvish"];
const POSSIBLE_VALUES: &[&str] = &["bash", "zsh", "fish", "elvish"];

impl FromStr for Shell {
type Err = &'static str;
Expand All @@ -21,7 +21,7 @@ impl FromStr for Shell {

#[derive(Debug, Clone, Args)]
pub struct Input {
#[clap(possible_values = WIDGET_POSSIBLE_VALUES, ignore_case = true, default_value = "bash")]
#[clap(possible_values = POSSIBLE_VALUES, ignore_case = true, default_value = "bash")]
pub shell: Shell,
}

Expand All @@ -41,3 +41,10 @@ impl Runnable for Input {
Ok(())
}
}

#[test]
fn test_possible_values() {
for v in POSSIBLE_VALUES {
assert!(Shell::from_str(v).is_ok())
}
}
8 changes: 4 additions & 4 deletions src/commands/temp.rs
Expand Up @@ -2,11 +2,11 @@ use crate::commands::core::get_fetcher;
use crate::common::shell::{self, ShellSpawnError};
use crate::finder::structures::Opts as FinderOpts;
use crate::parser::Parser;
use crate::{prelude::*, serializer};
use crate::{deser, prelude::*};
use std::io::{self, Write};

pub fn main() -> Result<()> {
let config = &CONFIG;
let _config = &CONFIG;
let _opts = FinderOpts::snippet_default();

let fetcher = get_fetcher()?;
Expand All @@ -22,7 +22,7 @@ pub fn main() -> Result<()> {

let variables = parser.variables;
let item_str = String::from_utf8(buf)?;
let item = serializer::raycast_deser(&item_str)?;
let item = deser::raycast::read(&item_str)?;
dbg!(&item);

let x = variables.get_suggestion(&item.tags, "local_branch").expect("foo");
Expand All @@ -49,7 +49,7 @@ pub fn main() -> Result<()> {
}

pub fn _main0() -> Result<()> {
let config = &CONFIG;
let _config = &CONFIG;

let fetcher = get_fetcher()?;

Expand Down
41 changes: 2 additions & 39 deletions src/config/cli.rs
@@ -1,10 +1,8 @@
use crate::commands;
use crate::finder::FinderChoice;
use crate::finder::{self, FinderChoice};
use crate::prelude::*;
use clap::{crate_version, AppSettings, Parser, Subcommand};

const FINDER_POSSIBLE_VALUES: &[&str] = &["fzf", "skim"];

#[derive(Debug, Parser)]
#[clap(after_help = "\x1b[0;33mMORE INFO:\x1b[0;0m
Please refer to \x1b[0;32mhttps://github.com/denisidoro/navi\x1b[0;0m
Expand Down Expand Up @@ -80,7 +78,7 @@ pub(super) struct ClapConfig {
pub fzf_overrides_var: Option<String>,

/// Finder application to use
#[clap(long, possible_values = FINDER_POSSIBLE_VALUES, ignore_case = true)]
#[clap(long, possible_values = finder::POSSIBLE_VALUES, ignore_case = true)]
pub finder: Option<FinderChoice>,

#[clap(subcommand)]
Expand Down Expand Up @@ -127,38 +125,3 @@ pub enum Action {
Print,
Execute,
}

/*
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_widget_possible_values() {
for v in WIDGET_POSSIBLE_VALUES {
assert!(Shell::from_str(v).is_ok())
}
}
#[test]
fn test_info_possible_values() {
for v in INFO_POSSIBLE_VALUES {
assert!(Info::from_str(v).is_ok())
}
}
#[test]
fn test_func_possible_values() {
for v in FUNC_POSSIBLE_VALUES {
assert!(Func::from_str(v).is_ok())
}
}
#[test]
fn test_finder_possible_values() {
for v in FINDER_POSSIBLE_VALUES {
assert!(FinderChoice::from_str(v).is_ok())
}
}
}
*/

0 comments on commit 9d86234

Please sign in to comment.