Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add refresh_code and json_formatter to app gallery
- Loading branch information
Showing
4 changed files
with
203 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,88 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Feature: | ||
No matter what user entered, always return a random value between 1 and 100. | ||
And this value is based on cache that won't change while user is typing. | ||
However, we want to provide a way to refresh the value. User can type "!~" | ||
and then hit ENTER to refresh the value. When user hit ENTER, it automatically | ||
removes the "!~" part and recover the original query. | ||
Difficulty: Hard | ||
Dependencies: NA | ||
Demo: https://asciinema.org/a/631197 | ||
""" | ||
|
||
import random | ||
import dataclasses | ||
|
||
import zelfred.api as zf | ||
|
||
|
||
@dataclasses.dataclass | ||
class RefreshItem(zf.Item): | ||
""" | ||
Represent an item that can refresh cache in the dropdown menu. | ||
""" | ||
|
||
def enter_handler(self, ui: zf.UI): | ||
""" | ||
Copy the content to clipboard. | ||
""" | ||
cache.value = None | ||
|
||
def post_enter_handler(self, ui: zf.UI): | ||
""" | ||
After the user input action, we would like to repaint the dropdown menu | ||
only to show the "Copied" item, and then wait for the next user input. | ||
""" | ||
ui.line_editor.move_to_end() | ||
ui.line_editor.press_backspace(self.variables["n_backspace"]) | ||
|
||
|
||
class Cache: | ||
def __init__(self): | ||
self.value = None | ||
|
||
|
||
cache = Cache() | ||
|
||
|
||
def handler(query: str, ui: zf.UI): | ||
""" | ||
The handler is the core of a Zelfred App. It's a user-defined function | ||
that takes the entered query and the UI object as inputs and returns | ||
a list of items to render. | ||
""" | ||
# if query is for refresh value | ||
if "!~" in query: | ||
before_query, after_query = query.split("!~", 1) | ||
return [ | ||
RefreshItem( | ||
title="Refresh value", | ||
subtitle=f"Hit {ui.render.ENTER} to refresh the value", | ||
variables={"n_backspace": len(after_query) + 2}, | ||
) | ||
] | ||
# otherwise, always return a random value, and cache it | ||
else: | ||
if cache.value is None: | ||
cache.value = random.randint(1, 100) | ||
return [ | ||
zf.Item( | ||
title=f"Value {cache.value}", | ||
) | ||
] | ||
|
||
|
||
if __name__ == "__main__": | ||
# reset the debugger and enable it | ||
zf.debugger.reset() | ||
zf.debugger.enable() | ||
|
||
# create the UI and run it | ||
ui = zf.UI(handler=handler, capture_error=True) | ||
ui.run() |
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,63 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
""" | ||
Feature: | ||
Copy JSON text to clipboard, then hit 'Enter' to dump the formatted JSON to | ||
``${HOME}/tmp/formatted.json`` and automatically open it. | ||
Difficulty: Easy | ||
Dependencies: NA | ||
Demo: https://asciinema.org/a/123456 | ||
""" | ||
|
||
import json | ||
import dataclasses | ||
from pathlib import Path | ||
|
||
import pyperclip | ||
import zelfred.api as zf | ||
|
||
p = Path.home().joinpath("tmp", "formatted.json") | ||
p.parent.mkdir(exist_ok=True) | ||
|
||
|
||
@dataclasses.dataclass | ||
class JsonFormatterItem(zf.Item): | ||
""" | ||
Represent a json formatter item in the dropdown menu. | ||
""" | ||
|
||
def enter_handler(self, ui: zf.UI): | ||
""" | ||
Read json from clipboard, format it, write to ~/tmp/formatted.json, then open it. | ||
""" | ||
s = pyperclip.paste() | ||
p.write_text(json.dumps(json.loads(s), indent=4)) | ||
zf.open_file(p) | ||
|
||
|
||
def handler(query: str, ui: zf.UI): | ||
""" | ||
The handler is the core of a Zelfred App. It's a user-defined function | ||
that takes the entered query and the UI object as inputs and returns | ||
a list of items to render. | ||
""" | ||
return [ | ||
JsonFormatterItem( | ||
title=f"Hit 'Enter' to format your JSON", | ||
subtitle="hint: copy your JSON to clipboard before you hit 'Enter'", | ||
) | ||
] | ||
|
||
|
||
if __name__ == "__main__": | ||
# reset the debugger and enable it | ||
zf.debugger.reset() | ||
zf.debugger.enable() | ||
|
||
# create the UI and run it | ||
ui = zf.UI(handler=handler, capture_error=True) | ||
ui.run() |
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
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