feat: copyrat

This commit is contained in:
graelo 2020-05-24 21:02:11 +02:00
parent 0d45a2872a
commit 37f22b67af
11 changed files with 840 additions and 728 deletions

View file

@ -1,239 +1,159 @@
extern crate clap;
extern crate termion;
mod alphabets;
mod colors;
mod state;
mod view;
use self::clap::{App, Arg};
use clap::crate_version;
use clap::Clap;
use std::fs::OpenOptions;
use std::io::prelude::*;
use std::io::{self, Read};
use structopt::StructOpt;
use std::path;
// TODO: position as an enum ::Leading ::Trailing
mod alphabets;
mod colors;
mod error;
mod state;
mod view;
/// A lightning fast version copy/pasting like vimium/vimperator.
#[derive(StructOpt, Debug)]
#[structopt(name = "thumbs")]
/// Main configuration, parsed from command line.
#[derive(Clap, Debug)]
#[clap(author, about, version)]
struct Opt {
/// Sets the alphabet.
#[structopt(short, long, default_value = "qwerty")]
alphabet: String,
/// Alphabet to draw hints from.
///
/// Possible values are "{A}", "{A}-homerow", "{A}-left-hand",
/// "{A}-right-hand", where "{A}" is one of "qwerty", "azerty", "qwertz",
/// "dvorak", "colemak". Examples: "qwerty", "dvorak-homerow".
#[clap(short = "k", long, default_value = "qwerty",
parse(try_from_str = alphabets::parse_alphabet))]
alphabet: alphabets::Alphabet,
/// Sets the foreground color for matches.
#[structopt(long, default_value = "green")]
fg_color: String,
/// Enable multi-selection.
#[clap(short, long)]
multi_selection: bool,
/// Sets the background color for matches.
#[structopt(long, default_value = "black")]
bg_color: String,
#[clap(flatten)]
colors: view::ViewColors,
/// Reverse the order for assigned hints.
#[clap(short, long)]
reverse: bool,
/// Keep the same hint for identical matches.
#[clap(short, long)]
unique: bool,
/// Align hint with its match.
#[clap(short = "a", long, arg_enum, default_value = "Leading")]
hint_alignment: view::HintAlignment,
/// Additional regex patterns.
#[clap(short = "c", long)]
custom_regex: Vec<String>,
/// Optional hint styling.
///
/// Underline or surround the hint for increased visibility.
/// If not provided, only the hint colors will be used.
#[clap(short = "s", long, arg_enum)]
hint_style: Option<HintStyleCli>,
/// Chars surrounding each hint, used with `Surrounded` style.
#[clap(long, default_value = "{}",
parse(try_from_str = parse_chars))]
hint_surroundings: (char, char),
/// Target path where to store the selected matches.
#[clap(short = "o", long = "output", parse(from_os_str))]
target_path: Option<path::PathBuf>,
/// Only output if key was uppercased.
#[clap(long)]
uppercased: bool,
}
fn app_args<'a>() -> clap::ArgMatches<'a> {
App::new("thumbs")
.version(crate_version!())
.about("A lightning fast version copy/pasting like vimium/vimperator")
.arg(
Arg::with_name("alphabet")
.help("Sets the alphabet")
.long("alphabet")
.short("a")
.default_value("qwerty"),
)
.arg(
Arg::with_name("format")
.help("Specifies the out format for the picked hint. (%U: Upcase, %H: Hint)")
.long("format")
.short("f")
.default_value("%H"),
)
.arg(
Arg::with_name("foreground_color")
.help("Sets the foregroud color for matches")
.long("fg-color")
.default_value("green"),
)
.arg(
Arg::with_name("background_color")
.help("Sets the background color for matches")
.long("bg-color")
.default_value("black"),
)
.arg(
Arg::with_name("hint_foreground_color")
.help("Sets the foregroud color for hints")
.long("hint-fg-color")
.default_value("yellow"),
)
.arg(
Arg::with_name("hint_background_color")
.help("Sets the background color for hints")
.long("hint-bg-color")
.default_value("black"),
)
.arg(
Arg::with_name("select_foreground_color")
.help("Sets the foreground color for selection")
.long("select-fg-color")
.default_value("blue"),
)
.arg(
Arg::with_name("select_background_color")
.help("Sets the background color for selection")
.long("select-bg-color")
.default_value("black"),
)
.arg(
Arg::with_name("multi")
.help("Enable multi-selection")
.long("multi")
.short("m"),
)
.arg(
Arg::with_name("reverse")
.help("Reverse the order for assigned hints")
.long("reverse")
.short("r"),
)
.arg(
Arg::with_name("unique")
.help("Don't show duplicated hints for the same match")
.long("unique")
.short("u"),
)
.arg(
Arg::with_name("position")
.help("Hint position")
.long("position")
.default_value("left")
.short("p"),
)
.arg(
Arg::with_name("regexp")
.help("Use this regexp as extra pattern to match")
.long("regexp")
.short("x")
.takes_value(true)
.multiple(true),
)
.arg(
Arg::with_name("contrast")
.help("Put square brackets around hint for visibility")
.long("contrast")
.short("c"),
)
.arg(
Arg::with_name("target")
.help("Stores the hint in the specified path")
.long("target")
.short("t")
.takes_value(true),
)
.get_matches()
/// Type introduced due to parsing limitation,
/// as we cannot directly parse into view::HintStyle.
#[derive(Debug, Clap)]
enum HintStyleCli {
Underlined,
Surrounded,
}
fn parse_chars(src: &str) -> Result<(char, char), error::ParseError> {
if src.len() != 2 {
return Err(error::ParseError::ExpectedSurroundingPair);
}
let chars: Vec<char> = src.chars().collect();
Ok((chars[0], chars[1]))
}
fn main() {
let args = app_args();
let format = args.value_of("format").unwrap();
let alphabet = args.value_of("alphabet").unwrap();
let position = args.value_of("position").unwrap();
let target = args.value_of("target");
let multi = args.is_present("multi");
let reverse = args.is_present("reverse");
let unique = args.is_present("unique");
let contrast = args.is_present("contrast");
let regexp = if let Some(items) = args.values_of("regexp") {
items.collect::<Vec<_>>()
} else {
[].to_vec()
};
let opt = Opt::parse();
let foreground_color = colors::get_color(args.value_of("foreground_color").unwrap());
let background_color = colors::get_color(args.value_of("background_color").unwrap());
let hint_foreground_color = colors::get_color(args.value_of("hint_foreground_color").unwrap());
let hint_background_color = colors::get_color(args.value_of("hint_background_color").unwrap());
let select_foreground_color = colors::get_color(args.value_of("select_foreground_color").unwrap());
let select_background_color = colors::get_color(args.value_of("select_background_color").unwrap());
// Copy the pane contents (piped in via stdin) into a buffer, and split lines.
let stdin = io::stdin();
let mut handle = stdin.lock();
// Copy the pane contents (piped in via stdin) into a buffer, and split lines.
let mut buffer = String::new();
let stdin = io::stdin();
let mut handle = stdin.lock();
let mut buffer = String::new();
handle.read_to_string(&mut buffer).unwrap();
let lines: Vec<&str> = buffer.split('\n').collect();
handle.read_to_string(&mut buffer).unwrap();
let mut state = state::State::new(&lines, &opt.alphabet, &opt.custom_regex);
let lines: Vec<&str> = buffer.split('\n').collect();
let hint_style = match opt.hint_style {
None => None,
Some(style) => match style {
HintStyleCli::Underlined => Some(view::HintStyle::Underlined),
HintStyleCli::Surrounded => {
let (open, close) = opt.hint_surroundings;
Some(view::HintStyle::Surrounded(open, close))
}
},
};
let uppercase_flag = opt.uppercased;
let mut state = state::State::new(&lines, alphabet, &regexp);
let selections = {
let mut viewbox = view::View::new(
&mut state,
opt.multi_selection,
opt.reverse,
opt.unique,
opt.hint_alignment,
&opt.colors,
hint_style,
);
let hint_alignment = if position == "left" {
view::HintAlignment::Leading
} else {
view::HintAlignment::Trailing
};
viewbox.present()
};
let rendering_colors = view::ViewColors {
focus_fg: select_foreground_color,
focus_bg: select_background_color,
match_fg: foreground_color,
match_bg: background_color,
hint_fg: hint_foreground_color,
hint_bg: hint_background_color,
};
let hint_style = if contrast {
Some(view::HintStyle::Surrounded('[', ']'))
} else {
None
};
let selections = {
let mut viewbox = view::View::new(
&mut state,
multi,
reverse,
unique,
hint_alignment,
&rendering_colors,
hint_style,
);
viewbox.present()
};
// Early exit, signaling tmux we had no selections.
if selections.is_empty() {
::std::process::exit(1);
}
let output = selections
.iter()
.map(|(text, upcase)| {
let upcase_value = if *upcase { "true" } else { "false" };
let mut output = format.to_string();
output = str::replace(&output, "%U", upcase_value);
output = str::replace(&output, "%H", text.as_str());
output
})
.collect::<Vec<_>>()
.join("\n");
match target {
None => println!("{}", output),
Some(target) => {
let mut file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(target)
.expect("Unable to open the target file");
file.write(output.as_bytes()).unwrap();
// Early exit, signaling tmux we had no selections.
if selections.is_empty() {
::std::process::exit(1);
}
let output = selections
.iter()
.map(|(text, uppercased)| {
let upcase_value = if *uppercased { "true" } else { "false" };
let output = if uppercase_flag { upcase_value } else { text };
// let mut output = &opt.format;
// output = str::replace(&output, "%U", upcase_value);
// output = str::replace(&output, "%H", text.as_str());
output
})
.collect::<Vec<&str>>()
.join("\n");
match opt.target_path {
None => println!("{}", output),
Some(target) => {
let mut file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.open(target)
.expect("Unable to open the target file");
file.write(output.as_bytes()).unwrap();
}
}
}
}