Python?命令行?prompt_toolkit?庫(kù)詳解
Python 的第三方庫(kù) prompt_toolkit
用于打造交互式命令行,在交互式場(chǎng)景的使用中,prompt_toolkit
具有以下特點(diǎn):
- 語(yǔ)法高亮
- 支持多行編輯
- 支持代碼補(bǔ)全
- 支持自動(dòng)提示
- 使用鼠標(biāo)移動(dòng)光標(biāo)
- 支持查詢(xún)歷史
- 對(duì) Unicode 支持良好
- 跨平臺(tái)
- 支持 Emacs 與 Vi 風(fēng)格的快捷鍵
prompt_toolkit
在使用前需要先進(jìn)行安裝:
pip install prompt_toolkit
一. 使用 Bash 下常用快捷鍵
想必很多開(kāi)發(fā)者在創(chuàng)建交互式命令行工具時(shí),使用最多的還是 input
和 raw_input
。比如下面的代碼讀取用戶(hù)輸入數(shù)據(jù),并進(jìn)行打印:
while True: # user_input = input('>') user_input = raw_input('>') print(user_input) if user_input.strip().lower() == 'exit': break
上述程序在 Linux 環(huán)境下運(yùn)行時(shí),我們將無(wú)法使用任何的 Linux 快捷鍵,甚至在輸入錯(cuò)誤時(shí),按退格刪除內(nèi)容都會(huì)出現(xiàn)問(wèn)題:
下面,我們使用 prompt_toolkit
模塊中的 prompt
函數(shù)改寫(xiě)上述程序:
from __future__ import print_function from prompt_toolkit import prompt while True: user_input = prompt(u'>>') print(user_input)
運(yùn)行新的程序,你會(huì)發(fā)現(xiàn),不僅可以實(shí)現(xiàn)退格刪除,而且可以使用 Bash
下常用的快捷鍵:Ctrl + a
跳轉(zhuǎn)到開(kāi)頭、Ctrl + e
跳轉(zhuǎn)到末尾、Ctrl + k
刪除光標(biāo)到末尾的內(nèi)容。
二. 實(shí)現(xiàn)查找歷史命令
在 Bash 下,我們可以使用方向鍵中的 ↑
和 ↓
查看歷史輸入,或者使用 Ctrl + r
搜索歷史命令:
在 Python 打造的交互式命令行中,使用 prompt_toolkit.history
我們可以很容易實(shí)現(xiàn)查找歷史:
from __future__ import print_function from __future__ import unicode_literals from prompt_toolkit import prompt from prompt_toolkit.history import FileHistory while True: user_input = prompt('>>>', history=FileHistory('history.txt')) print(user_input)
運(yùn)行結(jié)果:
上述歷史輸入將被保存至當(dāng)前目錄下的 history.txt
文件中,后續(xù)就可以使用查看或搜索歷史命令了~
三. 根據(jù)歷史輸入自動(dòng)提示
在上面是示例中我們實(shí)現(xiàn)了查看或搜索歷史輸入的功能,其實(shí)我們還可以更加充分地利用 history.txt
中記載的歷史輸入,在用戶(hù)輸入時(shí)進(jìn)行提示。實(shí)現(xiàn)此功能只需要在調(diào)用 prompt
函數(shù)時(shí)指定 auto_suggest
的參數(shù)即可:
from __future__ import print_function from __future__ import unicode_literals from prompt_toolkit import prompt from prompt_toolkit.history import FileHistory from prompt_toolkit.auto_suggest import AutoSuggestFromHistory while True: user_input = prompt('>>>', history=FileHistory('history.txt'), auto_suggest=AutoSuggestFromHistory()) if user_input.strip().lower() == 'exit': break print(user_input)
prompt_toolkit
將以暗色字體顯示匹配的歷史輸入:
四. 實(shí)現(xiàn)輸入的自動(dòng)補(bǔ)全
所謂自動(dòng)補(bǔ)全,即用戶(hù)輸入了關(guān)鍵字的一部分,我們的交互式程序能夠根據(jù)已有的輸入進(jìn)行提示,用戶(hù)可以使用 Tab 鍵補(bǔ)全選擇提示的內(nèi)容。以上功能,prompt_toolkit
提供了名為 WorldCompleter
的類(lèi)來(lái)幫助我們實(shí)現(xiàn)。下面我們來(lái)模仿 MySQL 客戶(hù)端的提示功能:
from __future__ import print_function from __future__ import unicode_literals from prompt_toolkit import prompt from prompt_toolkit.history import FileHistory from prompt_toolkit.auto_suggest import AutoSuggestFromHistory from prompt_toolkit.contrib.completers import WordCompleter SQLCompleter = WordCompleter(['select', 'from', 'insert', 'update', 'delete' 'drop'], ignore_case=True) while True: user_input = prompt('SQL>', history=FileHistory('history.txt'), auto_suggest=AutoSuggestFromHistory(), completer=SQLCompleter) if user_input.strip().lower() == 'exit': break print(user_input)
到此這篇關(guān)于Python 命令行 - prompt_toolkit 庫(kù)的文章就介紹到這了,更多相關(guān)Python prompt_toolkit 庫(kù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python測(cè)試框架:pytest學(xué)習(xí)筆記
這篇文章主要介紹了Python測(cè)試框架:pytest的相關(guān)資料,幫助大家更好的利用python進(jìn)行單元測(cè)試,感興趣的朋友可以了解下2020-10-10Spark處理數(shù)據(jù)排序問(wèn)題如何避免OOM
這篇文章主要介紹了Spark處理數(shù)據(jù)排序問(wèn)題如何避免OOM,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05python logging.info在終端沒(méi)輸出的解決
這篇文章主要介紹了python logging.info在終端沒(méi)輸出的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-05-05python矩陣/字典實(shí)現(xiàn)最短路徑算法
這篇文章主要為大家詳細(xì)介紹了python矩陣/字典實(shí)現(xiàn)最短路徑算法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-01-01python+jinja2實(shí)現(xiàn)接口數(shù)據(jù)批量生成工具
這篇文章主要介紹了python+jinja2實(shí)現(xiàn)接口數(shù)據(jù)批量生成工具的操作方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08