Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close stdin before waiting to allow --select-1 to work #34

Merged
merged 2 commits into from Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions iterfzf/__init__.py
Expand Up @@ -106,16 +106,16 @@ def iterfzf(
if e.errno != errno.EPIPE and errno.EPIPE != 32:
raise
break
if proc is None or proc.wait() not in [0, 1]:
if print_query:
return None, None
else:
return None
try:
stdin.close()
except IOError as e:
if e.errno != errno.EPIPE and errno.EPIPE != 32:
raise
if proc is None or proc.wait() not in [0, 1]:
if print_query:
return None, None
else:
return None
stdout = proc.stdout
decode = (lambda b: b) if byte else (lambda t: t.decode(encoding))
output = [decode(ln.strip(b'\r\n\0')) for ln in iter(stdout.readline, b'')]
Expand Down
26 changes: 26 additions & 0 deletions iterfzf/test_iterfzf.py
@@ -0,0 +1,26 @@
import unittest
import iterfzf

flavors = [
"Chocolate", "Chocolate Chip", "Vanilla", "Strawberry", "Blueberry",
"Rocky Road"
]


class IterFzfTest(unittest.TestCase):

def test_no_query(self):
choice = iterfzf.iterfzf(flavors, executable="fzf")
self.assertEqual("Chocolate", choice)

def test_select_one(self):
choice = iterfzf.iterfzf(
flavors, query="Vani", __extra__=["-1"], executable="fzf"
)
self.assertEqual("Vanilla", choice)

def test_select_one_ambiguous(self):
choice = iterfzf.iterfzf(
flavors, query="Choc", __extra__=["-1"], executable="fzf"
)
self.assertTrue(choice.rfind('Chocolate') == 0)