mirror of
https://github.com/TECHNOFAB11/tmux-copyrat.git
synced 2025-12-12 08:00:08 +01:00
chore(deps): bump clap to 3.0.0-beta.5
This commit is contained in:
parent
276d17ce8f
commit
077d16311e
9 changed files with 177 additions and 186 deletions
|
|
@ -1,4 +1,4 @@
|
|||
use clap::Clap;
|
||||
use clap::Parser;
|
||||
use std::io::{self, Read};
|
||||
|
||||
use copyrat::{config::basic, run, ui::Selection};
|
||||
|
|
|
|||
|
|
@ -1,14 +1,13 @@
|
|||
use clap::Clap;
|
||||
use std::str::FromStr;
|
||||
use clap::{ArgEnum, Parser};
|
||||
|
||||
use crate::{
|
||||
error,
|
||||
error::ParseError,
|
||||
textbuf::{alphabet, regexes},
|
||||
ui,
|
||||
};
|
||||
|
||||
/// Main configuration, parsed from command line.
|
||||
#[derive(Clap, Debug)]
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(author, about, version)]
|
||||
pub struct Config {
|
||||
/// Alphabet to draw hints from.
|
||||
|
|
@ -59,7 +58,7 @@ pub struct Config {
|
|||
///
|
||||
/// Underline or surround the hint for increased visibility.
|
||||
/// If not provided, only the hint colors will be used.
|
||||
#[clap(short = 's', long, arg_enum)]
|
||||
#[clap(short = 's', long, arg_enum, rename_all = "lowercase")]
|
||||
pub hint_style: Option<HintStyleArg>,
|
||||
|
||||
/// Chars surrounding each hint, used with `Surround` style.
|
||||
|
|
@ -69,8 +68,8 @@ pub struct Config {
|
|||
}
|
||||
|
||||
/// Type introduced due to parsing limitation,
|
||||
/// as we cannot directly parse into ui::HintStyle.
|
||||
#[derive(Debug, Clap)]
|
||||
/// as we cannot directly parse tuples into ui::HintStyle.
|
||||
#[derive(Debug, Clone, ArgEnum, Parser)]
|
||||
pub enum HintStyleArg {
|
||||
Bold,
|
||||
Italic,
|
||||
|
|
@ -78,26 +77,10 @@ pub enum HintStyleArg {
|
|||
Surround,
|
||||
}
|
||||
|
||||
impl FromStr for HintStyleArg {
|
||||
type Err = error::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, error::ParseError> {
|
||||
match s {
|
||||
"bold" => Ok(HintStyleArg::Bold),
|
||||
"italic" => Ok(HintStyleArg::Italic),
|
||||
"underline" => Ok(HintStyleArg::Underline),
|
||||
"surrond" => Ok(HintStyleArg::Surround),
|
||||
_ => Err(error::ParseError::ExpectedString(String::from(
|
||||
"bold, italic, underline or surround",
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Try to parse a `&str` into a tuple of `char`s.
|
||||
fn parse_chars(src: &str) -> Result<(char, char), error::ParseError> {
|
||||
fn parse_chars(src: &str) -> Result<(char, char), ParseError> {
|
||||
if src.chars().count() != 2 {
|
||||
return Err(error::ParseError::ExpectedSurroundingPair);
|
||||
return Err(ParseError::ExpectedSurroundingPair);
|
||||
}
|
||||
|
||||
let chars: Vec<char> = src.chars().collect();
|
||||
|
|
|
|||
|
|
@ -1,15 +1,15 @@
|
|||
use clap::Clap;
|
||||
use std::collections::HashMap;
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
|
||||
use clap::{ArgEnum, Parser};
|
||||
|
||||
use super::basic;
|
||||
use crate::{error, textbuf::alphabet, tmux, ui};
|
||||
use crate::{error::ParseError, textbuf::alphabet, tmux, ui};
|
||||
|
||||
/// Extended configuration for handling Tmux-specific configuration (options
|
||||
/// and outputs). This is only used by `tmux-copyrat` and parsed from command
|
||||
/// line..
|
||||
#[derive(Clap, Debug)]
|
||||
/// line.
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(author, about, version)]
|
||||
pub struct ConfigExt {
|
||||
/// Don't read options from Tmux.
|
||||
|
|
@ -30,13 +30,19 @@ pub struct ConfigExt {
|
|||
pub window_name: String,
|
||||
|
||||
/// Capture visible area or entire pane history.
|
||||
#[clap(long, arg_enum, default_value = "visible-area")]
|
||||
#[clap(
|
||||
arg_enum,
|
||||
long,
|
||||
rename_all = "kebab-case",
|
||||
default_value = "visible-area"
|
||||
)]
|
||||
pub capture_region: CaptureRegion,
|
||||
|
||||
/// Name of the copy-to-clipboard executable.
|
||||
///
|
||||
/// If during execution, the output destination is set to be clipboard,
|
||||
/// then copyrat will pipe the selected text to this executable.
|
||||
/// On macOS, this is `pbcopy`, on Linux, this is `xclip`.
|
||||
#[clap(long, default_value = "pbcopy")]
|
||||
pub clipboard_exe: String,
|
||||
|
||||
|
|
@ -46,46 +52,54 @@ pub struct ConfigExt {
|
|||
}
|
||||
|
||||
impl ConfigExt {
|
||||
pub fn initialize() -> Result<ConfigExt, error::ParseError> {
|
||||
pub fn initialize() -> Result<ConfigExt, ParseError> {
|
||||
let mut config_ext = ConfigExt::parse();
|
||||
|
||||
if !config_ext.ignore_tmux_options {
|
||||
let tmux_options: HashMap<String, String> = tmux::get_options("@copyrat-")?;
|
||||
|
||||
// Override default values with those coming from tmux.
|
||||
let wrapped = &mut config_ext.basic_config;
|
||||
let inner = &mut config_ext.basic_config;
|
||||
|
||||
for (name, value) in &tmux_options {
|
||||
match name.as_ref() {
|
||||
"@copyrat-capture-region" => {
|
||||
config_ext.capture_region = CaptureRegion::from_str(&value)?
|
||||
let case_insensitive = true;
|
||||
config_ext.capture_region = CaptureRegion::from_str(value, case_insensitive)
|
||||
.map_err(ParseError::ExpectedEnumVariant)?
|
||||
}
|
||||
"@copyrat-alphabet" => {
|
||||
wrapped.alphabet = alphabet::parse_alphabet(value)?;
|
||||
inner.alphabet = alphabet::parse_alphabet(value)?;
|
||||
}
|
||||
"@copyrat-reverse" => {
|
||||
wrapped.reverse = value.parse::<bool>()?;
|
||||
inner.reverse = value.parse::<bool>()?;
|
||||
}
|
||||
"@copyrat-unique-hint" => {
|
||||
wrapped.unique_hint = value.parse::<bool>()?;
|
||||
inner.unique_hint = value.parse::<bool>()?;
|
||||
}
|
||||
|
||||
"@copyrat-span-fg" => wrapped.colors.span_fg = ui::colors::parse_color(value)?,
|
||||
"@copyrat-span-bg" => wrapped.colors.span_bg = ui::colors::parse_color(value)?,
|
||||
"@copyrat-span-fg" => inner.colors.span_fg = ui::colors::parse_color(value)?,
|
||||
"@copyrat-span-bg" => inner.colors.span_bg = ui::colors::parse_color(value)?,
|
||||
"@copyrat-focused-fg" => {
|
||||
wrapped.colors.focused_fg = ui::colors::parse_color(value)?
|
||||
inner.colors.focused_fg = ui::colors::parse_color(value)?
|
||||
}
|
||||
"@copyrat-focused-bg" => {
|
||||
wrapped.colors.focused_bg = ui::colors::parse_color(value)?
|
||||
inner.colors.focused_bg = ui::colors::parse_color(value)?
|
||||
}
|
||||
"@copyrat-hint-fg" => wrapped.colors.hint_fg = ui::colors::parse_color(value)?,
|
||||
"@copyrat-hint-bg" => wrapped.colors.hint_bg = ui::colors::parse_color(value)?,
|
||||
"@copyrat-hint-fg" => inner.colors.hint_fg = ui::colors::parse_color(value)?,
|
||||
"@copyrat-hint-bg" => inner.colors.hint_bg = ui::colors::parse_color(value)?,
|
||||
|
||||
"@copyrat-hint-alignment" => {
|
||||
wrapped.hint_alignment = ui::HintAlignment::from_str(&value)?
|
||||
let case_insensitive = true;
|
||||
inner.hint_alignment = ui::HintAlignment::from_str(value, case_insensitive)
|
||||
.map_err(ParseError::ExpectedEnumVariant)?
|
||||
}
|
||||
"@copyrat-hint-style" => {
|
||||
wrapped.hint_style = Some(basic::HintStyleArg::from_str(&value)?)
|
||||
let case_insensitive = true;
|
||||
inner.hint_style = Some(
|
||||
basic::HintStyleArg::from_str(value, case_insensitive)
|
||||
.map_err(ParseError::ExpectedEnumVariant)?,
|
||||
)
|
||||
}
|
||||
|
||||
// Ignore unknown options.
|
||||
|
|
@ -98,11 +112,11 @@ impl ConfigExt {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Clap, Debug)]
|
||||
/// Specifies which region of the terminal buffer to capture.
|
||||
#[derive(Debug, Clone, ArgEnum, Parser)]
|
||||
pub enum CaptureRegion {
|
||||
/// The entire history.
|
||||
///
|
||||
/// This will end up sending `-S - -E -` to `tmux capture-pane`.
|
||||
// This will end up sending `-S - -E -` to `tmux capture-pane`.
|
||||
EntireHistory,
|
||||
/// The visible area.
|
||||
VisibleArea,
|
||||
|
|
@ -112,20 +126,6 @@ pub enum CaptureRegion {
|
|||
//Region(i32, i32),
|
||||
}
|
||||
|
||||
impl FromStr for CaptureRegion {
|
||||
type Err = error::ParseError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, error::ParseError> {
|
||||
match s {
|
||||
"entire-history" => Ok(CaptureRegion::EntireHistory),
|
||||
"visible-area" => Ok(CaptureRegion::VisibleArea),
|
||||
_ => Err(error::ParseError::ExpectedString(String::from(
|
||||
"entire-history or visible-area",
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Describes the type of buffer the selected should be copied to: either a
|
||||
/// tmux buffer or the system clipboard.
|
||||
#[derive(Clone)]
|
||||
|
|
|
|||
71
src/error.rs
71
src/error.rs
|
|
@ -1,50 +1,43 @@
|
|||
use std::fmt;
|
||||
// use std::fmt;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum ParseError {
|
||||
#[error("Expected 2 chars")]
|
||||
ExpectedSurroundingPair,
|
||||
|
||||
#[error("Unknown alphabet")]
|
||||
UnknownAlphabet,
|
||||
|
||||
#[error("Unknown ANSI color name: allowed values are magenta, cyan, black, ...")]
|
||||
UnknownColor,
|
||||
|
||||
#[error("Unknown pattern name")]
|
||||
UnknownPatternName,
|
||||
|
||||
#[error("Expected a pane id marker")]
|
||||
ExpectedPaneIdMarker,
|
||||
ExpectedInt(std::num::ParseIntError),
|
||||
ExpectedBool(std::str::ParseBoolError),
|
||||
|
||||
#[error("Failed parsing integer")]
|
||||
ExpectedInt {
|
||||
#[from]
|
||||
source: std::num::ParseIntError,
|
||||
},
|
||||
|
||||
#[error("Failed to parse bool")]
|
||||
ExpectedBool {
|
||||
#[from]
|
||||
source: std::str::ParseBoolError,
|
||||
},
|
||||
|
||||
#[error("Expected the string `{0}`")]
|
||||
ExpectedString(String),
|
||||
ProcessFailure(String),
|
||||
}
|
||||
|
||||
impl fmt::Display for ParseError {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
match self {
|
||||
ParseError::ExpectedSurroundingPair => write!(f, "Expected 2 chars"),
|
||||
ParseError::UnknownAlphabet => write!(f, "Expected a known alphabet"),
|
||||
ParseError::UnknownColor => {
|
||||
write!(f, "Expected ANSI color name (magenta, cyan, black, ...)")
|
||||
}
|
||||
ParseError::UnknownPatternName => write!(f, "Expected a known pattern name"),
|
||||
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),
|
||||
}
|
||||
}
|
||||
}
|
||||
#[error("Expected the value to be within `{0}`")]
|
||||
ExpectedEnumVariant(String),
|
||||
|
||||
impl From<std::num::ParseIntError> for ParseError {
|
||||
fn from(error: std::num::ParseIntError) -> Self {
|
||||
ParseError::ExpectedInt(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::str::ParseBoolError> for ParseError {
|
||||
fn from(error: std::str::ParseBoolError) -> Self {
|
||||
ParseError::ExpectedBool(error)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for ParseError {
|
||||
fn from(error: std::io::Error) -> Self {
|
||||
ParseError::ProcessFailure(error.to_string())
|
||||
}
|
||||
#[error("IOError: `{source}`")]
|
||||
Io {
|
||||
#[from]
|
||||
source: std::io::Error,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::error;
|
||||
use clap::Clap;
|
||||
use clap::Parser;
|
||||
use termion::color;
|
||||
|
||||
pub fn parse_color(src: &str) -> Result<Box<dyn color::Color>, error::ParseError> {
|
||||
|
|
@ -52,7 +52,7 @@ mod tests {
|
|||
/// - `focus_*` colors are used to render the currently focused text span.
|
||||
/// - `normal_*` colors are used to render other text spans.
|
||||
/// - `hint_*` colors are used to render the hints.
|
||||
#[derive(Clap, Debug)]
|
||||
#[derive(Parser, Debug)]
|
||||
#[clap(about)] // Needed to avoid this doc comment to be used as overall `about`.
|
||||
pub struct UiColors {
|
||||
/// Foreground color for base text.
|
||||
|
|
|
|||
|
|
@ -1,26 +1,9 @@
|
|||
use clap::Clap;
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::error::ParseError;
|
||||
use clap::{ArgEnum, Parser};
|
||||
|
||||
/// Describes if, during rendering, a hint should aligned to the leading edge of
|
||||
/// the matched text, or to its trailing edge.
|
||||
#[derive(Debug, Clap)]
|
||||
#[derive(Debug, Clone, ArgEnum, Parser)]
|
||||
pub enum HintAlignment {
|
||||
Leading,
|
||||
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",
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue