示例#1
0
class Editor:
    __text: str
    undo_stack: UndoStack

    def __init__(self, text: str):
        self.__text = text
        self.undo_stack = UndoStack()

    def get_text(self):
        return self.__text

    def select(self, start: int, end: int):
        if start > end or end >= len(self.__text):
            raise InvalidTextPositionError(
                "Требуемая позиция в тексте недоступна")
        return Selection(start, end)

    def find(self, what: str, after_pos: int = -1):
        return self.__text.find(what, after_pos + 1)

    def insert(self, selection: Selection, what: str):
        insert = InsertCommand(selection, what)
        self.__text = insert.do(self.__text)
        self.undo_stack.push(insert)

    def make_bold(self, selection: Selection):
        bold = MakeBoldCommand(selection)
        self.__text = bold.do(self.__text)
        self.undo_stack.push(bold)

    def make_italic(self, selection: Selection):
        italic = MakeItalicCommand(selection)
        self.__text = italic.do(self.__text)
        self.undo_stack.push(italic)

    def make_underline(self, selection: Selection):
        underline = MakeUnderlineCommand(selection)
        self.__text = underline.do(self.__text)
        self.undo_stack.push(underline)

    def undo(self):
        cmd = self.undo_stack.pop()
        if cmd is not None:
            self.__text = cmd.undo(self.__text)