python - ncurses 文本编辑器。需要帮助文本选择

标签 python ncurses

那里!我正在尝试创建一个文本编辑器。我被困在选择文本上。选择功能正常,直到转换到新行。这就是我尝试做的:

import curses
import string
from dlist_mod import DoubleList


stdscr=curses.initscr()
curses.start_color()
stdscr.keypad(1)
curses.noecho()
curses.cbreak()
class Editor:
"""klasa Editor"""

    def __init__(self, stdscr):        
    """"inicijalizacija"""

        self.stdscr = stdscr
        self.maxy, self.maxx = self.stdscr.getmaxyx()
        curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_WHITE)
        self.lines = DoubleList()
        self.lines.append("6)Like s str other dynamic languages, Python is often used as a                  scripting language, but is also used in a wide range of non-scripting contexts.")
        self.lines.append("7)Using third-party tools, Python code can be packaged into standalone executable programs.")    
        self.lines.append("3)The language provides constructs intended to enable clear programs on both a small and large scale.")
        self.lines.append("4)Python supports multiple programming paradigms, including object-oriented, imperative and functional programming styles.")
        self.lines.append("5)It features a dynamic type system and automatic memory management and has a large and comprehensive standard library.")
        self.lines.append("8)Python interpreters are available for many operating systems. ")
        self.lines.append("9)CPython, the reference implementation of Python, is free and          open source software and has a community-based development model.")       
        self.lines.append("1)Python is a popular general-purpose, high-level programming language whose design philosophy emphasizes code readability.")
        self.lines.append("2)Python's syntax allows programmers to express concepts in         fewer lines of code than would be possible in languages such as C.")
        self.cursorY = 0
        self.cursorX = 0

        self.offsetY = 0
        self.offsetX = 0

        self.selectX = 0
        self.selectY = 0

        self.isSelecting = False
  def draw(self):
    """Ova funkcija sluzi za iscrtavanje prozora"""
    # iscrtavanje
    self.maxy, self.maxx = self.stdscr.getmaxyx()

    self.stdscr.clear()

    for i, line in enumerate(self.lines.values()):
        # ograniciti prostor ispisa
        dy = i - self.offsetY+1
        visibleLine = line[self.offsetX:self.maxx + self.offsetX - 1]
        self.stdscr.addstr(dy, 0, visibleLine)
        if dy >= 0 and dy < self.maxy:

            for l in line:
                if self.isSelecting and self.selectY==i:

                    x1 = min(self.selectX, self.cursorX) 
                    x2 = max(self.selectX,self.cursorX)

                    SelectedLine = line [x1:x2] 
                    self.stdscr.chgat(dy, x1,len(SelectedLine), curses.color_pair(1))
    def right(self):
    """ Funkcija za pomeranje kursora desno."""
        self.maxy, self.maxx = self.stdscr.getmaxyx()
        line = self.lines.index(self.cursorY)
        if self.cursorX < len(line):
            if self.cursorX - self.offsetX == self.maxx - 1:
                self.offsetX += 1
            self.cursorX += 1
        elif self.cursorY != self.lines.size() - 1:
            self.home()
            self.down()

    def down(self):
    """Funkcija za pomeranje kursora dole."""
    if self.cursorY < self.lines.size() - 1:
        if self.cursorY - self.offsetY == self.maxy - 1:
            self.offsetY += 1
        self.cursorY += 1

        line = self.lines.index(self.cursorY)
        while self.cursorX > len(line):
            self.left()


def up(self):
    """Funkcija za pomeranje kursora gore."""
    if self.cursorY > 0:
        if self.offsetY == self.cursorY:
            self.offsetY -= 1
        self.cursorY -= 1

        line = self.lines.index(self.cursorY)
        while self.cursorX > len(line):
            self.left()


     def handleInput(self):

        ch = self.stdscr.getch()
        logging.error(ch)


        if ch == curses.KEY_LEFT:
            if self.isSelecting:
                self.isSelecting = False
            self.left()

        elif ch == curses.KEY_RIGHT:
            if self.isSelecting:
                self.isSelecting = False
            self.right()

如何选择文本?

最佳答案

我认为这是因为您启用了鼠标。

在选择时按住Shift,选择将正常进行。

关于python - ncurses 文本编辑器。需要帮助文本选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18570669/

相关文章:

c++ - PDCurses getch 不起作用

c++ - NCURSES:创建没有循环的标题栏

c - 在终端中隔离标准输入和标准输出

python /dpkt : Find out if packet is a tcp packet or a udp packet ,

python - PySpark:AttributeError: 'PipelineModel'对象没有属性 'clusterCenters'

python - 如何在 Django 上区分管理员和客户的用户(模型)

python - 沿任意维度切片 numpy 数组

python - TkInter 在文件名模式匹配中插入虚假通配符

c++ - 如何在 OSX 上为 ncurses 提供鼠标支持?

python - OSX iTerm2 可以将鼠标与 python-ncurses 一起使用,但不能与 C 中的curses一起使用