refactor: refactor

This commit is contained in:
graelo 2020-05-27 10:04:42 +02:00
parent 623c66cbba
commit 791aaadd49
10 changed files with 655 additions and 468 deletions

View file

@ -19,4 +19,4 @@ path = "src/main.rs"
[[bin]]
name = "tmux-copyrat"
path = "src/swapper.rs"
path = "src/bridge.rs"

View file

@ -1,10 +1,11 @@
use clap::Clap;
use regex::Regex;
use std::path;
use std::collections::HashMap;
use std::process::Command;
use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH};
use copyrat::error;
use copyrat::{error, process, CliOpt};
mod tmux;
@ -46,7 +47,7 @@ const TMP_FILE: &str = "/tmp/copyrat-last";
pub struct Swapper<'a> {
executor: Box<&'a mut dyn Executor>,
directory: &'a path::Path,
// directory: &'a path::Path,
command: &'a str,
alt_command: &'a str,
active_pane_id: Option<String>,
@ -61,7 +62,7 @@ pub struct Swapper<'a> {
impl<'a> Swapper<'a> {
fn new(
executor: Box<&'a mut dyn Executor>,
directory: &'a path::Path,
// directory: &'a path::Path,
command: &'a str,
alt_command: &'a str,
) -> Swapper<'a> {
@ -72,7 +73,7 @@ impl<'a> Swapper<'a> {
Swapper {
executor,
directory,
// directory,
command,
alt_command,
active_pane_id: None,
@ -198,10 +199,10 @@ impl<'a> Swapper<'a> {
// NOTE: For debugging add echo $PWD && sleep 5 after tee
let pane_command = format!(
"tmux capture-pane -t {active_id} -p{scroll_params} | {dir}/target/release/thumbs -f '%U:%H' -t {tmpfile} {args}; tmux swap-pane -t {active_id}; tmux wait-for -S {signal}",
"tmux capture-pane -t {active_id} -p{scroll_params} | target/release/thumbs -f '%U:%H' -t {tmpfile} {args}; tmux swap-pane -t {active_id}; tmux wait-for -S {signal}",
active_id = active_pane_id,
scroll_params = scroll_params,
dir = self.directory.to_str().unwrap(),
// dir = self.directory.to_str().unwrap(),
tmpfile = TMP_FILE,
args = args.join(" "),
signal = self.signal
@ -317,7 +318,7 @@ mod tests {
let last_command_outputs =
vec!["%97:100:24:1:active\n%106:100:24:1:nope\n%107:100:24:1:nope\n".to_string()];
let mut executor = TestShell::new(last_command_outputs);
let mut swapper = Swapper::new(Box::new(&mut executor), &path::Path::new(""), "", "");
let mut swapper = Swapper::new(Box::new(&mut executor), "", "");
swapper.capture_active_pane();
@ -333,7 +334,7 @@ mod tests {
"%106:100:24:1:nope\n%98:100:24:1:active\n%107:100:24:1:nope\n".to_string(),
];
let mut executor = TestShell::new(last_command_outputs);
let mut swapper = Swapper::new(Box::new(&mut executor), &path::Path::new(""), "", "");
let mut swapper = Swapper::new(Box::new(&mut executor), "", "");
swapper.capture_active_pane();
swapper.execute_thumbs();
@ -348,21 +349,13 @@ mod tests {
/// Main configuration, parsed from command line.
#[derive(Clap, Debug)]
#[clap(author, about, version)]
struct Opt {
/// Directory where to execute copyrat.
#[clap(long, required = true)]
directory: path::PathBuf,
struct BridgeOpt {
/// Command to execute on selection.
#[clap(short, long, default_value = "'tmux set-buffer {}'")]
#[clap(short, long, default_value = "tmux set-buffer {}")]
command: String,
/// Command to execute on alt selection.
#[clap(
short,
long,
default_value = "'tmux set-buffer {} && tmux-paste-buffer'"
)]
/// Command to execute on uppercased selection.
#[clap(short, long, default_value = "tmux set-buffer {} && tmux-paste-buffer")]
alt_command: String,
/// Retrieve options from tmux.
@ -372,22 +365,54 @@ struct Opt {
/// option) as this saves both a command call (about 10ms) and a Regex
/// compilation.
#[clap(long)]
options_from_tmux: bool,
get_options_from_tmux: bool,
/// Optionally capture entire pane history.
#[clap(long, arg_enum, default_value = "entire-history")]
capture: tmux::CaptureRegion,
// Include CLI Options
#[clap(flatten)]
cli_options: CliOpt,
}
impl BridgeOpt {
/// Try parsing provided options, and update self with the valid values.
pub fn merge_map(
&mut self,
options: &HashMap<String, String>,
) -> Result<(), error::ParseError> {
for (name, value) in options {
match name.as_ref() {
"@copyrat-command" => {
self.command = String::from(value);
}
"@copyrat-alt-command" => {
self.alt_command = String::from(value);
}
"@copyrat-capture" => {
self.capture = tmux::CaptureRegion::from_str(&value)?;
}
// Ignore unknown options.
_ => (),
}
}
// Pass the call to cli_options.
self.cli_options.merge_map(options)?;
Ok(())
}
}
fn main() -> Result<(), error::ParseError> {
let opt = Opt::parse();
// let dir = args.value_of("dir").unwrap();
// let command = args.value_of("command").unwrap();
// let upcase_command = args.value_of("upcase_command").unwrap();
let mut opt = BridgeOpt::parse();
// if dir.is_empty() {
// panic!("Invalid tmux-thumbs execution. Are you trying to execute tmux-thumbs directly?")
// }
if opt.get_options_from_tmux {
let tmux_options = tmux::get_options("@copyrat-")?;
opt.merge_map(&tmux_options)?;
}
let panes: Vec<tmux::Pane> = tmux::list_panes()?;
let active_pane = panes
@ -395,13 +420,34 @@ fn main() -> Result<(), error::ParseError> {
.find(|p| p.is_active)
.expect("One tmux pane should be active");
let content = tmux::capture_pane(&active_pane, &opt.capture)?;
let buffer = tmux::capture_pane(&active_pane, &opt.capture)?;
let selections: Vec<(String, bool)> = if active_pane.in_mode {
// TODO: fancy stuff
vec![(String::new(), false)]
} else {
copyrat::run(buffer, &opt.cli_options)
};
selections.iter().for_each(|(text, b)| {
let command = if *b {
opt.alt_command.replace("{}", text)
} else {
opt.command.replace("{}", text)
};
let args: Vec<&str> = vec![];
// Simply execute the command as is and don't mind about potential
// errors.
process::execute(&command, &args).unwrap();
});
if false {
let mut executor = RealShell::new();
let mut swapper = Swapper::new(
Box::new(&mut executor),
opt.directory.as_path(),
// opt.directory.as_path(),
&opt.command,
&opt.alt_command,
);
@ -413,5 +459,7 @@ fn main() -> Result<(), error::ParseError> {
swapper.retrieve_content();
swapper.destroy_content();
swapper.execute_command();
}
Ok(())
}

View file

@ -8,6 +8,7 @@ pub enum ParseError {
ExpectedPaneIdMarker,
ExpectedInt(std::num::ParseIntError),
ExpectedBool(std::str::ParseBoolError),
ExpectedString(String),
ProcessFailure(String),
}
@ -22,6 +23,7 @@ impl fmt::Display for ParseError {
ParseError::ExpectedPaneIdMarker => write!(f, "Expected pane id marker"),
ParseError::ExpectedInt(msg) => write!(f, "Expected an int: {}", msg),
ParseError::ExpectedBool(msg) => write!(f, "Expected a bool: {}", msg),
ParseError::ExpectedString(msg) => write!(f, "Expected {}", msg),
ParseError::ProcessFailure(msg) => write!(f, "{}", msg),
}
}

View file

@ -1,9 +1,13 @@
use clap::Clap;
use std::collections::HashMap;
use std::path;
use std::str::FromStr;
pub mod alphabets;
pub mod colors;
pub mod error;
pub mod process;
pub mod regexes;
pub mod state;
pub mod view;
@ -11,8 +15,8 @@ pub mod view;
///
/// # Note
///
/// Maybe the decision to move ownership is a bit bold.
pub fn run(buffer: String, opt: &Opt) -> String {
/// Maybe the decision to take ownership of the buffer is a bit bold.
pub fn run(buffer: String, opt: &CliOpt) -> Vec<(String, bool)> {
let lines: Vec<&str> = buffer.split('\n').collect();
let mut state = state::State::new(&lines, &opt.alphabet, &opt.custom_regex);
@ -27,7 +31,6 @@ pub fn run(buffer: String, opt: &Opt) -> String {
}
},
};
let uppercased_marker = opt.uppercased_marker;
let selections: Vec<(String, bool)> = {
let mut viewbox = view::View::new(
@ -43,32 +46,13 @@ pub fn run(buffer: String, opt: &Opt) -> String {
viewbox.present()
};
// Early exit, signaling tmux we had no selections.
if selections.is_empty() {
std::process::exit(1);
}
let output: String = if uppercased_marker {
selections
.iter()
.map(|(text, uppercased)| format!("{}:{}", *uppercased, text))
.collect::<Vec<String>>()
.join("\n")
} else {
selections
.iter()
.map(|(text, _)| text.as_str())
.collect::<Vec<&str>>()
.join("\n")
};
output
}
/// Main configuration, parsed from command line.
#[derive(Clap, Debug)]
#[clap(author, about, version)]
pub struct Opt {
pub struct CliOpt {
/// Alphabet to draw hints from.
///
/// Possible values are "{A}", "{A}-homerow", "{A}-left-hand",
@ -78,13 +62,20 @@ pub struct Opt {
parse(try_from_str = alphabets::parse_alphabet))]
alphabet: alphabets::Alphabet,
// /// Which existing regexes to use.
// #[clap(short = "x", long, arg_enum)]
// regex_id: Vec<regexes::RegexId>,
// TODO: choose if pre-baked regexes is a good idea
// TODO: check if compiled regexes are possible
/// Additional regex patterns.
#[clap(short = "X", long)]
custom_regex: Vec<String>,
/// Enable multi-selection.
#[clap(short, long)]
multi_selection: bool,
#[clap(flatten)]
colors: view::ViewColors,
/// Reverse the order for assigned hints.
#[clap(short, long)]
reverse: bool,
@ -93,14 +84,13 @@ pub struct Opt {
#[clap(short, long)]
unique_hint: bool,
#[clap(flatten)]
colors: view::ViewColors,
/// 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.
@ -121,7 +111,7 @@ pub struct Opt {
/// indicating if hint key was uppercased. This is only used by
/// tmux-copyrat, so it is hidden (skipped) from the CLI.
#[clap(skip)]
uppercased_marker: bool,
pub uppercased_marker: bool,
}
/// Type introduced due to parsing limitation,
@ -132,6 +122,20 @@ enum HintStyleCli {
Surround,
}
impl FromStr for HintStyleCli {
type Err = error::ParseError;
fn from_str(s: &str) -> Result<Self, error::ParseError> {
match s {
"leading" => Ok(HintStyleCli::Underline),
"trailing" => Ok(HintStyleCli::Surround),
_ => Err(error::ParseError::ExpectedString(String::from(
"underline or surround",
))),
}
}
}
fn parse_chars(src: &str) -> Result<(char, char), error::ParseError> {
if src.len() != 2 {
return Err(error::ParseError::ExpectedSurroundingPair);
@ -140,3 +144,47 @@ fn parse_chars(src: &str) -> Result<(char, char), error::ParseError> {
let chars: Vec<char> = src.chars().collect();
Ok((chars[0], chars[1]))
}
impl CliOpt {
/// Try parsing provided options, and update self with the valid values.
pub fn merge_map(
&mut self,
options: &HashMap<String, String>,
) -> Result<(), error::ParseError> {
for (name, value) in options {
match name.as_ref() {
"@copyrat-alphabet" => {
self.alphabet = alphabets::parse_alphabet(value)?;
}
"@copyrat-regex-id" => (), // TODO
"@copyrat-custom-regex" => self.custom_regex = vec![String::from(value)],
"@copyrat-multi-selection" => {
self.multi_selection = value.parse::<bool>()?;
}
"@copyrat-reverse" => {
self.reverse = value.parse::<bool>()?;
}
"@copyrat-unique-hint" => {
self.unique_hint = value.parse::<bool>()?;
}
"@copyrat-match-fg" => self.colors.match_fg = colors::parse_color(value)?,
"@copyrat-match-bg" => self.colors.match_bg = colors::parse_color(value)?,
"@copyrat-focused-fg" => self.colors.focused_fg = colors::parse_color(value)?,
"@copyrat-focused-bg" => self.colors.focused_bg = colors::parse_color(value)?,
"@copyrat-hint-fg" => self.colors.hint_fg = colors::parse_color(value)?,
"@copyrat-hint-bg" => self.colors.hint_bg = colors::parse_color(value)?,
"@copyrat-hint-alignment" => {
self.hint_alignment = view::HintAlignment::from_str(&value)?
}
"@copyrat-hint-style" => self.hint_style = Some(HintStyleCli::from_str(&value)?),
// Ignore unknown options.
_ => (),
}
}
Ok(())
}
}

View file

@ -3,10 +3,10 @@ use std::fs::OpenOptions;
use std::io::prelude::*;
use std::io::{self, Read};
use copyrat::{run, Opt};
use copyrat::{run, CliOpt};
fn main() {
let opt = Opt::parse();
let opt = CliOpt::parse();
// Copy the pane contents (piped in via stdin) into a buffer, and split lines.
let stdin = io::stdin();
@ -17,7 +17,18 @@ fn main() {
// Execute copyrat over the buffer (will take control over stdout).
// This returns the selected matches.
let output: String = run(buffer, &opt);
let selections: Vec<(String, bool)> = run(buffer, &opt);
// Early exit, signaling no selections were found.
if selections.is_empty() {
std::process::exit(1);
}
let output = selections
.iter()
.map(|(text, _)| text.as_str())
.collect::<Vec<&str>>()
.join("\n");
// Write output to a target_path if provided, else print to original stdout.
match opt.target_path {

21
src/process.rs Normal file
View file

@ -0,0 +1,21 @@
use std::process::Command;
use crate::error::ParseError;
/// Execute an arbitrary Unix command and return the stdout as a `String` if
/// successful.
pub fn execute(command: &str, args: &Vec<&str>) -> Result<String, ParseError> {
let output = Command::new(command).args(args).output()?;
if !output.status.success() {
let msg = String::from_utf8_lossy(&output.stderr);
return Err(ParseError::ProcessFailure(format!(
"Process failure: {} {}, error {}",
command,
args.join(" "),
msg
)));
}
Ok(String::from_utf8_lossy(&output.stdout).to_string())
}

25
src/regexes.rs Normal file
View file

@ -0,0 +1,25 @@
pub const EXCLUDE_PATTERNS: [(&'static str, &'static str); 1] =
[("bash", r"[[:cntrl:]]\[([0-9]{1,2};)?([0-9]{1,2})?m")];
pub const PATTERNS: [(&'static str, &'static str); 14] = [
("markdown_url", r"\[[^]]*\]\(([^)]+)\)"),
(
"url",
r"((https?://|git@|git://|ssh://|ftp://|file:///)[^ ]+)",
),
("diff_a", r"--- a/([^ ]+)"),
("diff_b", r"\+\+\+ b/([^ ]+)"),
("docker", r"sha256:([0-9a-f]{64})"),
("path", r"(([.\w\-@~]+)?(/[.\w\-@]+)+)"),
("color", r"#[0-9a-fA-F]{6}"),
(
"uid",
r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
),
("ipfs", r"Qm[0-9a-zA-Z]{44}"),
("sha", r"[0-9a-f]{7,40}"),
("ip", r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"),
("ipv6", r"[A-f0-9:]+:+[A-f0-9:]+[%\w\d]+"),
("address", r"0x[0-9a-fA-F]+"),
("number", r"[0-9]{4,}"),
];

View file

@ -3,24 +3,7 @@ use regex::Regex;
use std::collections::HashMap;
use std::fmt;
const EXCLUDE_PATTERNS: [(&'static str, &'static str); 1] = [("bash", r"[[:cntrl:]]\[([0-9]{1,2};)?([0-9]{1,2})?m")];
const PATTERNS: [(&'static str, &'static str); 14] = [
("markdown_url", r"\[[^]]*\]\(([^)]+)\)"),
("url", r"((https?://|git@|git://|ssh://|ftp://|file:///)[^ ]+)"),
("diff_a", r"--- a/([^ ]+)"),
("diff_b", r"\+\+\+ b/([^ ]+)"),
("docker", r"sha256:([0-9a-f]{64})"),
("path", r"(([.\w\-@~]+)?(/[.\w\-@]+)+)"),
("color", r"#[0-9a-fA-F]{6}"),
("uid", r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}"),
("ipfs", r"Qm[0-9a-zA-Z]{44}"),
("sha", r"[0-9a-f]{7,40}"),
("ip", r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"),
("ipv6", r"[A-f0-9:]+:+[A-f0-9:]+[%\w\d]+"),
("address", r"0x[0-9a-fA-F]+"),
("number", r"[0-9]{4,}"),
];
use crate::regexes::{EXCLUDE_PATTERNS, PATTERNS};
#[derive(Clone)]
pub struct Match<'a> {
@ -58,7 +41,11 @@ pub struct State<'a> {
}
impl<'a> State<'a> {
pub fn new(lines: &'a Vec<&'a str>, alphabet: &'a Alphabet, regexp: &'a Vec<String>) -> State<'a> {
pub fn new(
lines: &'a Vec<&'a str>,
alphabet: &'a Alphabet,
regexp: &'a Vec<String>,
) -> State<'a> {
State {
lines,
alphabet,
@ -99,7 +86,9 @@ impl<'a> State<'a> {
None => None,
})
.collect::<Vec<_>>();
let first_match_option = submatches.iter().min_by(|x, y| x.2.start().cmp(&y.2.start()));
let first_match_option = submatches
.iter()
.min_by(|x, y| x.2.start().cmp(&y.2.start()));
if let Some(first_match) = first_match_option {
let (name, pattern, matching) = first_match;
@ -241,7 +230,10 @@ mod tests {
assert_eq!(results.len(), 3);
assert_eq!(results.get(0).unwrap().text.clone(), "/tmp/foo/bar_lol");
assert_eq!(results.get(1).unwrap().text.clone(), "/var/log/boot-strap.log");
assert_eq!(
results.get(1).unwrap().text.clone(),
"/var/log/boot-strap.log"
);
assert_eq!(results.get(2).unwrap().text.clone(), "../log/kern.log");
}
@ -258,7 +250,8 @@ mod tests {
#[test]
fn match_uids() {
let lines = split("Lorem ipsum 123e4567-e89b-12d3-a456-426655440000 lorem\n Lorem lorem lorem");
let lines =
split("Lorem ipsum 123e4567-e89b-12d3-a456-426655440000 lorem\n Lorem lorem lorem");
let custom = [].to_vec();
let alphabet = Alphabet("abcd".to_string());
let results = State::new(&lines, &alphabet, &custom).matches(false, false);
@ -285,7 +278,8 @@ mod tests {
#[test]
fn match_ips() {
let lines = split("Lorem ipsum 127.0.0.1 lorem\n Lorem 255.255.10.255 lorem 127.0.0.1 lorem");
let lines =
split("Lorem ipsum 127.0.0.1 lorem\n Lorem 255.255.10.255 lorem 127.0.0.1 lorem");
let custom = [].to_vec();
let alphabet = Alphabet("abcd".to_string());
let results = State::new(&lines, &alphabet, &custom).matches(false, false);
@ -310,21 +304,32 @@ mod tests {
"2001:67c:670:202:7ba8:5e41:1591:d723"
);
assert_eq!(results.get(2).unwrap().text.clone(), "fe80::2:1");
assert_eq!(results.get(3).unwrap().text.clone(), "fe80:22:312:fe::1%eth0");
assert_eq!(
results.get(3).unwrap().text.clone(),
"fe80:22:312:fe::1%eth0"
);
}
#[test]
fn match_markdown_urls() {
let lines = split("Lorem ipsum [link](https://github.io?foo=bar) ![](http://cdn.com/img.jpg) lorem");
let lines = split(
"Lorem ipsum [link](https://github.io?foo=bar) ![](http://cdn.com/img.jpg) lorem",
);
let custom = [].to_vec();
let alphabet = Alphabet("abcd".to_string());
let results = State::new(&lines, &alphabet, &custom).matches(false, false);
assert_eq!(results.len(), 2);
assert_eq!(results.get(0).unwrap().pattern.clone(), "markdown_url");
assert_eq!(results.get(0).unwrap().text.clone(), "https://github.io?foo=bar");
assert_eq!(
results.get(0).unwrap().text.clone(),
"https://github.io?foo=bar"
);
assert_eq!(results.get(1).unwrap().pattern.clone(), "markdown_url");
assert_eq!(results.get(1).unwrap().text.clone(), "http://cdn.com/img.jpg");
assert_eq!(
results.get(1).unwrap().text.clone(),
"http://cdn.com/img.jpg"
);
}
#[test]
@ -335,11 +340,17 @@ mod tests {
let results = State::new(&lines, &alphabet, &custom).matches(false, false);
assert_eq!(results.len(), 4);
assert_eq!(results.get(0).unwrap().text.clone(), "https://www.rust-lang.org/tools");
assert_eq!(
results.get(0).unwrap().text.clone(),
"https://www.rust-lang.org/tools"
);
assert_eq!(results.get(0).unwrap().pattern.clone(), "url");
assert_eq!(results.get(1).unwrap().text.clone(), "https://crates.io");
assert_eq!(results.get(1).unwrap().pattern.clone(), "url");
assert_eq!(results.get(2).unwrap().text.clone(), "https://github.io?foo=bar");
assert_eq!(
results.get(2).unwrap().text.clone(),
"https://github.io?foo=bar"
);
assert_eq!(results.get(2).unwrap().pattern.clone(), "url");
assert_eq!(results.get(3).unwrap().text.clone(), "ssh://github.io");
assert_eq!(results.get(3).unwrap().pattern.clone(), "url");
@ -360,7 +371,8 @@ mod tests {
#[test]
fn match_hex_colors() {
let lines = split("Lorem #fd7b56 lorem #FF00FF\n Lorem #00fF05 lorem #abcd00 lorem #afRR00");
let lines =
split("Lorem #fd7b56 lorem #FF00FF\n Lorem #00fF05 lorem #abcd00 lorem #afRR00");
let custom = [].to_vec();
let alphabet = Alphabet("abcd".to_string());
let results = State::new(&lines, &alphabet, &custom).matches(false, false);
@ -434,7 +446,10 @@ mod tests {
assert_eq!(results.get(0).unwrap().text.clone(), "http://foo.bar");
assert_eq!(results.get(1).unwrap().text.clone(), "CUSTOM-52463");
assert_eq!(results.get(2).unwrap().text.clone(), "ISSUE-123");
assert_eq!(results.get(3).unwrap().text.clone(), "/var/fd70b569/9999.log");
assert_eq!(
results.get(3).unwrap().text.clone(),
"/var/fd70b569/9999.log"
);
assert_eq!(results.get(4).unwrap().text.clone(), "52463");
assert_eq!(results.get(5).unwrap().text.clone(), "973113");
assert_eq!(
@ -442,6 +457,9 @@ mod tests {
"123e4567-e89b-12d3-a456-426655440000"
);
assert_eq!(results.get(7).unwrap().text.clone(), "8888");
assert_eq!(results.get(8).unwrap().text.clone(), "https://crates.io/23456/fd70b569");
assert_eq!(
results.get(8).unwrap().text.clone(),
"https://crates.io/23456/fd70b569"
);
}
}

View file

@ -1,26 +1,10 @@
use clap::Clap;
use copyrat::error::ParseError;
use regex::Regex;
use std::collections::HashMap;
use std::process::Command;
use std::str::FromStr;
/// Execute an arbitrary Unix command and return the stdout as a `String` if
/// successful.
fn execute(command: &str, args: &Vec<&str>) -> Result<String, ParseError> {
let output = Command::new(command).args(args).output()?;
if !output.status.success() {
let msg = String::from_utf8_lossy(&output.stderr);
return Err(ParseError::ProcessFailure(format!(
"Process failure: {} {}, error {}",
command,
args.join(" "),
msg
)));
}
Ok(String::from_utf8_lossy(&output.stdout).to_string())
}
use copyrat::error::ParseError;
use copyrat::process;
#[derive(Debug, PartialEq)]
pub struct Pane {
@ -97,7 +81,7 @@ pub fn list_panes() -> Result<Vec<Pane>, ParseError> {
"#{pane_id}:#{?pane_in_mode,1,0}:#{pane_height}:#{scroll_position}:#{?pane_active,active,nope}",
];
let output = execute("tmux", &args)?;
let output = process::execute("tmux", &args)?;
// Each call to `Pane::parse` returns a `Result<Pane, _>`. All results
// are collected into a Result<Vec<Pane>, _>, thanks to `collect()`.
@ -116,7 +100,7 @@ pub fn list_panes() -> Result<Vec<Pane>, ParseError> {
pub fn get_options(prefix: &str) -> Result<HashMap<String, String>, ParseError> {
let args = vec!["show", "-g"];
let output = execute("tmux", &args)?;
let output = process::execute("tmux", &args)?;
let lines: Vec<&str> = output.split('\n').collect();
let pattern = format!(r#"{prefix}([\w\-0-9]+) "?(\w+)"?"#, prefix = prefix);
@ -192,6 +176,20 @@ pub enum CaptureRegion {
//Region(i32, i32),
}
impl FromStr for CaptureRegion {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, ParseError> {
match s {
"leading" => Ok(CaptureRegion::EntireHistory),
"trailing" => Ok(CaptureRegion::VisibleArea),
_ => Err(ParseError::ExpectedString(String::from(
"entire-history or visible-area",
))),
}
}
}
/// Returns the entire Pane content as a `String`.
///
/// `CaptureRegion` specifies if the visible area is captured, or the entire
@ -224,7 +222,7 @@ pub fn capture_pane(pane: &Pane, region: &CaptureRegion) -> Result<String, Parse
let args: Vec<&str> = args.split(' ').collect();
let output = execute("tmux", &args)?;
let output = process::execute("tmux", &args)?;
Ok(output)
// format!(

View file

@ -1,8 +1,10 @@
use super::{colors, state};
use crate::error::ParseError;
use clap::Clap;
use std::char;
use std::io::{stdout, Read, Write};
use std::str::FromStr;
use termion::async_stdin;
use termion::event::Key;
use termion::input::TermRead;
@ -30,32 +32,32 @@ pub struct ViewColors {
/// Foreground color for matches.
#[clap(long, default_value = "green",
parse(try_from_str = colors::parse_color))]
match_fg: Box<dyn color::Color>,
pub match_fg: Box<dyn color::Color>,
/// Background color for matches.
#[clap(long, default_value = "black",
parse(try_from_str = colors::parse_color))]
match_bg: Box<dyn color::Color>,
pub match_bg: Box<dyn color::Color>,
/// Foreground color for the focused match.
#[clap(long, default_value = "blue",
parse(try_from_str = colors::parse_color))]
focused_fg: Box<dyn color::Color>,
pub focused_fg: Box<dyn color::Color>,
/// Background color for the focused match.
#[clap(long, default_value = "black",
parse(try_from_str = colors::parse_color))]
focused_bg: Box<dyn color::Color>,
pub focused_bg: Box<dyn color::Color>,
/// Foreground color for hints.
#[clap(long, default_value = "white",
parse(try_from_str = colors::parse_color))]
hint_fg: Box<dyn color::Color>,
pub hint_fg: Box<dyn color::Color>,
/// Background color for hints.
#[clap(long, default_value = "black",
parse(try_from_str = colors::parse_color))]
hint_bg: Box<dyn color::Color>,
pub hint_bg: Box<dyn color::Color>,
}
/// Describes if, during rendering, a hint should aligned to the leading edge of
@ -66,6 +68,20 @@ pub enum HintAlignment {
Trailing,
}
impl FromStr for HintAlignment {
type Err = ParseError;
fn from_str(s: &str) -> Result<HintAlignment, ParseError> {
match s {
"leading" => Ok(HintAlignment::Leading),
"trailing" => Ok(HintAlignment::Trailing),
_ => Err(ParseError::ExpectedString(String::from(
"leading or trailing",
))),
}
}
}
/// Describes the style of contrast to be used during rendering of the hint's
/// text.
///