Skip to content

Picker

A search box over a list of results on a floating surface: the basis of the command palette and the file finder. ui.fuzzy_filter(query, items, key=) ranks items (best match first) and returns [(item, positions), ...]; pass those pairs to picker to highlight the matches. ui.fuzzy_match scores one string and ui.highlighted draws one with its matches.

ui.picker · Category: Overlays · Full reference →

PickerPicker
  • For your own finders: files, symbols, branches, projects, emoji.
import pyzed as pz
from pyzed import ui
class Finder(pz.App):
def __init__(self):
self.query, self.selected = "", 0
self.files = ["src/app.rs", "src/render.rs", "README.md"]
def view(self):
matches = ui.fuzzy_filter(self.query, self.files)
items = [(ui.PaletteItem(f, lambda f=f: self.open(f), icon="file"), positions)
for f, positions in matches[:50]]
return ui.picker(self.query, lambda q: setattr(self, "query", q), items,
selected=self.selected, placeholder="Search files…")
Picker: With matchesPicker: With matches
With matches
Code
files = ["src/app.rs", "src/render.rs", "app.py"]
items = ui.fuzzy_filter("app", [ui.PaletteItem(f, on_open, icon="file-code") for f in files],
key=lambda item: item.label)
ui.picker("app", on_query, items, width=380)
Picker: empty_text=Picker: empty_text=
empty_text=
Code
ui.picker("zzz", on_query, [], empty_text="No files match", width=380)
Picker: PaletteItem(detail=)Picker: PaletteItem(detail=)
PaletteItem(detail=)
Code
ui.picker("", on_query, [(ui.PaletteItem("main", on_open, icon="git-branch", detail="2 hours ago"), []),
(ui.PaletteItem("docs", on_open, icon="git-branch", detail="yesterday"), [])], width=380)
Picker: ui.highlightedPicker: ui.highlighted
ui.highlighted
Code
t = pz.theme.current()
score, positions = ui.fuzzy_match("tgsw", "editor: toggle soft wrap")
ui.highlighted("editor: toggle soft wrap", positions, color=t.text, match_color=t.accent, size=15)

fuzzy_match returns (score, positions) or None; highlighted paints those positions.

Picker: Highlighting matchesPicker: Highlighting matches

Picker has 2 events of its own, listed below. Like every widget it also has the 18 common events (click, double_click, right_click, mouse_enter, drag, key_down, resize…), attached with .on(...): see Events and Events of every component.

Event Handler gets When
on_query (text) Callable that receives the new text.
on_submit () Message or callable sent when Enter is pressed.

Handler gets is what Pyzed passes to your callable: () means nothing. Any handler can also be a message (a value that is not callable), which goes to update().

The same events written three ways. Imperative: methods of the app change self. Declarative: the handlers produce messages and one update() decides. Functional: a pure function draws an immutable state and asks for changes with set_state(...). See Imperative, declarative and functional.

class Finder(pz.App):
def __init__(self):
self.query, self.opened = "", None
self.files = ["src/app.rs", "src/render.rs", "README.md"]
def matches(self):
return ui.fuzzy_filter(self.query, self.files)
def open_first(self): # on_submit: Enter
found = self.matches()
self.opened = found[0][0] if found else None
def view(self):
items = [(ui.PaletteItem(f, lambda f=f: setattr(self, "opened", f), icon="file"), positions)
for f, positions in self.matches()]
return ui.picker(self.query, lambda q: setattr(self, "query", q), items, # on_query
on_submit=self.open_first, placeholder="Search files…")
picker(
query,
on_query,
items,
*,
selected=0,
placeholder='Search…',
input_id='picker',
on_submit=None,
width=560,
max_height=340,
empty_text='No matches',
theme=None,
)

A searchable list on a floating surface. The basis of the palette and the file finder.

Property Default Description
query required Search text.
items required (PaletteItem, positions) pairs, for example from fuzzy_filter.
selected 0 Highlighted index.
placeholder "Search…" Hint text shown when empty.
input_id "picker" Field id.
width 560 Width: pixels, "fill", "shrink" or "fill:N" (proportion).
max_height 340 Maximum height in pixels.
empty_text "No matches" Text shown when there are no results.
theme None Theme to use instead of the current one (pyzed.theme.current()).

Events: on_query, on_submit (events).

Full reference →

fuzzy_filter(query, items, key=<class 'str'>)

Filters and sorts items by fuzzy score. Returns [(item, positions), ...].

Property Default Description
query required Search text.
items required Items.
key <class 'str'> Function that returns the text of each item.

Full reference →

fuzzy_match(query, candidate)

Returns (score, positions) if every letter of query appears in order in candidate; otherwise None. Rewards word starts and consecutive letters.

Property Default Description
query required Search text.
candidate required Candidate text.

Full reference →

highlighted(text, positions, *, color, match_color, size=13.5, weight=None)

Text with the characters at positions highlighted (fuzzy search matches).

Property Default Description
text required Text.
positions required Indices to highlight.
color required Text or icon color.
match_color required Color of the matches.
size 13.5 Size.
weight None Text weight.

Full reference →