2020-05-24 21:02:11 +02:00
|
|
|
use clap::Clap;
|
|
|
|
|
use std::io::{self, Read};
|
2020-06-02 20:03:16 +02:00
|
|
|
|
2021-03-21 08:51:45 +01:00
|
|
|
use copyrat::{config::basic, run, ui::Selection};
|
2020-06-02 20:03:16 +02:00
|
|
|
|
|
|
|
|
fn main() {
|
2021-03-21 08:51:45 +01:00
|
|
|
let opt = basic::Config::parse();
|
2020-05-24 21:02:11 +02:00
|
|
|
|
|
|
|
|
// Copy the pane contents (piped in via stdin) into a buffer, and split lines.
|
|
|
|
|
let stdin = io::stdin();
|
|
|
|
|
let mut handle = stdin.lock();
|
|
|
|
|
|
|
|
|
|
let mut buffer = String::new();
|
|
|
|
|
handle.read_to_string(&mut buffer).unwrap();
|
2021-03-22 08:55:03 +01:00
|
|
|
let lines = buffer.split('\n').collect::<Vec<_>>();
|
2020-05-24 21:02:11 +02:00
|
|
|
|
2020-05-25 23:32:37 +02:00
|
|
|
// Execute copyrat over the buffer (will take control over stdout).
|
2021-03-22 23:56:35 +01:00
|
|
|
// This returns the selected span of text.
|
2021-03-22 08:55:03 +01:00
|
|
|
let selection: Option<Selection> = run(&lines, &opt);
|
2020-05-27 10:04:42 +02:00
|
|
|
|
|
|
|
|
// Early exit, signaling no selections were found.
|
2020-05-29 18:43:17 +02:00
|
|
|
if selection.is_none() {
|
2020-05-27 10:04:42 +02:00
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-17 07:55:24 +01:00
|
|
|
let Selection { text, .. } = selection.unwrap();
|
2021-03-21 11:32:57 +01:00
|
|
|
println!("{}", text);
|
2020-06-02 20:03:16 +02:00
|
|
|
}
|