Skip to content

Commit

Permalink
use fifo for input
Browse files Browse the repository at this point in the history
  • Loading branch information
LoricAndre committed Dec 1, 2024
1 parent fabb0dc commit d57ed5b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 15 deletions.
2 changes: 1 addition & 1 deletion skim/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ name = "sk"
path = "src/bin/main.rs"

[dependencies]
nix = "0.29.0"
nix = { version = "0.29.0", features = ["fs"] }
atty = { version = "0.2.14", optional = true }
regex = "1.6.0"
lazy_static = "1.4.0"
Expand Down
4 changes: 4 additions & 0 deletions skim/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ impl ItemPool {
self.length.load(Ordering::SeqCst)
}

pub fn is_empty(&self) -> bool {
self.len() == 0
}

pub fn num_not_taken(&self) -> usize {
self.length.load(Ordering::SeqCst) - self.taken.load(Ordering::SeqCst)
}
Expand Down
35 changes: 21 additions & 14 deletions skim/src/tmux.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::{
borrow::Cow,
env,
io::{BufRead as _, BufReader, BufWriter, IsTerminal as _, Write as _},
fs::File,
io::{BufRead as _, BufReader, IsTerminal as _, Write as _},
process::{Command, Stdio},
sync::Arc,
thread,
};

use nix::{sys::stat, unistd::mkfifo};
use rand::{distributions::Alphanumeric, Rng};
use tuikit::key::Key;
use which::which;
Expand Down Expand Up @@ -109,21 +111,26 @@ pub fn run_with(opts: &SkimOptions) -> Option<SkimOutput> {
let mut stdin_reader = BufReader::new(std::io::stdin());
let line_ending = if opts.read0 { b'\0' } else { b'\n' };

let t_tmp_stdin = temp_dir.join("stdin");
let stdin_handle = if has_piped_input {
debug!("Reading stdin and piping to file");

let stdin_f = std::fs::File::create(tmp_stdin.clone())
.unwrap_or_else(|e| panic!("Failed to create stdin file {}: {}", tmp_stdin.clone().display(), e));
let mut stdin_writer = BufWriter::new(stdin_f);
Some(thread::spawn(move || loop {
let mut buf = vec![];
match stdin_reader.read_until(line_ending, &mut buf) {
Ok(0) => break,
Ok(n) => {
debug!("Read {n} bytes from stdin");
stdin_writer.write_all(&buf).unwrap();
mkfifo(&tmp_stdin, stat::Mode::S_IRWXU)
.unwrap_or_else(|e| panic!("Failed to create stdin pipe {}: {}", tmp_stdin.clone().display(), e));
Some(thread::spawn(move || {
let mut stdin_writer = File::create(&t_tmp_stdin)
.unwrap_or_else(|e| panic!("Failed to open stdin pipe {}: {}", t_tmp_stdin.clone().display(), e));

loop {
let mut buf = vec![];
match stdin_reader.read_until(line_ending, &mut buf) {
Ok(0) => break,
Ok(n) => {
debug!("Read {n} bytes from stdin");
stdin_writer.write_all(&buf).unwrap();
}
Err(e) => panic!("Failed to read from stdin: {}", e),
}
Err(e) => panic!("Failed to read from stdin: {}", e),
}
}))
} else {
Expand Down Expand Up @@ -174,7 +181,7 @@ pub fn run_with(opts: &SkimOptions) -> Option<SkimOutput> {
.args(["-y", tmux_opts.y]);

for (name, value) in std::env::vars() {
if name.starts_with("SKIM") || name == "PATH" {
if name.starts_with("SKIM") || name == "PATH" || name.starts_with("RUST") {
debug!("adding {} = {} to the command's env", name, value);
tmux_cmd.args(["-e", &format!("{}={}", name, value)]);
}
Expand Down Expand Up @@ -245,7 +252,7 @@ fn push_quoted_arg(args_str: &mut String, arg: &str) {
"zsh" => Zsh::quote(arg),
"bash" => Bash::quote(arg),
"fish" => Fish::quote(arg),
"sh" | _ => Sh::quote(arg),
_ => Sh::quote(arg),
};
args_str.push_str(&format!(
" {}",
Expand Down

0 comments on commit d57ed5b

Please sign in to comment.