Closed Aeres-u99 closed 4 years ago
Currently iterfzf()
only accepts Unicode strings (i.e., str
) or byte strings (i.e., bytes
), not arbitrary Python objects like tuple
s. That's why iterfzf(d.items())
raises an error, as dict.items()
returns an iterable of pairs, not strings.
As iterfzf()
takes only strings and there is no way to give each item some “hidden keys” besides displayed values (and I believe it's same to fzf
), you need to give iterfzf()
string representations of key–value pairs. Here's an example:
from iterfzf import iterfzf
def fzf_dict(d, multi):
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()
The above code assumes keys must have no tabs, hence '\t'
as a separator.
iterfzf(d.values(),multi=True) shows nothing else other than 0 0 and exits without any errors
I believe iterfzf(d.values())
must show dictionary values, but I am not sure why you've experienced that. I guess it's because you did it in an IPython session, which directly deals with TTY?
Ahh thank you very much for explanation. Should we add this into the documentation as well? It will come in very handy. Are we planning to change it in anyways in near future?
I believe the limitation (that it takes only strings) should be fixed in the upstream fzf
project. In the docs I already wrote it only accepts Unicode strings or byte strings. Or do you mean other part of the docs?
I mean the workaround for these, like the one you just suggested to me. imho this is fantastic I will definitely want to see more of the development :D
Okay, I added the example code into the repository's examples/ directory:
So basically I have a dictionary
I would like to be able to search through foo,bar etc and return the corresponding id. Even If I could get the key and value both of selection, that'd be fine as well. I have tried following:
iterfzf(d,multi=True) which basically allows me to select the keys as dictionaries are iterable over keys. then iterfzf(d.values(),multi=True) shows nothing else other than 0 0 and exits without any errors iterfzf(d.items(),multi=True) shows some weird behaviour and error as follows:
It shows the error in exact disoriented manner.
I would like to know the correct way to achieve the desired result as well as once specified would go ahead and make the pr with appropriate inclusion in documentation. Thank you for this awesome module and for your generous time. using d.iteritems gives following Error:
EDIT: Added the iteritems error