tmux-copyrat/src/main.rs

37 lines
1 KiB
Rust
Raw Normal View History

2020-05-24 21:02:11 +02:00
use clap::Clap;
2020-05-25 23:32:37 +02:00
use std::fs::OpenOptions;
use std::io::prelude::*;
2020-05-24 21:02:11 +02:00
use std::io::{self, Read};
2020-06-02 20:03:16 +02:00
2020-05-25 23:06:00 +02:00
use copyrat::{run, Opt};
2020-06-02 20:03:16 +02:00
fn main() {
2020-05-24 21:02:11 +02:00
let opt = Opt::parse();
// 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();
2020-05-25 23:32:37 +02:00
// Execute copyrat over the buffer (will take control over stdout).
// This returns the selected matches.
let output: String = run(buffer, &opt);
// Write output to a target_path if provided, else print to original stdout.
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();
}
}
2020-06-02 20:03:16 +02:00
}