我想更新這個問題,以防其他人在網上搜索這個問題並絆倒這個問題。
好的,所以答案其實很簡單,並且需要閱讀python curses文檔中列出的所有函數。
我做的是做一個3狀態機:狀態1:普通模式(僅顯示文本),狀態2:高亮模式,允許光標在窗口周圍移動,狀態3:高亮模式,只給出從左到右限制文字移動,並突出文字移動。
爲了完成這個任務,它只需要一些基本的curses函數調用。
我做了單獨的窗口,但我只是假設一個窗口來解釋。
不要在窗口中顯示的文字,堅持使用:
window.addstr()
window.refresh()
四處移動光標:
#get current cursor position
curr_y, curr_x = window.getyx()
# depending on direction, update the cursor with
next_y, next_x = get_next_direction()
# place cursor in new position
window.move(next_y, next_x)
window.refresh()
一旦光標在起點突出,按「V」進入熒光筆狀態並限制光標的移動,更改所選文字的屬性:
# get current cursor position
curr_y, curr_x = window.getyx()
# save position for later use
start_y = curr_y; start_x = curr_x
# loop until 'v' is pressed again
while highlight_state:
# change the attribute of the current character, for 1 character only, to reverse
window.chgat(curr_y,curr_x, 1, curses.A_REVERSE)
curr_y, curr_x = get_next_direction()
# save end state, this is buggy obviously, but you get the point
end_y = curr_y; end_x = curr_X
現在從頭到尾提取該信息
# get integer representation of char at positiong
outstr = ''
#from start to end
char_as_int = windows.inch(y,x)
char = char_as_int & 0000FF
attr = char_as_int & FFFF00 #not useful here, but maybe later
outstr += char
就是這樣!我還嘗試了另一種方法來保存突出顯示的材質,該材質基本上將x,y座標轉換爲正在顯示的字符串的索引,但允許以字符串表示形式(換行符,製表符等)進行發佈,加上很難做到。
如果有人對更高效/更清潔的方法有任何意見,請回復!
謝謝你的迴應。我想Tkinter可能更容易使用。問題是我必須遠程運行服務器,我必須ssh兩次才能進入,並且最終服務器未連接到Internet(它包含敏感信息)。我認爲它有一個Tkinter庫,所以我必須檢查它。因爲我必須通過終端運行所有的東西,我想我可以用ssh -X; ssh -X能夠創建一個窗口環境。 – Neal