譯者 | 布加迪
審校 | 重樓
少些滾動,多點(diǎn)專注。在這個用時15分鐘的Python 項目中,我們將借助氛圍編程編寫一個簡潔、無干擾的速讀應(yīng)用程序。
想象一下:你想構(gòu)建一個速讀應(yīng)用程序。你無需花數(shù)小時研究要使用哪些Python模塊和庫、編寫不同的組件代碼以及調(diào)試語法錯誤,只需用簡單的英語描述需求即可。短短幾分鐘內(nèi),你就可以調(diào)整字體大小,并與AI編程伙伴討論改進(jìn)用戶體驗。
這就是氛圍編程,這種協(xié)作方法利用自然語言指令通過迭代對話,幫助構(gòu)建實用的應(yīng)用程序。它并非要取代傳統(tǒng)的編程技能,而是加快從概念到實用原型的過程。
今天,我將向你介紹如何使用Python在短短15分鐘內(nèi)構(gòu)建一個功能齊全的RSVP(快速序列視覺呈現(xiàn))速讀應(yīng)用程序。
GitHub上的速寫應(yīng)用程序鏈接:https://github.com/balapriyac/data-science-tutorials/tree/main/vibe-coding/speed-reader。
從想法到實現(xiàn)
假設(shè)你有一個想法,想用氛圍編程來實現(xiàn)。如果你已經(jīng)使用ChatGPT、Claude或Gemini,可以繼續(xù)使用同一個工具。建議你嘗試這些提示(或更精準(zhǔn)的提示),看看你能構(gòu)建什么。
第1步:描述你想要構(gòu)建的內(nèi)容
你可以用一個簡單的請求開始:
"I'd like to create a command-line speed reading application using Python that implements RSVP (Rapid Serial Visual Presentation) technique. The app should run on Ubuntu, display words sequentially at adjustable speeds, and include basic controls based on keyboard inputs. Could you provide a clean, well-structured implementation with proper error handling?"
(我想用Python創(chuàng)建一個命令行速讀應(yīng)用程序,該應(yīng)用程序?qū)崿F(xiàn)RSVP(快速序列視化呈現(xiàn))技術(shù)。該應(yīng)用程序應(yīng)該在Ubuntu上運(yùn)行,以可調(diào)整的速度按順序顯示單詞,并包含基于鍵盤輸入的基本控件。你能否提供一個簡潔、結(jié)構(gòu)良好且擁有適當(dāng)錯誤處理功能的實現(xiàn)方法?)
無需技術(shù)規(guī)格,無需詳細(xì)要求,只需要明確的意圖,這正是氛圍編程的妙處所在——你從“是什么”入手,而不是從“怎么做”入手。
這為我們提供了良好的起點(diǎn)。從這個最初的提示開始,你應(yīng)該會得到一個實用的且基于終端的速讀應(yīng)用程序:
class RSVPReader:
def __init__(self, text, wpm=250, chunk_size=1):
self.text = text
self.wpm = wpm
self.words = self._prepare_text()
self.current_index = 0
self.is_paused = False
self.delay = 60.0 / (wpm * chunk_size)
初始實現(xiàn)包括:
- 文本處理:將內(nèi)容拆分成可讀的塊
- 速度控制:易于配置的每分鐘字?jǐn)?shù)
- 交互式控制:暫停、繼續(xù)、導(dǎo)航、速度調(diào)整
- 進(jìn)度跟蹤:通過進(jìn)度條提供視覺反饋
- 文件支持:從文本文件或直接輸入讀取
有關(guān)該類的完整實現(xiàn),你可以查看rsvp_reader.py文件:https://github.com/balapriyac/data-science-tutorials/blob/main/vibe-coding/speed-reader/rsvp_reader.py。
第2步:提升用戶體驗
在提出改進(jìn)請求時,我們使用了描述性、目標(biāo)導(dǎo)向的語言:
"I'd like to enhance the visual presentation by centering the text display in the terminal window and increasing the font emphasis for better readability. Could you modify the code to utilize the terminal's center area more effectively while maintaining clean, professional output?"
(我希望通過讓文本在終端窗口居中顯示,并加大字體強(qiáng)調(diào)度以提高可讀性,從而增強(qiáng)視覺呈現(xiàn)效果。你能否修改代碼,以便更有效地利用終端的中心區(qū)域,同時保持輸出簡潔又專業(yè)?)
這促使我們對終端進(jìn)行調(diào)整:
def _get_terminal_size(self):
"""Get terminal dimensions for responsive layout"""
try:
import shutil
cols, rows = shutil.get_terminal_size()
return cols, rows
except OSError:
return 80, 24 # Sensible fallbacks
現(xiàn)在,速讀應(yīng)用程序仍然可以運(yùn)行。然而,我們可以進(jìn)行一番最后的改進(jìn)。
第3步:根據(jù)需要優(yōu)化用戶界面需求
我們最終的迭代請求明確了以下需求:
"I'd like to refine the interface design with these specific requirements: 1) Display text in the center 40% of the terminal screen, 2) Reduce default reading speed for better comprehension, 3) Create a static control interface that doesn't refresh, with only the reading text updating dynamically, 4) Maintain clean borders around the active display area. Could you implement these changes while preserving all existing functionality?"
(我希望根據(jù)以下具體需求優(yōu)化界面設(shè)計:1) 在終端屏幕中央 40% 的位置顯示文本;2) 降低默認(rèn)閱讀速度以提高理解能力;3) 創(chuàng)建一個不刷新的靜態(tài)控制界面,僅動態(tài)更新閱讀文本;4) 保持活動顯示區(qū)域周圍的邊框清晰。你能否在保留所有現(xiàn)有功能的同時實現(xiàn)這些更改?)
最終形成了以下終端控制:
def _get_display_area(self):
"""Get the 40% center rectangle dimensions"""
cols, rows = self._get_terminal_size()
display_width = int(cols * 0.4)
display_height = int(rows * 0.4)
start_col = (cols - display_width) // 2
start_row = (rows - display_height) // 2
return start_col, start_row, display_width, display_height
def _draw_static_interface(self):
"""Draw the static interface"""
# Controls stay fixed, only words change
技術(shù)細(xì)節(jié)概述
我們構(gòu)建的RSVP速讀應(yīng)用程序包含以下內(nèi)容。
線程化實現(xiàn)響應(yīng)式控制
該方法通過將終端切換到原始模式并使用非阻塞I/O輪詢,實時捕獲鍵盤輸入,又不暫停主程序:
def _get_keyboard_input(self):
"""Non-blocking keyboard input handler"""
old_settings = termios.tcgetattr(sys.stdin)
try:
tty.setraw(sys.stdin.fileno())
while self.is_running:
if select.select([sys.stdin], [], [], 0.1)[0]:
# Handle real-time input without blocking
智能終端定位
該方法使用ANSI轉(zhuǎn)義序列將文本定位于終端屏幕上的精確坐標(biāo),代碼會在打印輸出單詞之前將光標(biāo)移動到特定的行和列:
def _display_word(self, word):
# Use ANSI escape codes for precise positioning
print(f'\033[{word_row};{word_start_col}H{large_word}')
自適應(yīng)速度控制
這會根據(jù)單詞長度動態(tài)調(diào)整閱讀速度,使用戶閱讀長單詞(8個字符以上)的時間增加 20%,閱讀短單詞(4 個字符以下)的時間減少 20%,從而優(yōu)化理解:
# Longer words get more display time
word_delay = self.delay
if len(current_word) > 8:
word_delay *= 1.2
elif len(current_word) < 4:
word_delay *= 0.8
好了,你可以運(yùn)行該應(yīng)用程序,體驗一下其工作原理。
首先,你可以像這樣使其可執(zhí)行。確保你能夠在腳本頂部添加shebang行:
$ chmod +x rsvp_reader.py
你可以以這種方式運(yùn)行它:
$ ./rsvp_reader.py sample.txt
可以在README 文件中找到更多詳細(xì)信息:https://github.com/balapriyac/data-science-tutorials/blob/main/vibe-coding/speed-reader/README.md。
結(jié)語
我們的氛圍編程課成果如下:
- 一個功能齊全的基于終端的速讀應(yīng)用程序,用Python編寫
- 支持可變化的閱讀速度 (50-1000+ WPM)
- 實時控制,以實現(xiàn)暫停、導(dǎo)航和速度調(diào)節(jié)
- 自適應(yīng)顯示屏,適用于任何尺寸的終端
- 簡潔、無干擾的界面,專注于40%的中心區(qū)域
- 基于字長和復(fù)雜度的智能單詞計時
我們在15分鐘內(nèi)將一個簡單的想法變成了一個切實可行的實用的應(yīng)用程序。
準(zhǔn)備好親自嘗試氛圍編程了嗎?不妨從一個簡單的想法入手,用簡明英文來描述,看看對話會把你帶到何處,代碼會自動跟上。
原文標(biāo)題:Vibe Coding a Speed Reading App with Python in Just 15 Minutes,作者:Bala Priya C