mirror of
https://github.com/TECHNOFAB11/tmux-copyrat.git
synced 2025-12-14 17:03:50 +01:00
refactor: refactor
This commit is contained in:
parent
0e0e581f86
commit
d2d3e812fc
5 changed files with 7 additions and 8 deletions
43
src/main.rs
43
src/main.rs
|
|
@ -1,43 +0,0 @@
|
|||
use clap::Clap;
|
||||
use std::fs::OpenOptions;
|
||||
use std::io::prelude::*;
|
||||
use std::io::{self, Read};
|
||||
|
||||
use copyrat::{run, selection::Selection, CliOpt};
|
||||
|
||||
fn main() {
|
||||
let opt = CliOpt::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();
|
||||
|
||||
// Execute copyrat over the buffer (will take control over stdout).
|
||||
// This returns the selected matche.
|
||||
let selection: Option<Selection> = run(buffer, &opt);
|
||||
|
||||
// Early exit, signaling no selections were found.
|
||||
if selection.is_none() {
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
let Selection { text, .. } = selection.unwrap();
|
||||
|
||||
// Write output to a target_path if provided, else print to original stdout.
|
||||
match opt.target_path {
|
||||
None => println!("{}", text),
|
||||
Some(target) => {
|
||||
let mut file = OpenOptions::new()
|
||||
.create(true)
|
||||
.truncate(true)
|
||||
.write(true)
|
||||
.open(target)
|
||||
.expect("Unable to open the target file");
|
||||
|
||||
file.write_all(text.as_bytes()).unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue