-
Notifications
You must be signed in to change notification settings - Fork 521
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
599b0c9
commit d8d0d81
Showing
22 changed files
with
252 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
pub mod core; | ||
pub mod func; | ||
pub mod info; | ||
pub mod preview; | ||
pub mod preview_var; | ||
pub mod repo; | ||
pub mod shell; | ||
|
||
use crate::handler; | ||
use crate::structures::config::Command::{Fn, Info, Preview, PreviewVar, Repo, Widget}; | ||
use crate::structures::config::{Config, RepoCommand}; | ||
use anyhow::Context; | ||
use anyhow::Result; | ||
|
||
pub fn handle_config(config: Config) -> Result<()> { | ||
match config.cmd.as_ref() { | ||
None => handler::core::main(config), | ||
|
||
Some(c) => match c { | ||
Preview { line } => handler::preview::main(&line), | ||
|
||
PreviewVar { | ||
selection, | ||
query, | ||
variable, | ||
} => handler::preview_var::main(&selection, &query, &variable), | ||
|
||
Widget { shell } => handler::shell::main(shell).context("Failed to print shell widget code"), | ||
|
||
Fn { func, args } => handler::func::main(func, args.to_vec()) | ||
.with_context(|| format!("Failed to execute function `{:#?}`", func)), | ||
|
||
Info { info } => { | ||
handler::info::main(info).with_context(|| format!("Failed to fetch info `{:#?}`", info)) | ||
} | ||
|
||
Repo { cmd } => match cmd { | ||
RepoCommand::Add { uri } => { | ||
handler::repo::add(uri.clone(), &config.finder) | ||
.with_context(|| format!("Failed to import cheatsheets from `{}`", uri))?; | ||
handler::core::main(config) | ||
} | ||
RepoCommand::Browse => { | ||
handler::repo::browse(&config.finder).context("Failed to browse featured cheatsheets")?; | ||
handler::core::main(config) | ||
} | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
use crate::env_var; | ||
use crate::finder; | ||
|
||
use crate::terminal::style::style; | ||
use crate::ui; | ||
use crate::writer; | ||
use anyhow::Result; | ||
|
||
use std::collections::HashSet; | ||
use std::iter; | ||
use std::process; | ||
|
||
pub fn main(selection: &str, query: &str, variable: &str) -> Result<()> { | ||
let snippet = env_var::must_get(env_var::PREVIEW_INITIAL_SNIPPET); | ||
let tags = env_var::must_get(env_var::PREVIEW_TAGS); | ||
let comment = env_var::must_get(env_var::PREVIEW_COMMENT); | ||
let column = env_var::parse(env_var::PREVIEW_COLUMN); | ||
let delimiter = env_var::get(env_var::PREVIEW_DELIMITER).ok(); | ||
let map = env_var::get(env_var::PREVIEW_MAP).ok(); | ||
|
||
let active_color = *ui::TAG_COLOR; | ||
let inactive_color = *ui::COMMENT_COLOR; | ||
|
||
let mut colored_snippet = String::from(&snippet); | ||
let mut visited_vars: HashSet<&str> = HashSet::new(); | ||
|
||
let mut variables = String::from(""); | ||
|
||
println!( | ||
"{comment} {tags}", | ||
comment = style(comment).with(*ui::COMMENT_COLOR), | ||
tags = style(format!("[{}]", tags)).with(*ui::TAG_COLOR), | ||
); | ||
|
||
let bracketed_current_variable = format!("<{}>", variable); | ||
|
||
let bracketed_variables: Vec<&str> = { | ||
if snippet.contains(&bracketed_current_variable) { | ||
writer::VAR_REGEX | ||
.find_iter(&snippet) | ||
.map(|m| m.as_str()) | ||
.collect() | ||
} else { | ||
iter::once(&bracketed_current_variable) | ||
.map(|s| s.as_str()) | ||
.collect() | ||
} | ||
}; | ||
|
||
for bracketed_variable_name in bracketed_variables { | ||
let variable_name = &bracketed_variable_name[1..bracketed_variable_name.len() - 1]; | ||
|
||
if visited_vars.contains(variable_name) { | ||
continue; | ||
} else { | ||
visited_vars.insert(variable_name); | ||
} | ||
|
||
let is_current = variable_name == variable; | ||
let variable_color = if is_current { active_color } else { inactive_color }; | ||
let env_variable_name = env_var::escape(variable_name); | ||
|
||
let value = if is_current { | ||
let v = selection.trim_matches('\''); | ||
if v.is_empty() { query.trim_matches('\'') } else { v }.to_string() | ||
} else if let Ok(v) = env_var::get(&env_variable_name) { | ||
v | ||
} else { | ||
"".to_string() | ||
}; | ||
|
||
let replacement = format!( | ||
"{variable}", | ||
variable = style(bracketed_variable_name).with(variable_color), | ||
); | ||
|
||
colored_snippet = colored_snippet.replace(bracketed_variable_name, &replacement); | ||
|
||
variables = format!( | ||
"{variables}\n{variable} = {value}", | ||
variables = variables, | ||
variable = style(variable_name).with(variable_color), | ||
value = finder::process(value, column, delimiter.as_deref(), map.clone()) | ||
.expect("Unable to process value"), | ||
); | ||
} | ||
|
||
println!("{snippet}", snippet = writer::fix_newlines(&colored_snippet)); | ||
println!("{variables}", variables = variables); | ||
|
||
process::exit(0) | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.