def moveDown(editorObj): """ Moves down one line """ y = editorObj.editorscr.getyx()[0] if editorObj.currentLine.nextNode is None: # we don't have any more nodes return currentLineHeight = editorUtil.lineHeight(editorObj.editorscr, editorObj.currentLine) if y + currentLineHeight < editorObj.editorscr.getmaxyx()[0] - 2: editorObj.currentLine = editorObj.currentLine.nextNode elif editorObj.currentLine.nextNode is not None: editorObj.currentLine = editorObj.currentLine.nextNode amountToMoveDown = editorUtil.lineHeight(editorObj.editorscr, editorObj.currentLine) while amountToMoveDown > 0: amountToMoveDown -= editorUtil.lineHeight(editorObj.editorscr, editorObj.topLine) editorObj.topLine = editorObj.topLine.nextNode editorObj.topLineCount += 1 if editorObj.currentLineIndex > len(editorObj.currentLine.value) - 2: editorObj.currentLineIndex = len(editorObj.currentLine.value) - 2 if editorObj.currentLineIndex < 0: editorObj.currentLineIndex = 0
def moveUp(fileNavObj, y): """ Move up one step """ y -= editorUtil.lineHeight(fileNavObj.filenavscr, fileNavObj.currentDir.lastNode) # remember you have ' -' at the start of every dir if y < 0: temp = fileNavObj.topDir fileNavObj.topDir = fileNavObj.topDir.lastNode if fileNavObj.topDir is None: fileNavObj.topDir = temp fileNavObj.currentDir = fileNavObj.currentDir.nextNode y += editorUtil.lineHeight(fileNavObj.filenavscr, fileNavObj.currentDir.lastNode) if fileNavObj.currentDir is not fileNavObj.dirs.start: fileNavObj.currentDir = fileNavObj.currentDir.lastNode return y
def moveDown(fileNavObj, y): """ Move down one step """ y += editorUtil.lineHeight(fileNavObj.filenavscr, fileNavObj.currentDir) if fileNavObj.currentDir.nextNode is None: # if we are at the end y -= editorUtil.lineHeight(fileNavObj.filenavscr, fileNavObj.currentDir) if y > fileNavObj.filenavscr.getmaxyx()[0]-2: y -= editorUtil.lineHeight(fileNavObj.filenavscr, fileNavObj.currentDir) fileNavObj.topDir = fileNavObj.topDir.nextNode if fileNavObj.currentDir is not fileNavObj.dirs.end: fileNavObj.currentDir = fileNavObj.currentDir.nextNode return y
def drawLineNumbers(editorObj): """ Draws the line numbers on editorscr, using the corresponding linenumscr. Only relevant for use by linenumscr. """ # clear old data off the screen editorObj.linenumscr.clear() (moveY, moveX) = (0, 0) # draw line numbers lineToDraw = editorObj.topLine y = 0 lineIndex = editorObj.topLineCount editorObj.linenumscr.move(0, 0) while y < editorObj.linenumscr.getmaxyx()[0] - 1: editorObj.linenumscr.addstr(str(lineIndex)) if lineToDraw == editorObj.currentLine: moveY = y + editorObj.currentLineIndex // editorObj.editorscr.getmaxyx( )[1] for i in range(editorObj.currentLineIndex): c = editorObj.currentLine.value[i] if c == '\t': moveX += 8 else: moveX += 1 if moveX > editorObj.editorscr.getmaxyx()[1]: moveX -= editorObj.editorscr.getmaxyx()[1] moveX = moveX % editorObj.editorscr.getmaxyx()[1] editorObj.currentLineCount = lineIndex if moveX <= -1: moveX = 0 y += editorUtil.lineHeight(editorObj.editorscr, lineToDraw) if lineToDraw.nextNode is None: # ran out of nodes break lineToDraw = lineToDraw.nextNode lineIndex += 1 if y > editorObj.linenumscr.getmaxyx()[0] - 1: break editorObj.linenumscr.move(y, 0) editorObj.editorscr.move(moveY, moveX)
def drawLines(editorObj, scr, topLine): """ Takes in a scr object from which it draws on Draws the line numbers and the lines themselves onto the ui O(n) where n is the number of blocks that can fit onto the terminal, but since n is very small and theta(n) is a fraction of n usually < n/2 this is fine. """ # clear the old data off of the screen scr.clear() # draw the lines themselves lineToDraw = topLine scr.move(0, 0) cursorY = 0 while lineToDraw is not None: if scr.getyx()[0] + editorUtil.lineHeight( editorObj.editorscr, lineToDraw) > scr.getmaxyx()[0] - 1: # handle no space at bottom when scrolling up scr.addstr('@') break i = 0 for c in lineToDraw.value: colorToDraw = lineToDraw.colors[i] scr.addstr(c, curses.color_pair(colorToDraw)) currentX = scr.getyx()[1] if currentX + 1 > scr.getmaxyx()[1] - 1: # if we have reached the end of the line horizontally cursorY += 1 scr.move(cursorY, 0) i += 1 if scr.getyx()[0] + 1 > scr.getmaxyx()[0] - 1: break scr.move(cursorY + 1, 0) cursorY += 1 lineToDraw = lineToDraw.nextNode # move cursor to where it should be as # specified by currentLine and currentLineIndex, refresh and move on editorObj.linenumscr.refresh() scr.refresh()
def setColors(editorObj, colorMap): """ Sets the colors of the editorObj for every line in editorObj.lineLinkedList. Assumes every file is a python file, need to use different lexers later (possibly check with something else then pass it in as an argument). colorMap is a dictionary which maps the kind of syntax to a certain color. Use this with editorscr only! Notice that this won't work perfectly; there will be times that the syntax highlighting won't be perfect. This is a decided tradeoff; the popular text editors don't seem to have a solution. """ pylex = Python3Lexer(stripnl=False, stripall=False) # lets build the string to parse syntax = '' walk = editorObj.lineLinkedList.start y = 0 increaseY = False while walk is not None: if walk == editorObj.currentLine: increaseY = True syntax += walk.value if increaseY: y += editorUtil.lineHeight(editorObj.editorscr, walk) if y > editorObj.editorscr.getmaxyx()[0]: break walk = walk.nextNode walk = editorObj.lineLinkedList.start i = 0 # index of where i am walking through the string #kill(editorObj) #for token in pylex.get_tokens(syntax): # print(token,end='\r\n') #assert(False) for token in pylex.get_tokens(syntax): if token[1] == '\n': walk = walk.nextNode #i = -1 i = 0 if walk is None: break continue tokenType = colorMap[ 'TEXT'] # assume everything is text and find contradiction if pygments.token.Comment.Single is token[0]: tokenType = colorMap['COMMENT'] if pygments.token.Keyword.Namespace is token[0]: tokenType = colorMap['NAMESPACE'] if pygments.token.Keyword is token[0]: tokenType = colorMap['KEYWORD'] if pygments.token.Name.Builtin is token[0]: tokenType = colorMap['BUILTIN'] if pygments.token.Name.Function is token[0]: tokenType = colorMap['FUNCTION'] if pygments.token.Literal.Number.Integer is token[ 0] or pygments.token.Literal.Number.Float is token[0]: tokenType = colorMap['LITERAL'] if pygments.token.Operator is token[0]: tokenType = colorMap['OPERATOR'] if (pygments.token.Literal.String.Single is token[0] or pygments.token.Literal.String.Double is token[0]): tokenType = colorMap['STRING_LITERAL'] if pygments.token.Literal.String.Doc is token[0]: tokenType = colorMap['STRING_LITERAL'] if token[0] is pygments.token.Literal.String.Doc: for tok in token[1].split('\n'): for c in tok: try: walk.colors[i] = tokenType except: a = 1 # do nothing i += 1 i = 0 walk = walk.nextNode # if we were at the end break if walk is None: break # set everything back to the way it was before walk = walk.lastNode i = -1 continue for c in token[1]: try: walk.colors[i] = tokenType i += 1 except: a = 1 # do nothing