Skip to content

Instantly share code, notes, and snippets.

@pudquick
Created September 27, 2012 17:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pudquick/3795253 to your computer and use it in GitHub Desktop.
Save pudquick/3795253 to your computer and use it in GitHub Desktop.
# Workaround for default editable text entries on OS X .. sadly relies on curses
import curses, curses.textpad
from curses.textpad import Textbox
screen = curses.initscr()
curses.savetty()
curses.noecho()
class BackspaceTextbox(Textbox):
def do_command(self, ch):
# Correct delete so it works with ^? (standard Backspace mapping in Terminal)
(y, x) = self.win.getyx()
if ch == 127:
self.lastcmd = ch
if x > 0:
self.win.move(y, x-1)
elif y == 0:
pass
elif self.stripspaces:
self.win.move(y-1, self._end_of_line(y-1))
else:
self.win.move(y-1, self.maxx)
self.win.delch()
return 1
return Textbox.do_command(self, ch)
def maketextbox(value=""):
nw = curses.newwin(1,0,0,0)
txtbox = BackspaceTextbox(nw)
nw.addstr(0,0,value,0)
screen.refresh()
return txtbox
foo = maketextbox("default")
text = foo.edit()
curses.resetty()
curses.endwin()
print "Entered data:", text
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment