使用Python curses突出显示和选择文本 [英] Highlighting and Selecting text with Python curses

查看:199
本文介绍了使用Python curses突出显示和选择文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个导致堆栈溢出的帖子.我多年来一直在这个网站上寻找信息,它总是有帮助的,所以我认为我会提出我的第一个问题.

This is my first post to stack overflow. I've been lurking this site for information for years, and it's always helpful, so I thought that I would post my first question.

我一直在寻找一些类似的例子,但似乎找不到任何东西.

I've been searching for some similar examples, but can't seem to find anything.

最终,我正在尝试编写一个简单的文本ui,以在文本提取程序中查找误报和误报.误报模块是一个简单的是/否选择,并且显示彩色文本和使用getch()很容易.然而,假阴性部分变得越来越困难.

Ultimately, I'm trying to write a simple text ui for finding false positives and false negatives in a text extraction program. The false positive module is a simple yes/no selection, and displaying colored text and using getch() is easy enough. The False negative portion however, is a becoming difficult.

这就是我想要做的:

  1. 在屏幕上显示一个字符串(暂时不滚动.... ugh)它只会是没有换行符的字符串.
  2. 用户看到文本,然后按"h"进入突出显示模式.
  3. 然后,用户可以控制光标将其移动到文本的一部分(仍显示)上,并选择"v"以开始突出显示(我试图使其尽可能接近vi)
  4. 然后,用户使用控制键(箭头,hjkl)将光标从起点移至终点,并在途中突出显示文本.此突出显示的部分应该是一个虚假否定词
  5. 用户按下某个键(可能是"y"),所选文本将被着色或保持突出显示状态,并且突出显示的文本将保存到稍后将要处理的某个变量中.
  6. 用户退出突出显示模式并继续.

有什么想法可以开始吗?我正在尝试一些简单的事情,例如在屏幕上保留文本并四处移动光标,但无济于事.

Any ideas to even START? I'm trying simple things like keeping text on the screen and moving the cursor around, but to no avail.

我知道curses.textpad.TextBox()模块,但是它执行诸如插入和删除之类的编辑,我不想这样做.也许有一种方法可以禁用它.

I'm aware of the curses.textpad.TextBox() module, but it performs editing like insertion and deletion, which I don't want to do. Perhaps there is a way to disable it.

我还有其他问题,但我将暂时保持具体.

I have other questions, but I'll keep this specific for now.

谢谢!

尼尔

更具体地说,我并不是在寻求编写整个程序的帮助,而只是希望将光标移至显示的文本上方,突出显示该文本并将其选中并保存到变量中.

To be more specific, I'm not looking for help writing the whole program, just help moving the cursor over displayed text, highlighting it, and selecting it and saVing it in a variable.

推荐答案

我想更新此问题,以防其他人在网上搜索该问题并偶然发现该问题.

I would like to update this question in case anybody else is searching the web for this and stumbles upon this question.

好的,所以答案实际上非常简单,需要阅读python curses文档中列出的所有功能.

Okay, so the answer was actually quite simple and required reading all the functions listed on the python curses documentation.

我所做的就是制作一个3状态机:状态1:普通模式(仅显示文本),状态2:突出显示模式,允许光标在窗口中移动,状态3:突出显示模式,仅提供有限的状态在文本上从左到右移动,并在移动时突出显示文本.

What I did was make a 3 state machine: State 1: Normal mode (displays text only), State 2: highlight mode, allow the cursor to move around the window, and State 3: highlighter mode, which gives only limited movement from left to right over texts and highlights the text as it's moving.

要完成此任务,只需执行一些基本的curses函数调用.

To accomplish this task, it just takes some basic curses function calls.

我制作了单独的窗口,但我只假设使用一个窗口进行解释.

I made separate windows, but I'll just assume a single window for explaination.

要在窗口中显示文本,请坚持:

Do display text in the window, stick with the:

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"进入突出显示状态,并限制光标的移动,更改所选文本的属性:

Once the cursor is over the starting point for highlighting, press 'v' enter the highlighter state, and limit the movement of the cursor, change the attribute of the selected text:

# 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

现在从头到尾提取该信息

Now extract that information from start to end

# 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坐标上转换为要显示的字符串的索引,但允许以字符串表示形式(换行符,制表符等)发出,加上只是很难做.

That's it! I also tried another way to save the highlighted material which was basically transformat the x,y coordinates to the index of the string that was being display, but that let to issue in string representation (newlines, tabs, and so on), plus was just harder to do.

如果有人对更有效/更清洁的方法发表评论,请回复!

If anyone has comments of a more efficient/ cleaner method, please respond!

这篇关于使用Python curses突出显示和选择文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆