Advice on textuser inteface modules.

This is the place for queries that don't fit in any of the other categories.

Advice on textuser inteface modules.

Postby Emilnd » Tue May 28, 2013 10:49 am

Hey everyone!

I have decided to pick up coding now after a 3 months break. What I want to do is finish a project I started on a while back and it is and text editor for use in terminals (nano, vi etc.).
The main goal I have and reason to not use named editors is that I love the typing features of and want to bring those into the terminal.
Some examples of typing feature is like:
# If you make one "(", you will automatically get both "(" and ")", and the cursor placed in the middle of them.
# When you make a ",". There will be an space inserted afterwards and the cursor is placed after it.
# If you press ctrl+d you will duplicate that whole line downwards.

These are not perhaps the best practices for overall coding but I really like it this way and don't see why I should learn any other.
Then ofcourse I would like to add some highlighting and line numbers and other basic things. In the end it will probably look like Nano with line numbers and aswell the use of wanted features..

Anyway, what I need your help for is to find a good library that can help me with this. Ofcourse the first which popped up in my head was curses and I have made a pretty good prototype with it, though curses it self seems to contain a bunch of bugs? And aswell a very incomplete set of basics? Like turn on and off insert mode! I will show you with he code I have written soon.
I have tried some urwid, but their example of a text editor is very hard to understand and when I seaked help in their irc channel they told me it was a very lazy written code, really nothing more hehe.

So I just really need advice on how I can get going. Because I really need this for use on my servers where I more and more impeltent python scripts.

Here is the prototype I have written. If you try it you will see weird things like backspace clears the whole row and some kind of insert mode is always on, which I don't use at all.
The features I have made works pretty good though, so make sure to try them out, type "," and "(" or try out ctrl+d ;)

I am using Python 2.7.5 on Linux.

Here is the code from pastebin
Code: Select all
# Sorry for poor documentation and at the end I probably smashed in some
# things that don't need to be here at all.

import curses, curses.textpad, curses.ascii

class sheet:
    def __init__(self, win): = win
        self.gridY = win.getmaxyx()[0]
        self.gridX = win.getmaxyx()[1]
    def end(self, gridPosY):
        last = self.gridX
        while True:
            if curses.ascii.ascii(, last)) != curses.ascii.SP:
                print curses.ascii.ascii(, last))
                last = min(self.gridX, last+1)
            elif last == 0:
            last = last - 1
        return last
    def compose(self):
        endIndex = {}
        for i in range(0, self.gridY):
            endIndex[str(i)] = 0
        while 1:
            gridPosY, gridPosX =[0],[1]
            ch =
            if ch == ord(curses.ascii.ctrl('o')):
                print, gridPosX)
            elif ch == ord(curses.ascii.ctrl('c')):
            elif ch == ord(curses.ascii.ctrl('d')):
                bucket = []
                for i in range(0, self.gridX):
                    seed =, i)
       + 1, 0)
                for i in range(0, len(bucket)):
                    if gridPosX + 1 != self.gridX :
              , i)
            elif ch == curses.KEY_END:
                last = self.end(gridPosY)
                print last
      , last)
            elif ch == curses.KEY_ENTER:
                endIndex[str(gridPosY)] = gridPosX
            elif ch == curses.KEY_LEFT:
                endIndex[str(gridPosY)] = gridPosX
                if gridPosX > 0:
          , gridPosX - 1)
            elif ch == curses.KEY_RIGHT:
                endIndex[str(gridPosY)] = gridPosX
                if gridPosX + 1 != self.gridX :
          , gridPosX + 1)
            elif ch == curses.KEY_UP:
                endIndex[str(gridPosY)] = gridPosX
                if gridPosY > 0:
           - 1, endIndex[str(gridPosY - 1)])
            elif ch == curses.KEY_DOWN:
                endIndex[str(gridPosY)] = gridPosX
       + 1, endIndex[str(gridPosY + 1)])
            elif ch == ord('\t'): # Tabs
      ' ', 4)
      , gridPosX + 4)
            elif ch == ord('('): # (cursor)
      , gridPosX + 1)
            elif ch == ord('"'): # "cursor"
      , gridPosX + 1)
            elif ch == ord("'"): # 'cursor'
      , gridPosX + 1)
            elif ch == ord(','): # Make space after ","
      ,' ')
      , gridPosX + 2)
      , gridPosX - 1)
            elif ch in (127, curses.KEY_BACKSPACE): # Backspace
                if gridPosX > 0:
          , gridPosX - 1)
    def gather(self):
        # collect and save some shit, attrs!!!!
    def drawInterface(self):
        myScreen_gridY, myScreen_gridX = myscreen.getmaxyx()[0], myscreen.getmaxyx()[1]
        for i in range(0, myScreen_gridY - 2 - 1):
            if i < 10:
                myscreen.addstr(2 + i, 2, str(i))
                myscreen.addstr(2 + i, 1, str(i))
        myscreen.addstr(0, 5, "EngeboEditor - v0.0.1")
        curses.textpad.rectangle(myscreen, 1, 4, myScreen_gridY - 1, myScreen_gridX - 2)

myscreen = curses.initscr()

win = curses.newwin(myscreen.getmaxyx()[0] - 3, myscreen.getmaxyx()[1] - 7, 2, 5)

Last edited by Yoriz on Tue May 28, 2013 10:56 am, edited 1 time in total.
Reason: Added the code in code tags
Posts: 1
Joined: Mon May 13, 2013 5:31 pm

Re: Advice on textuser inteface modules.

Postby micseydel » Tue May 28, 2013 11:18 am

If I were told about this and had to get started, curses would be the first and really only thing that came to my mind. That said, have you seen this? It's just another option on top of what you mentioned you've already come across, but it's definitely another option.
Due to the reasons discussed here we will be moving to on October 1, 2016.

This forum will be locked down and no one will be able to post/edit/create threads, etc. here from thereafter. Please create an account at the new site to continue discussion.
User avatar
Posts: 3000
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Return to General Coding Help

Who is online

Users browsing this forum: Google [Bot], Yahoo [Bot] and 14 guests