Created
September 27, 2012 17:25
-
-
Save pudquick/3795253 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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