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

时间:2013-09-02 10:11:57

标签: 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()

我如何选择文字?

1 个答案:

答案 0 :(得分:1)

这是因为我假设你的鼠标已启用。

按住 Shift ,同时选择和选择将正常工作。