def __init__(self, file):
self.file = file
self.scr = curses.initscr()
self.scr.border()
self.scr_height, self.scr_width = self.scr.getmaxyx()
self.text_win = curses.newwin(self.scr_height - 1, self.scr_width, 1, 0)
self.file_text = file.content
if self.file_text != None:
self.text_win.addstr(self.file_text)
curses.noecho()
#curses.start_color()
#curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN)
if self.file.exists:
self.start_editor()
else:
curses.endwin()
gc.error('An error occurred while editing this file.')
python类newwin()的实例源码
def __init__(self, username):
os.environ.setdefault('ESCDELAY', '25') # shorten esc delay
self.username = username
# set up IRC
self.channel = "##HTP"
# set up curses
self.scr = curses.initscr()
self.disconnect = False
curses.start_color()
self.scr_height, self.scr_width = self.scr.getmaxyx()
self.chatbar = curses.newwin(5, self.scr_height - 1, 5, 10)
self.msg_text = ''
self.logfile = '../data/irc.txt'
self.log_text = []
# curses color config
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_GREEN)
# start the client
try:
curses.wrapper(self.start_loop())
except Exception as e:
self.scr.addstr(2, 0, str(e), curses.A_REVERSE)
# client game loop
def init_layout(self):
"""Initialize the each windows with their size and shape"""
self.height, self.width = self.window.getmaxyx()
# Title section
self.window.addstr(0, 0, '[awesome-{}] Find awesome things!'.format(self.awesome_title), curses.color_pair(1))
self.window.hline(1, 0, curses.ACS_HLINE, self.width)
# Search result section
self.result_window = curses.newwin(self.height - 4, self.width, 2, 0)
self.result_window.keypad(True)
# Search bar section
self.window.hline(self.height - 2, 0, curses.ACS_HLINE, self.width)
self.window.addch(self.height - 1, 0, '>')
self.search_window = curses.newwin(1, self.width - 1, self.height - 1, 2)
self.search_window.keypad(True)
self.window.refresh()
def test_textpad(stdscr, insert_mode=False):
ncols, nlines = 8, 3
uly, ulx = 3, 2
if insert_mode:
mode = 'insert mode'
else:
mode = 'overwrite mode'
stdscr.addstr(uly-3, ulx, "Use Ctrl-G to end editing (%s)." % mode)
stdscr.addstr(uly-2, ulx, "Be sure to try typing in the lower-right corner.")
win = curses.newwin(nlines, ncols, uly, ulx)
textpad.rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols)
stdscr.refresh()
box = textpad.Textbox(win, insert_mode)
contents = box.edit()
stdscr.addstr(uly+ncols+2, 0, "Text entered in the box\n")
stdscr.addstr(repr(contents))
stdscr.addstr('\n')
stdscr.addstr('Press any key')
stdscr.getch()
for i in range(3):
stdscr.move(uly+ncols+2 + i, 0)
stdscr.clrtoeol()
def test_textpad(stdscr, insert_mode=False):
ncols, nlines = 8, 3
uly, ulx = 3, 2
if insert_mode:
mode = 'insert mode'
else:
mode = 'overwrite mode'
stdscr.addstr(uly-3, ulx, "Use Ctrl-G to end editing (%s)." % mode)
stdscr.addstr(uly-2, ulx, "Be sure to try typing in the lower-right corner.")
win = curses.newwin(nlines, ncols, uly, ulx)
textpad.rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols)
stdscr.refresh()
box = textpad.Textbox(win, insert_mode)
contents = box.edit()
stdscr.addstr(uly+ncols+2, 0, "Text entered in the box\n")
stdscr.addstr(repr(contents))
stdscr.addstr('\n')
stdscr.addstr('Press any key')
stdscr.getch()
for i in range(3):
stdscr.move(uly+ncols+2 + i, 0)
stdscr.clrtoeol()
def test_textpad(stdscr, insert_mode=False):
ncols, nlines = 8, 3
uly, ulx = 3, 2
if insert_mode:
mode = 'insert mode'
else:
mode = 'overwrite mode'
stdscr.addstr(uly-3, ulx, "Use Ctrl-G to end editing (%s)." % mode)
stdscr.addstr(uly-2, ulx, "Be sure to try typing in the lower-right corner.")
win = curses.newwin(nlines, ncols, uly, ulx)
textpad.rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols)
stdscr.refresh()
box = textpad.Textbox(win, insert_mode)
contents = box.edit()
stdscr.addstr(uly+ncols+2, 0, "Text entered in the box\n")
stdscr.addstr(repr(contents))
stdscr.addstr('\n')
stdscr.addstr('Press any key')
stdscr.getch()
for i in range(3):
stdscr.move(uly+ncols+2 + i, 0)
stdscr.clrtoeol()
def test_textpad(stdscr, insert_mode=False):
ncols, nlines = 8, 3
uly, ulx = 3, 2
if insert_mode:
mode = 'insert mode'
else:
mode = 'overwrite mode'
stdscr.addstr(uly-3, ulx, "Use Ctrl-G to end editing (%s)." % mode)
stdscr.addstr(uly-2, ulx, "Be sure to try typing in the lower-right corner.")
win = curses.newwin(nlines, ncols, uly, ulx)
textpad.rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols)
stdscr.refresh()
box = textpad.Textbox(win, insert_mode)
contents = box.edit()
stdscr.addstr(uly+ncols+2, 0, "Text entered in the box\n")
stdscr.addstr(repr(contents))
stdscr.addstr('\n')
stdscr.addstr('Press any key')
stdscr.getch()
for i in range(3):
stdscr.move(uly+ncols+2 + i, 0)
stdscr.clrtoeol()
def test_textpad(stdscr, insert_mode=False):
ncols, nlines = 8, 3
uly, ulx = 3, 2
if insert_mode:
mode = 'insert mode'
else:
mode = 'overwrite mode'
stdscr.addstr(uly-3, ulx, "Use Ctrl-G to end editing (%s)." % mode)
stdscr.addstr(uly-2, ulx, "Be sure to try typing in the lower-right corner.")
win = curses.newwin(nlines, ncols, uly, ulx)
textpad.rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols)
stdscr.refresh()
box = textpad.Textbox(win, insert_mode)
contents = box.edit()
stdscr.addstr(uly+ncols+2, 0, "Text entered in the box\n")
stdscr.addstr(repr(contents))
stdscr.addstr('\n')
stdscr.addstr('Press any key')
stdscr.getch()
for i in range(3):
stdscr.move(uly+ncols+2 + i, 0)
stdscr.clrtoeol()
def test_textpad(stdscr, insert_mode=False):
ncols, nlines = 8, 3
uly, ulx = 3, 2
if insert_mode:
mode = 'insert mode'
else:
mode = 'overwrite mode'
stdscr.addstr(uly-3, ulx, "Use Ctrl-G to end editing (%s)." % mode)
stdscr.addstr(uly-2, ulx, "Be sure to try typing in the lower-right corner.")
win = curses.newwin(nlines, ncols, uly, ulx)
textpad.rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols)
stdscr.refresh()
box = textpad.Textbox(win, insert_mode)
contents = box.edit()
stdscr.addstr(uly+ncols+2, 0, "Text entered in the box\n")
stdscr.addstr(repr(contents))
stdscr.addstr('\n')
stdscr.addstr('Press any key')
stdscr.getch()
for i in range(3):
stdscr.move(uly+ncols+2 + i, 0)
stdscr.clrtoeol()
def test_textpad(stdscr, insert_mode=False):
ncols, nlines = 8, 3
uly, ulx = 3, 2
if insert_mode:
mode = 'insert mode'
else:
mode = 'overwrite mode'
stdscr.addstr(uly-3, ulx, "Use Ctrl-G to end editing (%s)." % mode)
stdscr.addstr(uly-2, ulx, "Be sure to try typing in the lower-right corner.")
win = curses.newwin(nlines, ncols, uly, ulx)
textpad.rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols)
stdscr.refresh()
box = textpad.Textbox(win, insert_mode)
contents = box.edit()
stdscr.addstr(uly+ncols+2, 0, "Text entered in the box\n")
stdscr.addstr(repr(contents))
stdscr.addstr('\n')
stdscr.addstr('Press any key')
stdscr.getch()
for i in range(3):
stdscr.move(uly+ncols+2 + i, 0)
stdscr.clrtoeol()
def top(args):
scr = curses.initscr()
#scr.start_color()
curses.noecho()
curses.cbreak()
hx, wm = scr.getmaxyx()
scr.keypad(True)
try:
header = curses.newwin(HEADER_SIZE, wm, 0, 0)
body = curses.newwin(BODY_SIZE, wm, HEADER_SIZE, 0)
while True:
draw_header(header)
draw_body(body)
draw_footer(scr)
sleep(0.2)
except KeyboardInterrupt:
curses.nocbreak()
scr.keypad(False)
curses.echo()
curses.endwin()
def test_textpad(stdscr, insert_mode=False):
ncols, nlines = 8, 3
uly, ulx = 3, 2
if insert_mode:
mode = 'insert mode'
else:
mode = 'overwrite mode'
stdscr.addstr(uly-3, ulx, "Use Ctrl-G to end editing (%s)." % mode)
stdscr.addstr(uly-2, ulx, "Be sure to try typing in the lower-right corner.")
win = curses.newwin(nlines, ncols, uly, ulx)
textpad.rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols)
stdscr.refresh()
box = textpad.Textbox(win, insert_mode)
contents = box.edit()
stdscr.addstr(uly+ncols+2, 0, "Text entered in the box\n")
stdscr.addstr(repr(contents))
stdscr.addstr('\n')
stdscr.addstr('Press any key')
stdscr.getch()
for i in range(3):
stdscr.move(uly+ncols+2 + i, 0)
stdscr.clrtoeol()
def _wrapped_run(self, stdscr):
self.left_width = 60
self.right_width = curses.COLS - self.left_width
# Setup windows
self.top = curses.newwin(2, curses.COLS, 0, 0)
self.left = curses.newpad(curses.LINES * 2, self.left_width)
self.right = curses.newwin(curses.LINES - 4, self.right_width, 2, self.left_width)
self.bottom = curses.newwin(2, curses.COLS, curses.LINES - 2, 0)
Color.setup_palette()
# Load some data and redraw
self.fetch_next()
self.selected = 0
self.full_redraw()
self.loop()
def __init__(self, parent, x, y, w=None, h=None):
self._children = []
self._parent = parent
self._x = x
self._y = y
self._h = h
self._w = w
self._vcenter, self._hcenter = False, False
self.set_size(w, h)
ax, ay = self.get_screen_coords(0, 0)
self._wnd = curses.newwin(self._h, self._w, ay, ax)
if parent is not None:
parent._children.append(self)
def __init__(self, stdscr, label="", x=0, y=0, width=10, height=1):
self.default_color = 'white-black'
self.stdscr = stdscr
self.label = label
self.x, self.y = x, y
self.width, self.height = width, height
self.borderbox = curses.newwin(height + 2, width + 2, y - 1, x - 1)
self.textinpt = curses.newwin(height, width, y, x)
self.textinpt.bkgd(' ', colors.get_colorpair(self.default_color))
self.textinpt.keypad(1)
self.refresh()
def test_editbox(stdscr):
ncols, nlines = 9, 4
uly, ulx = 15, 20
stdscr.addstr(uly-2, ulx, "Use Ctrl-G to end editing.")
win = curses.newwin(nlines, ncols, uly, ulx)
rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols)
stdscr.refresh()
return Textbox(win).edit()
def enter_edit_mode(self, value=None):
if self.items[self.position]['field'] == 'Comments':
editwin = curses.newwin(10, 60, self.position+2, 27)
rectangle(self.window, self.position + 1, 26, self.position + 12, 26 + 61)
else:
editwin = curses.newwin(1, 30, self.position+2, 27)
editwin.attron(curses.color_pair(2))
curses.curs_set(1)
if value:
box = _Textbox(editwin, True, text=value)
else:
box = _Textbox(editwin, True, text=self.items[self.position]['value'])
_Textbox.stripspaces = True
self.window.refresh()
while True:
edit_field = box.edit()
if not edit_field is None:
result = self.validate(edit_field.strip())
if result:
self.navigate(1)
break
else:
break
curses.curs_set(0)
self.window.clear()
def sub_selection_dialog(self, pos):
"""
Draw a `Selection Dialog` on screen used to make configurations.
:param pos: Index of selected item in `Configure Setting` frame.
:type pos: int
.. warning:: The value of `pos` MUST NOT be `None`.
:return: A **WindowObject** which represents the selection dialog.
:rtype: WindowObject
"""
i_len = len(self.settings[pos][2])
# Draw Shadow
shadow = curses.newwin(i_len + 2, 18, 13 - i_len / 2, 31)
shadow.bkgd(' ', curses.color_pair(8))
shadow.refresh()
# Draw Subwindow
screen = curses.newwin(i_len + 2, 18, 12 - i_len / 2, 30)
screen.box()
screen.bkgd(' ', curses.color_pair(1))
screen.keypad(1)
# Set local variable
normal = curses.A_NORMAL
# Title of Subwindow
screen.addstr(0, 3, self.settings[pos][0].center(12), normal)
return screen
def __init__( self, x, y, width, height, fg=curses.COLOR_BLACK, bg=curses.COLOR_WHITE ):
self.win = curses.newwin( height, width, y, x )
self.dimensions = ( x, y, width, height )
"""
if curses.has_colors():
color = 1
curses.init_pair( color, fg, bg )
self.win.bkgdset( ord(' '), curses.color_pair(color) )
else:
self.win.bkgdset( ord(' '), curses.A_BOLD )
"""
self.erase()
self.setScrolling()
self.win.noutrefresh()
def __init__(self, screen):
self.screen = screen
(max_y, max_x) = self.screen.getmaxyx()
self.window = curses.newwin(5, max_x, self.row, 0)
self.status = ''
self.banner = Banner(self.window, self.banner_prefix)
# Widgets
self.widgets = {
'X+': Button(self.window, 'X+', 3, 6),
'X-': Button(self.window, 'X-', 3, 0),
'Y+': Button(self.window, 'Y+', 2, 3),
'Y-': Button(self.window, 'Y-', 4, 3),
'Z+': Button(self.window, 'Z+', 2, 11),
'Z-': Button(self.window, 'Z-', 4, 11),
'jog': NumberLabel(self.window, 1, 6, 0.001),
'MPosX': NumberLabel(self.window, 2, 21),
'MPosY': NumberLabel(self.window, 3, 21),
'MPosZ': NumberLabel(self.window, 4, 21),
'WPosX': NumberLabel(self.window, 2, 31),
'WPosY': NumberLabel(self.window, 3, 31),
'WPosZ': NumberLabel(self.window, 4, 31),
'feed_rate': Label(self.window, 2, 44, len=20, text='?', prefix='Feed Rate: '),
'spindle': Label(self.window, 3, 44, len=20, text='?', prefix='Spindle: '),
}
self.render()
self.refresh()
def init_window(self, row, height):
self.window = curses.newwin(height, self.screen.getmaxyx()[1], row, 0)
self.banner = Banner(self.window, self.title)
def test_editbox(stdscr):
ncols, nlines = 9, 4
uly, ulx = 15, 20
stdscr.addstr(uly-2, ulx, "Use Ctrl-G to end editing.")
win = curses.newwin(nlines, ncols, uly, ulx)
rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols)
stdscr.refresh()
return Textbox(win).edit()
def __init__(self, title, w, h, y, x):
self.win = curses.newwin(h, w, y, x)
self.win.border(0)
self.win.addstr(0, 1, title)
def __init__(self, mainwindow, game_menu):
self.main = mainwindow
self.game = game_menu
size = mainwindow.getmaxyx()
self.win = curses.newwin(size[0]-5, PREVIEW_WIDTH, 4, GAME_WIDTH + SYSTEM_WIDTH + 2)
self.last_game_loaded = None
def __init__(self):
self.swin = curses.newwin(4, 59, 0, 0)
self.inp = curses.newwin(1, 55, 2, 2)
self.text = textpad.Textbox(self.inp, insert_mode=False)
self.history_point = 0
self.search_history = collections.deque(maxlen=100)
def __init__(self, mainwindow):
self.main = mainwindow
size = mainwindow.getmaxyx()
self.syswin = curses.newwin(size[0]-5, SYSTEM_WIDTH, 4, 0)
self.gameswin = curses.newwin(size[0]-5, GAME_WIDTH, 4, SYSTEM_WIDTH)
self.offset = 0
self.pos = 0
self.search_pos = 0
def __init__(self, ticks, silent, debug, compat_debug, debug_lines, autostep_debug, head):
super().__init__()
self.ticks = ticks
self.silent = silent
self.debug = debug
self.compat_debug = compat_debug
self.debug_lines = debug_lines
self.autostep_debug = autostep_debug
self.head = head
self.tick_number = 0
self.output_count = 0
if self.debug and not self.compat_debug:
self.logging_loc = 0
self.logging_x = 1
self.stdscr = curses.initscr()
curses.start_color()
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK)
curses.noecho()
curses.curs_set(False)
self.win_program = curses.newwin(self.debug_lines, curses.COLS - 1, 0, 0)
self.logging_pad = curses.newpad(1000, curses.COLS - 1)
def signal_handler(signal, frame):
self.on_finish()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def newlinebox(h, w, y, x, title=None, line=True, opts=None):
wb = curses.newwin(2, w, y-1, x)
wb.keypad(True)
if line:
wb.addstr(0, 0, e(HORLINE)*w, 0 if opts is None else opts)
if title is not None:
wb.addstr(0, 2, e(title), 0 if opts is None else opts)
pn = curses.panel.new_panel(wb)
wb.refresh()
win = curses.newwin(h, w, y, x)
win.keypad(True)
panel = curses.panel.new_panel(win)
return win, panel, wb, pn
def test_editbox(stdscr):
ncols, nlines = 9, 4
uly, ulx = 15, 20
stdscr.addstr(uly-2, ulx, "Use Ctrl-G to end editing.")
win = curses.newwin(nlines, ncols, uly, ulx)
rectangle(stdscr, uly-1, ulx-1, uly + nlines, ulx + ncols)
stdscr.refresh()
return Textbox(win).edit()
def mkpanel(color, rows, cols, tly, tlx):
win = curses.newwin(rows, cols, tly, tlx)
pan = panel.new_panel(win)
if curses.has_colors():
if color == curses.COLOR_BLUE:
fg = curses.COLOR_WHITE
else:
fg = curses.COLOR_BLACK
bg = color
curses.init_pair(color, fg, bg)
win.bkgdset(ord(' '), curses.color_pair(color))
else:
win.bkgdset(ord(' '), curses.A_BOLD)
return pan