Skip to content

Commit

Permalink
Check for interactive stdin in fzf example
Browse files Browse the repository at this point in the history
  • Loading branch information
alexrutar committed Jan 3, 2025
1 parent a2fd96b commit 2dce211
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
16 changes: 12 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ cat myfile.txt | ./target/release/examples/fzf
```
The code to create the binary:
```rust
use std::{io, process::exit, thread::spawn};
use std::{
io::{self, IsTerminal},
process::exit,
thread::spawn,
};

use nucleo_picker::{render::StrRenderer, Picker};

Expand All @@ -52,9 +56,13 @@ fn main() -> io::Result<()> {

let injector = picker.injector();
spawn(move || {
for line in io::stdin().lines() {
if let Ok(s) = line {
injector.push(s);
let stdin = io::stdin();
if !stdin.is_terminal() {
for line in stdin.lines() {
// silently drop IO errors!
if let Ok(s) = line {
injector.push(s);
}
}
}
});
Expand Down
17 changes: 12 additions & 5 deletions examples/fzf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
//!
//! Read lines from `stdin` in a streaming fashion and populate the picker, imitating the basic
//! functionality of [fzf](https://github.com/junegunn/fzf).
use std::{io, process::exit, thread::spawn};
use std::{
io::{self, IsTerminal},
process::exit,
thread::spawn,
};

use nucleo_picker::{render::StrRenderer, Picker};

Expand All @@ -11,10 +15,13 @@ fn main() -> io::Result<()> {

let injector = picker.injector();
spawn(move || {
for line in io::stdin().lines() {
// silently drop IO errors!
if let Ok(s) = line {
injector.push(s);
let stdin = io::stdin();
if !stdin.is_terminal() {
for line in stdin.lines() {
// silently drop IO errors!
if let Ok(s) = line {
injector.push(s);
}
}
}
});
Expand Down

0 comments on commit 2dce211

Please sign in to comment.