Skip to content

Commit

Permalink
Rename display package
Browse files Browse the repository at this point in the history
  • Loading branch information
denisidoro authored Apr 5, 2021
1 parent b2816e9 commit 30c642e
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 49 deletions.
9 changes: 3 additions & 6 deletions src/actor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::clipboard;
use crate::display;
use crate::env_vars;
use crate::extractor;
use crate::writer;

use crate::finder::structures::{Opts as FinderOpts, SuggestionType};
use crate::finder::Finder;
Expand Down Expand Up @@ -133,10 +133,7 @@ fn replace_variables_from_snippet(
config: &Config,
) -> Result<String, Error> {
let mut interpolated_snippet = String::from(snippet);
let variables_found: Vec<&str> = display::VAR_REGEX
.find_iter(snippet)
.map(|m| m.as_str())
.collect();
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);

for bracketed_variable_name in variables_found {
Expand Down Expand Up @@ -187,7 +184,7 @@ pub fn act(
env::set_var(env_vars::PREVIEW_TAGS, &tags);
env::set_var(env_vars::PREVIEW_COMMENT, &comment);

let interpolated_snippet = display::with_new_lines(
let interpolated_snippet = writer::with_new_lines(
replace_variables_from_snippet(
snippet,
tags,
Expand Down
2 changes: 1 addition & 1 deletion src/cheatsh.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::display::Writer;
use crate::parser;
use crate::structures::cheat::VariableMap;
use crate::structures::fetcher;
use crate::writer::Writer;
use anyhow::Context;
use anyhow::Error;
use std::collections::HashSet;
Expand Down
16 changes: 8 additions & 8 deletions src/cmds/alfred.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::display;
use crate::filesystem;
use crate::shell::BashSpawnError;
use crate::structures::cheat::Suggestion;
use crate::structures::config::Config;
use crate::structures::fetcher::Fetcher;
use crate::writer;
use anyhow::Context;
use anyhow::Error;
use std::env;
Expand All @@ -15,9 +15,9 @@ pub fn main(config: Config) -> Result<(), Error> {
.spawn()
.context("Unable to create child")?;
let stdin = child.stdin.as_mut().context("Unable to get stdin")?;
let mut writer = display::alfred::Writer::new();
let mut writer = writer::alfred::Writer::new();

display::alfred::print_items_start(None);
writer::alfred::print_items_start(None);

let fetcher = filesystem::Fetcher::new(config.path);
fetcher
Expand All @@ -27,7 +27,7 @@ pub fn main(config: Config) -> Result<(), Error> {
// make sure everything was printed to stdout before attempting to close the items vector
let _ = child.wait_with_output().context("Failed to wait for fzf")?;

display::alfred::print_items_end();
writer::alfred::print_items_end();
Ok(())
}

Expand Down Expand Up @@ -59,7 +59,7 @@ pub fn suggestions(config: Config, dry_run: bool) -> Result<(), Error> {
.spawn()
.context("Unable to create child")?;
let stdin = child.stdin.as_mut().context("Unable to get stdin")?;
let mut writer = display::alfred::Writer::new();
let mut writer = writer::alfred::Writer::new();

let fetcher = filesystem::Fetcher::new(config.path);
let variables = fetcher
Expand All @@ -70,7 +70,7 @@ pub fn suggestions(config: Config, dry_run: bool) -> Result<(), Error> {
let tags = env::var("tags").context(r#"The env var "tags" isn't set"#)?;
let snippet = env::var("snippet").context(r#"The env var "snippet" isn't set"#)?;

let capture = display::VAR_REGEX.captures_iter(&snippet).next();
let capture = writer::VAR_REGEX.captures_iter(&snippet).next();
let bracketed_varname = &(capture.expect("Invalid capture"))[0];
let varname = &bracketed_varname[1..bracketed_varname.len() - 1];
let command = variables.get_suggestion(&tags, &varname);
Expand All @@ -82,7 +82,7 @@ pub fn suggestions(config: Config, dry_run: bool) -> Result<(), Error> {
return Ok(());
}

display::alfred::print_items_start(Some(varname));
writer::alfred::print_items_start(Some(varname));

let command = command.context("Invalid command")?;
let lines = prompt_finder(command).context("Invalid lines")?;
Expand All @@ -93,7 +93,7 @@ pub fn suggestions(config: Config, dry_run: bool) -> Result<(), Error> {
writer.write_suggestion(&snippet, &varname, &line);
}

display::alfred::print_items_end();
writer::alfred::print_items_end();

Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmds/core.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::actor;
use crate::cheatsh;

use crate::display;
use crate::writer;

use crate::extractor;
use crate::filesystem;
Expand Down Expand Up @@ -51,7 +51,7 @@ pub fn main(config: Config) -> Result<(), Error> {
let (raw_selection, variables) = config
.finder
.call(opts, &mut files, |stdin, files| {
let mut writer = display::terminal::Writer::new();
let mut writer = writer::terminal::Writer::new();

let fetcher: Box<dyn Fetcher> = match config.source() {
Source::Cheats(query) => Box::new(cheatsh::Fetcher::new(query)),
Expand Down
8 changes: 4 additions & 4 deletions src/cmds/preview.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::display;
use crate::writer;

use anyhow::Error;

use std::process;

fn extract_elements(argstr: &str) -> (&str, &str, &str) {
let mut parts = argstr.split(display::DELIMITER).skip(3);
let mut parts = argstr.split(writer::DELIMITER).skip(3);
let tags = parts.next().expect("No `tags` element provided.");
let comment = parts.next().expect("No `comment` element provided.");
let snippet = parts.next().expect("No `snippet` element provided.");
Expand All @@ -14,11 +14,11 @@ fn extract_elements(argstr: &str) -> (&str, &str, &str) {

pub fn main(line: &str) -> Result<(), Error> {
let (tags, comment, snippet) = extract_elements(line);
display::terminal::preview(comment, tags, snippet);
writer::terminal::preview(comment, tags, snippet);
process::exit(0)
}

pub fn main_var(selection: &str, query: &str, variable: &str) -> Result<(), Error> {
display::terminal::preview_var(selection, query, variable);
writer::terminal::preview_var(selection, query, variable);
process::exit(0)
}
4 changes: 2 additions & 2 deletions src/extractor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::display;
use crate::writer;

use anyhow::Context;
use anyhow::Error;
Expand All @@ -18,7 +18,7 @@ pub fn extract_from_selections(raw_snippet: &str, is_single: bool) -> Result<Out
let mut parts = lines
.next()
.context("No more parts in `selections`")?
.split(display::DELIMITER)
.split(writer::DELIMITER)
.skip(3);

let tags = parts.next().unwrap_or("");
Expand Down
6 changes: 3 additions & 3 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::display::Writer;
pub use crate::fs::{
create_dir, exe_string, pathbuf_to_string, read_lines, remove_dir, InvalidPath, UnreadableDir,
};
use crate::parser;
use crate::structures::cheat::VariableMap;
use crate::structures::fetcher;
use crate::writer::Writer;
use anyhow::Error;
use directories_next::BaseDirs;
use std::collections::HashSet;
Expand Down Expand Up @@ -111,8 +111,8 @@ pub fn read_all(
#[cfg(test)]
mod tests {
use super::*;
use crate::display;
use crate::finder::structures::{Opts as FinderOpts, SuggestionType};
use crate::writer;
use std::process::{Command, Stdio};

#[test]
Expand All @@ -126,7 +126,7 @@ mod tests {
.unwrap();
let child_stdin = child.stdin.as_mut().unwrap();
let mut visited_lines: HashSet<u64> = HashSet::new();
let mut writer: Box<dyn Writer> = Box::new(display::terminal::Writer::new());
let mut writer: Box<dyn Writer> = Box::new(writer::terminal::Writer::new());
read_file(
path,
0,
Expand Down
4 changes: 2 additions & 2 deletions src/finder/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::display;
use crate::structures::cheat::VariableMap;
use crate::writer;
use anyhow::Context;
use anyhow::Error;
use std::process::{self, Output};
Expand Down Expand Up @@ -83,7 +83,7 @@ impl Finder for FinderChoice {
"--with-nth",
"1,2,3",
"--delimiter",
display::DELIMITER.to_string().as_str(),
writer::DELIMITER.to_string().as_str(),
"--ansi",
"--bind",
format!("ctrl-j:down,ctrl-k:up{}", bindings).as_str(),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod actor;
mod cheatsh;
mod clipboard;
mod cmds;
mod display;
mod env_vars;
mod extractor;
mod filesystem;
Expand All @@ -23,6 +22,7 @@ mod terminal;
mod tldr;
mod url;
mod welcome;
mod writer;

pub use handler::handle_config;
pub use structures::config::{config_from_env, config_from_iter};
4 changes: 2 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::display::{self, Writer};
use crate::finder::structures::{Opts as FinderOpts, SuggestionType};
use crate::hash::fnv;
use crate::structures::cheat::VariableMap;
use crate::structures::item::Item;
use crate::writer::{self, Writer};
use anyhow::{Context, Error};
use regex::Regex;
use std::collections::HashSet;
Expand Down Expand Up @@ -202,7 +202,7 @@ pub fn read_lines(
visited_lines.insert(hash);

if !(&snippet).is_empty() {
snippet.push_str(display::LINE_SEPARATOR);
snippet.push_str(writer::LINE_SEPARATOR);
}
snippet.push_str(&line);
}
Expand Down
2 changes: 1 addition & 1 deletion src/structures/fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::display::Writer;
use crate::structures::cheat::VariableMap;
use crate::writer::Writer;
use anyhow::Error;

pub trait Fetcher {
Expand Down
2 changes: 1 addition & 1 deletion src/tldr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::display::Writer;
use crate::parser;
use crate::structures::cheat::VariableMap;
use crate::structures::fetcher;
use crate::writer::Writer;
use anyhow::{Context, Error};
use regex::Regex;
use std::collections::HashSet;
Expand Down
2 changes: 1 addition & 1 deletion src/welcome.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::display::Writer;
use crate::structures::item::Item;
use crate::writer::Writer;
use std::io::Write;

fn add_msg(
Expand Down
6 changes: 3 additions & 3 deletions src/display/alfred.rs → src/writer/alfred.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::display;
use crate::structures::item::Item;
use crate::writer;

pub struct Writer {
is_first: bool,
Expand All @@ -8,7 +8,7 @@ pub struct Writer {
fn escape_for_json(txt: &str) -> String {
txt.replace('\\', "\\\\")
.replace('"', "“")
.replace(display::NEWLINE_ESCAPE_CHAR, " ")
.replace(writer::NEWLINE_ESCAPE_CHAR, " ")
}

pub fn print_items_start(varname: Option<&str>) {
Expand All @@ -25,7 +25,7 @@ pub fn print_items_end() {
println!(r#"]}}"#);
}

impl display::Writer for Writer {
impl writer::Writer for Writer {
fn write(&mut self, item: Item) -> String {
let prefix = if self.is_first {
self.is_first = false;
Expand Down
File renamed without changes.
21 changes: 9 additions & 12 deletions src/display/terminal.rs → src/writer/terminal.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::display;
use crate::env_vars;
use crate::finder;
use crate::structures::item::Item;
use crate::terminal::{self, color};
use crate::writer;
use std::cmp::max;
use std::collections::HashSet;
use std::env;
Expand Down Expand Up @@ -34,7 +34,7 @@ pub fn preview(comment: &str, tags: &str, snippet: &str) {
"{comment_color}{comment} {tag_color}{tags} \n{snippet_color}{snippet}",
comment = comment.to_string(),
tags = format!("[{}]", tags),
snippet = display::fix_newlines(snippet),
snippet = writer::fix_newlines(snippet),
comment_color = color::Fg(*COMMENT_COLOR),
tag_color = color::Fg(*TAG_COLOR),
snippet_color = color::Fg(*SNIPPET_COLOR),
Expand All @@ -53,7 +53,7 @@ pub fn preview_var(selection: &str, query: &str, variable: &str) {
let snippet = &get_env_var(env_vars::PREVIEW_INITIAL_SNIPPET);
let tags = get_env_var(env_vars::PREVIEW_TAGS);
let comment = get_env_var(env_vars::PREVIEW_COMMENT);
let column = display::terminal::parse_env_var(env_vars::PREVIEW_COLUMN);
let column = writer::terminal::parse_env_var(env_vars::PREVIEW_COLUMN);
let delimiter = env::var(env_vars::PREVIEW_DELIMITER).ok();
let map = env::var(env_vars::PREVIEW_MAP).ok();

Expand All @@ -79,10 +79,7 @@ pub fn preview_var(selection: &str, query: &str, variable: &str) {

let bracketed_variables: Vec<&str> = {
if snippet.contains(&bracketed_current_variable) {
display::VAR_REGEX
.find_iter(snippet)
.map(|m| m.as_str())
.collect()
writer::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 @@ -132,7 +129,7 @@ pub fn preview_var(selection: &str, query: &str, variable: &str) {
);
}

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

Expand All @@ -159,26 +156,26 @@ pub struct Writer {
impl Writer {
pub fn new() -> Writer {
let (tag_width, comment_width) = get_widths();
display::terminal::Writer {
writer::terminal::Writer {
tag_width,
comment_width,
}
}
}

impl display::Writer for Writer {
impl writer::Writer for Writer {
fn write(&mut self, item: Item) -> String {
format!(
"{tag_color}{tags_short}{delimiter}{comment_color}{comment_short}{delimiter}{snippet_color}{snippet_short}{delimiter}{tags}{delimiter}{comment}{delimiter}{snippet}{delimiter}{file_index}{delimiter}\n",
tags_short = limit_str(item.tags, self.tag_width),
comment_short = limit_str(item.comment, self.comment_width),
snippet_short = display::fix_newlines(item.snippet),
snippet_short = writer::fix_newlines(item.snippet),
comment_color = color::Fg(*COMMENT_COLOR),
tag_color = color::Fg(*TAG_COLOR),
snippet_color = color::Fg(*SNIPPET_COLOR),
tags = item.tags,
comment = item.comment,
delimiter = display::DELIMITER,
delimiter = writer::DELIMITER,
snippet = &item.snippet,
file_index = item.file_index,
)
Expand Down

0 comments on commit 30c642e

Please sign in to comment.