使用命令資料庫開發 ulauncher 擴展
週末,我透過 Reddit 找到了一個項目,涉及 Flow Launcher 的插件。我為我的 Ubuntu Linux 環境創建了一個 fzf 和 rofi 版本,然後想,將其移植到 uLauncher 有多難?
在這裡我記錄了我所做的事情。
1.在~/.local/share/ulauncher/extensions/裡面
建立一個新目錄。就我而言,我創建了 ~/.local/share/ulauncher/extensions/com.github.ubuntupunk.ulauncher-vim
2. 觸摸以下文件:
├── images │ └── icon.png ├── versions.json ├── manifest.json └── main.py
3. 在 versions.json 中放置以下樣板:
[ {"required_api_version": "2", "commit": "master"} ]
4. 在manifest.json中
{ "required_api_version": "2", "name": "Demo extension", "description": "Extension Description", "developer_name": "John Doe", "icon": "images/icon.png", "options": { "query_debounce": 0.1 }, "preferences": [ { "id": "demo_kw", "type": "keyword", "name": "Demo", "description": "Demo extension", "default_value": "dm" } ] }
5. 在main.py中
from ulauncher.api.client.Extension import Extension from ulauncher.api.client.EventListener import EventListener from ulauncher.api.shared.event import KeywordQueryEvent, ItemEnterEvent from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction from ulauncher.api.shared.action.HideWindowAction import HideWindowAction class DemoExtension(Extension): def __init__(self): super().__init__() self.subscribe(KeywordQueryEvent, KeywordQueryEventListener()) class KeywordQueryEventListener(EventListener): def on_event(self, event, extension): items = [] for i in range(5): items.append(ExtensionResultItem(icon='images/icon.png', name='Item %s' % i, description='Item description %s' % i, on_enter=HideWindowAction())) return RenderResultListAction(items) if __name__ == '__main__': DemoExtension().run()
6.現在編輯manifest.json
{ "required_api_version": "2", "name": "Vim Prompter", "description": "Vim cheatsheet helper", "developer_name": "David Robert Lewis", "icon": "images/icon.png", "options": { "query_debounce": 0.1 }, "preferences": [ { "id": "vm_kw", "type": "keyword", "name": "Vim", "description": "Search for Vim commands", "default_value": "vm" } ]
7.在main.py中新增命令載入功能
class VmExtension(Extension): def load_vim_commands(self): """Load Vim commands from JSON file.""" package_dir = os.path.dirname(os.path.abspath(__file__)) full_path = os.path.join(package_dir, 'db', 'commands.json') with open(full_path, 'r') as file: return json.load(file) def __init__(self): super().__init__() self.vim_commands = self.load_vim_commands() self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
8. 使用以下內容建立 db 資料夾:
commands.json
範例結構:
{ "categories": { "navigation": { "name": "Navigation", "patterns": [ "scroll", "jump", "goto", "position" ], "subcategories": { "cursor": { "name": "Cursor Movement", "patterns": [ "move[s]? cursor", "^[hjkl]$", "^[HJKL]$", "^[wWeEbB]$" ] },
您可以在此處查看完整的commands.json。
9. 修改KeywordQueryEventListener以實作搜尋功能。
class KeywordQueryEventListener(EventListener): def on_event(self, event, extension): query = event.get_argument() or "" items = [] # If no query, show all commands (limited to first 8) commands_to_show = extension.vim_commands # If there's a query, filter commands if query: commands_to_show = [ cmd for cmd in extension.vim_commands if query.lower() in cmd['command'].lower() or query.lower() in cmd['description'].lower() ] # Limit results to first 8 matches for cmd in commands_to_show[:8]: items.append(ExtensionResultItem( icon='images/icon.png', name=cmd['command'], description=f"{cmd['name']} - {cmd['description']}", on_enter=HideWindowAction() )) return RenderResultListAction(items)
10.新增URL開啟功能。我們需要匯入 webbrowser 並修改 on_enter 操作以開啟 Vim 命令 URL
from ulauncher.api.shared.action.OpenUrlAction import OpenUrlAction class KeywordQueryEventListener(EventListener): def on_event(self, event, extension): query = event.get_argument() or "" items = [] commands_to_show = extension.vim_commands if query: commands_to_show = [ cmd for cmd in extension.vim_commands if query.lower() in cmd['command'].lower() or query.lower() in cmd['description'].lower() ] for cmd in commands_to_show[:8]: url = f"https://vim.rtorr.com/#:~:text={cmd['rtorr_description']}" items.append(ExtensionResultItem( icon='images/icon.png', name=cmd['command'], description=f"{cmd['name']} - {cmd['description']}", on_enter=OpenUrlAction(url) )) return RenderResultListAction(items)
11. 主要變化是:
- 新增了 OpenUrlAction 導入
- 用 OpenUrlAction 取代 HideWindowAction
- 使用指令的 rtorr_description 建構 URL
12.完整的項目代碼可以在這裡查看:
ulauncher-vim 倉庫
以及此處的 ulauncher 擴充
參考
- https://dev.to/brpaz/an-introduction-to-ulauncher-extension-development-1m69
- https://ext.ulauncher.io/-/github-ubuntupunk-ulauncher-vim
以上是使用命令資料庫開發 ulauncher 擴展的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Python更易學且易用,C 則更強大但複雜。 1.Python語法簡潔,適合初學者,動態類型和自動內存管理使其易用,但可能導致運行時錯誤。 2.C 提供低級控制和高級特性,適合高性能應用,但學習門檻高,需手動管理內存和類型安全。

每天學習Python兩個小時是否足夠?這取決於你的目標和學習方法。 1)制定清晰的學習計劃,2)選擇合適的學習資源和方法,3)動手實踐和復習鞏固,可以在這段時間內逐步掌握Python的基本知識和高級功能。

Python在開發效率上優於C ,但C 在執行性能上更高。 1.Python的簡潔語法和豐富庫提高開發效率。 2.C 的編譯型特性和硬件控制提升執行性能。選擇時需根據項目需求權衡開發速度與執行效率。

Python和C 各有優勢,選擇應基於項目需求。 1)Python適合快速開發和數據處理,因其簡潔語法和動態類型。 2)C 適用於高性能和系統編程,因其靜態類型和手動內存管理。

pythonlistsarepartofthestAndArdLibrary,herilearRaysarenot.listsarebuilt-In,多功能,和Rused ForStoringCollections,而EasaraySaraySaraySaraysaraySaraySaraysaraySaraysarrayModuleandleandleandlesscommonlyusedDduetolimitedFunctionalityFunctionalityFunctionality。

Python在自動化、腳本編寫和任務管理中表現出色。 1)自動化:通過標準庫如os、shutil實現文件備份。 2)腳本編寫:使用psutil庫監控系統資源。 3)任務管理:利用schedule庫調度任務。 Python的易用性和豐富庫支持使其在這些領域中成為首選工具。

Python在科學計算中的應用包括數據分析、機器學習、數值模擬和可視化。 1.Numpy提供高效的多維數組和數學函數。 2.SciPy擴展Numpy功能,提供優化和線性代數工具。 3.Pandas用於數據處理和分析。 4.Matplotlib用於生成各種圖表和可視化結果。

Python在Web開發中的關鍵應用包括使用Django和Flask框架、API開發、數據分析與可視化、機器學習與AI、以及性能優化。 1.Django和Flask框架:Django適合快速開發複雜應用,Flask適用於小型或高度自定義項目。 2.API開發:使用Flask或DjangoRESTFramework構建RESTfulAPI。 3.數據分析與可視化:利用Python處理數據並通過Web界面展示。 4.機器學習與AI:Python用於構建智能Web應用。 5.性能優化:通過異步編程、緩存和代碼優
