-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
An example to show a workaround for dict
- Loading branch information
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
"""Currently ``fzf`` and ``iterfzf()`` takes only some kind of strings, | ||
so you can't pass arbitrary Python objects into that. Also there is no way | ||
to give each item some "hidden keys" besides displayed values. | ||
Therefore, if you want to show items in a dictionary, and make users to choose | ||
some items, then get chosen item keys, you need to give ``iterfzf()`` string | ||
representations of key-value pairs. Here's an example: | ||
""" | ||
from iterfzf import iterfzf | ||
|
||
|
||
def fzf_dict(d, multi): | ||
r"""This assumes keys must have no tabs, hence ``'\t'`` as a separator.""" | ||
options = ('{0}\t{1}'.format(k, v) for k, v in d.items()) | ||
for kv in iterfzf(options, multi=multi): | ||
yield kv[:kv.index('\t')] | ||
|
||
|
||
def main(): | ||
d = { | ||
'1': 'foo', | ||
'2': 'bar', | ||
'3': 'spam', | ||
'4': 'egg', | ||
} | ||
print(iterfzf(d.values())) | ||
keys = fzf_dict(d, multi=True) | ||
for key in keys: | ||
print(repr(key), '=>', repr(d[key])) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
2d13fdf
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice