I am done

This commit is contained in:
2024-10-30 22:14:35 +01:00
parent 720dc28c09
commit 40e2a747cf
36901 changed files with 5011519 additions and 0 deletions

View File

@ -0,0 +1,16 @@
from pyreadline3.py3k_compat import is_ironpython
if is_ironpython:
try:
from .ironpython_console import *
except ImportError as x:
raise ImportError(
"Could not find a console implementation for local " "ironpython version"
) from x
else:
try:
from .console import *
except ImportError as x:
raise ImportError(
"Could not find a console implementation for local " "python version"
) from x

View File

@ -0,0 +1,253 @@
# -*- coding: ISO-8859-1 -*-
import os
import re
import sys
terminal_escape = re.compile("(\001?\033\\[[0-9;]*m\002?)")
escape_parts = re.compile("\001?\033\\[([0-9;]*)m\002?")
class AnsiState(object):
def __init__(
self,
bold=False,
inverse=False,
color="white",
background="black",
backgroundbold=False,
):
self.bold = bold
self.inverse = inverse
self.color = color
self.background = background
self.backgroundbold = backgroundbold
trtable = {
"black": 0,
"red": 4,
"green": 2,
"yellow": 6,
"blue": 1,
"magenta": 5,
"cyan": 3,
"white": 7,
}
revtable = dict(zip(trtable.values(), trtable.keys()))
def get_winattr(self):
attr = 0
if self.bold:
attr |= 0x0008
if self.backgroundbold:
attr |= 0x0080
if self.inverse:
attr |= 0x4000
attr |= self.trtable[self.color]
attr |= self.trtable[self.background] << 4
return attr
def set_winattr(self, attr):
self.bold = bool(attr & 0x0008)
self.backgroundbold = bool(attr & 0x0080)
self.inverse = bool(attr & 0x4000)
self.color = self.revtable[attr & 0x0007]
self.background = self.revtable[(attr & 0x0070) >> 4]
winattr = property(get_winattr, set_winattr)
def __repr__(self):
return (
"AnsiState(bold=%s,inverse=%s,color=%9s,"
"background=%9s,backgroundbold=%s)# 0x%x"
% (
self.bold,
self.inverse,
'"%s"' % self.color,
'"%s"' % self.background,
self.backgroundbold,
self.winattr,
)
)
def copy(self):
x = AnsiState()
x.bold = self.bold
x.inverse = self.inverse
x.color = self.color
x.background = self.background
x.backgroundbold = self.backgroundbold
return x
defaultstate = AnsiState(False, False, "white")
trtable = {
0: "black",
1: "red",
2: "green",
3: "yellow",
4: "blue",
5: "magenta",
6: "cyan",
7: "white",
}
class AnsiWriter(object):
def __init__(self, default=defaultstate):
if isinstance(defaultstate, AnsiState):
self.defaultstate = default
else:
self.defaultstate = AnsiState()
self.defaultstate.winattr = defaultstate
def write_color(self, text, attr=None):
"""write text at current cursor position and interpret color escapes.
return the number of characters written.
"""
if isinstance(attr, AnsiState):
defaultstate = attr
elif attr is None: # use attribute form initial console
attr = self.defaultstate.copy()
else:
defaultstate = AnsiState()
defaultstate.winattr = attr
attr = defaultstate
chunks = terminal_escape.split(text)
n = 0 # count the characters we actually write, omitting the escapes
res = []
for chunk in chunks:
m = escape_parts.match(chunk)
if m:
parts = m.group(1).split(";")
if len(parts) == 1 and parts[0] == "0":
attr = self.defaultstate.copy()
continue
for part in parts:
if part == "0": # No text attribute
attr = self.defaultstate.copy()
attr.bold = False
elif part == "7": # switch on reverse
attr.inverse = True
# switch on bold (i.e. intensify foreground color)
elif part == "1":
attr.bold = True
elif len(part) == 2:
if part == "22": # Normal foreground color
attr.bold = False
elif "30" <= part <= "37": # set foreground color
attr.color = trtable[int(part) - 30]
elif part == "39": # Default foreground color
attr.color = self.defaultstate.color
elif "40" <= part <= "47": # set background color
attr.background = trtable[int(part) - 40]
elif part == "49": # Default background color
attr.background = self.defaultstate.background
continue
n += len(chunk)
res.append((attr.copy(), chunk))
return n, res
def parse_color(self, text, attr=None):
n, res = self.write_color(text, attr)
return n, [attr.winattr for attr, text in res]
def write_color(text, attr=None):
a = AnsiWriter(defaultstate)
return a.write_color(text, attr)
def write_color_old(text, attr=None):
"""write text at current cursor position and interpret color escapes.
return the number of characters written.
"""
res = []
chunks = terminal_escape.split(text)
n = 0 # count the characters we actually write, omitting the escapes
if attr is None: # use attribute from initial console
attr = 15
for chunk in chunks:
m = escape_parts.match(chunk)
if m:
for part in m.group(1).split(";"):
if part == "0": # No text attribute
attr = 0
elif part == "7": # switch on reverse
attr |= 0x4000
# switch on bold (i.e. intensify foreground color)
if part == "1":
attr |= 0x08
elif len(part) == 2 and "30" <= part <= "37": # set foreground color
part = int(part) - 30
# we have to mirror bits
attr = (
(attr & ~0x07)
| ((part & 0x1) << 2)
| (part & 0x2)
| ((part & 0x4) >> 2)
)
elif len(part) == 2 and "40" <= part <= "47": # set background color
part = int(part) - 40
# we have to mirror bits
attr = (
(attr & ~0x70)
| ((part & 0x1) << 6)
| ((part & 0x2) << 4)
| ((part & 0x4) << 2)
)
# ignore blink, underline and anything we don't understand
continue
n += len(chunk)
if chunk:
res.append(("0x%x" % attr, chunk))
return res
# trtable={0:"black",1:"red",2:"green",3:"yellow",4:"blue",5:"magenta",6:"cyan",7:"white"}
if __name__ == "__main__x":
import pprint
pprint = pprint.pprint
s = "\033[0;31mred\033[0;32mgreen\033[0;33myellow\033[0;34mblue\033[0;35mmagenta\033[0;36mcyan\033[0;37mwhite\033[0m"
pprint(write_color(s))
pprint(write_color_old(s))
s = "\033[1;31mred\033[1;32mgreen\033[1;33myellow\033[1;34mblue\033[1;35mmagenta\033[1;36mcyan\033[1;37mwhite\033[0m"
pprint(write_color(s))
pprint(write_color_old(s))
s = "\033[0;7;31mred\033[0;7;32mgreen\033[0;7;33myellow\033[0;7;34mblue\033[0;7;35mmagenta\033[0;7;36mcyan\033[0;7;37mwhite\033[0m"
pprint(write_color(s))
pprint(write_color_old(s))
s = "\033[1;7;31mred\033[1;7;32mgreen\033[1;7;33myellow\033[1;7;34mblue\033[1;7;35mmagenta\033[1;7;36mcyan\033[1;7;37mwhite\033[0m"
pprint(write_color(s))
pprint(write_color_old(s))
if __name__ == "__main__":
import pprint
import console
pprint = pprint.pprint
c = console.Console()
c.write_color("dhsjdhs")
c.write_color("\033[0;32mIn [\033[1;32m1\033[0;32m]:")
print
pprint(write_color("\033[0;32mIn [\033[1;32m1\033[0;32m]:"))
if __name__ == "__main__x":
import pprint
pprint = pprint.pprint
s = "\033[0;31mred\033[0;32mgreen\033[0;33myellow\033[0;34mblue\033[0;35mmagenta\033[0;36mcyan\033[0;37mwhite\033[0m"
pprint(write_color(s))

View File

@ -0,0 +1,901 @@
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (C) 2003-2006 Gary Bishop.
# Copyright (C) 2006-2020 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
# Copyright (C) 2020 Bassem Girgis. <brgirgis@gmail.com>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
# *****************************************************************************
from .event import Event
"""Cursor control and color for the Windows console.
This was modeled after the C extension of the same name by Fredrik Lundh.
"""
# primitive debug printing that won't interfere with the screen
import os
import re
import sys
import traceback
import pyreadline3.unicode_helper as unicode_helper
from pyreadline3.console.ansi import AnsiState, AnsiWriter
from pyreadline3.keysyms import KeyPress, make_KeyPress
from pyreadline3.logger import log
from pyreadline3.unicode_helper import ensure_str, ensure_unicode
try:
import ctypes.util
from ctypes import *
from ctypes.wintypes import *
from _ctypes import call_function
except ImportError:
raise ImportError("You need ctypes to run this code")
def nolog(string):
pass
log = nolog
# some constants we need
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
ENABLE_WINDOW_INPUT = 0x0008
ENABLE_MOUSE_INPUT = 0x0010
ENABLE_PROCESSED_INPUT = 0x0001
WHITE = 0x7
BLACK = 0
MENU_EVENT = 0x0008
KEY_EVENT = 0x0001
MOUSE_MOVED = 0x0001
MOUSE_EVENT = 0x0002
WINDOW_BUFFER_SIZE_EVENT = 0x0004
FOCUS_EVENT = 0x0010
MENU_EVENT = 0x0008
VK_SHIFT = 0x10
VK_CONTROL = 0x11
VK_MENU = 0x12
GENERIC_READ = int(0x80000000)
GENERIC_WRITE = 0x40000000
# Windows structures we'll need later
class COORD(Structure):
_fields_ = [("X", c_short), ("Y", c_short)]
class SMALL_RECT(Structure):
_fields_ = [
("Left", c_short),
("Top", c_short),
("Right", c_short),
("Bottom", c_short),
]
class CONSOLE_SCREEN_BUFFER_INFO(Structure):
_fields_ = [
("dwSize", COORD),
("dwCursorPosition", COORD),
("wAttributes", c_short),
("srWindow", SMALL_RECT),
("dwMaximumWindowSize", COORD),
]
class CHAR_UNION(Union):
_fields_ = [("UnicodeChar", c_wchar), ("AsciiChar", c_char)]
class CHAR_INFO(Structure):
_fields_ = [("Char", CHAR_UNION), ("Attributes", c_short)]
class KEY_EVENT_RECORD(Structure):
_fields_ = [
("bKeyDown", c_byte),
("pad2", c_byte),
("pad1", c_short),
("wRepeatCount", c_short),
("wVirtualKeyCode", c_short),
("wVirtualScanCode", c_short),
("uChar", CHAR_UNION),
("dwControlKeyState", c_int),
]
class MOUSE_EVENT_RECORD(Structure):
_fields_ = [
("dwMousePosition", COORD),
("dwButtonState", c_int),
("dwControlKeyState", c_int),
("dwEventFlags", c_int),
]
class WINDOW_BUFFER_SIZE_RECORD(Structure):
_fields_ = [("dwSize", COORD)]
class MENU_EVENT_RECORD(Structure):
_fields_ = [("dwCommandId", c_uint)]
class FOCUS_EVENT_RECORD(Structure):
_fields_ = [("bSetFocus", c_byte)]
class INPUT_UNION(Union):
_fields_ = [
("KeyEvent", KEY_EVENT_RECORD),
("MouseEvent", MOUSE_EVENT_RECORD),
("WindowBufferSizeEvent", WINDOW_BUFFER_SIZE_RECORD),
("MenuEvent", MENU_EVENT_RECORD),
("FocusEvent", FOCUS_EVENT_RECORD),
]
class INPUT_RECORD(Structure):
_fields_ = [("EventType", c_short), ("Event", INPUT_UNION)]
class CONSOLE_CURSOR_INFO(Structure):
_fields_ = [("dwSize", c_int), ("bVisible", c_byte)]
# I didn't want to have to individually import these so I made a list, they are
# added to the Console class later in this file.
funcs = [
"AllocConsole",
"CreateConsoleScreenBuffer",
"FillConsoleOutputAttribute",
"FillConsoleOutputCharacterW",
"FreeConsole",
"GetConsoleCursorInfo",
"GetConsoleMode",
"GetConsoleScreenBufferInfo",
"GetConsoleTitleW",
"GetProcAddress",
"GetStdHandle",
"PeekConsoleInputW",
"ReadConsoleInputW",
"ScrollConsoleScreenBufferW",
"SetConsoleActiveScreenBuffer",
"SetConsoleCursorInfo",
"SetConsoleCursorPosition",
"SetConsoleMode",
"SetConsoleScreenBufferSize",
"SetConsoleTextAttribute",
"SetConsoleTitleW",
"SetConsoleWindowInfo",
"WriteConsoleW",
"WriteConsoleOutputCharacterW",
"WriteFile",
]
# I don't want events for these keys, they are just a bother for my application
key_modifiers = {
VK_SHIFT: 1,
VK_CONTROL: 1,
VK_MENU: 1, # alt key
0x5B: 1, # windows key
}
def split_block(text, size=1000):
return [text[start : start + size] for start in range(0, len(text), size)]
class Console(object):
"""Console driver for Windows."""
def __init__(self, newbuffer=0):
"""Initialize the Console object.
newbuffer=1 will allocate a new buffer so the old content will be restored
on exit.
"""
# Do I need the following line? It causes a console to be created whenever
# readline is imported into a pythonw application which seems wrong. Things
# seem to work without it...
# self.AllocConsole()
if newbuffer:
self.hout = self.CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE, 0, None, 1, None
)
self.SetConsoleActiveScreenBuffer(self.hout)
else:
self.hout = self.GetStdHandle(STD_OUTPUT_HANDLE)
self.hin = self.GetStdHandle(STD_INPUT_HANDLE)
self.inmode = DWORD(0)
self.GetConsoleMode(self.hin, byref(self.inmode))
self.SetConsoleMode(self.hin, 0xF)
info = CONSOLE_SCREEN_BUFFER_INFO()
self.GetConsoleScreenBufferInfo(self.hout, byref(info))
self.attr = info.wAttributes
self.saveattr = info.wAttributes # remember the initial colors
self.defaultstate = AnsiState()
self.defaultstate.winattr = info.wAttributes
self.ansiwriter = AnsiWriter(self.defaultstate)
background = self.attr & 0xF0
for escape in self.escape_to_color:
if self.escape_to_color[escape] is not None:
self.escape_to_color[escape] |= background
log("initial attr=%x" % self.attr)
self.softspace = 0 # this is for using it as a file-like object
self.serial = 0
self.pythondll = ctypes.pythonapi
self.inputHookPtr = c_void_p.from_address(
addressof(self.pythondll.PyOS_InputHook)
).value
self.pythondll.PyMem_RawMalloc.restype = c_size_t
self.pythondll.PyMem_RawMalloc.argtypes = [c_size_t]
setattr(Console, "PyMem_Malloc", self.pythondll.PyMem_RawMalloc)
def __del__(self):
"""Cleanup the console when finished."""
# I don't think this ever gets called
self.SetConsoleTextAttribute(self.hout, self.saveattr)
self.SetConsoleMode(self.hin, self.inmode)
self.FreeConsole()
def _get_top_bot(self):
info = CONSOLE_SCREEN_BUFFER_INFO()
self.GetConsoleScreenBufferInfo(self.hout, byref(info))
rect = info.srWindow
top = rect.Top
bot = rect.Bottom
return top, bot
def fixcoord(self, x, y):
"""Return a long with x and y packed inside,
also handle negative x and y."""
if x < 0 or y < 0:
info = CONSOLE_SCREEN_BUFFER_INFO()
self.GetConsoleScreenBufferInfo(self.hout, byref(info))
if x < 0:
x = info.srWindow.Right - x
y = info.srWindow.Bottom + y
# this is a hack! ctypes won't pass structures but COORD is
# just like a long, so this works.
return c_int(y << 16 | x)
def pos(self, x=None, y=None):
"""Move or query the window cursor."""
if x is None:
info = CONSOLE_SCREEN_BUFFER_INFO()
self.GetConsoleScreenBufferInfo(self.hout, byref(info))
return (info.dwCursorPosition.X, info.dwCursorPosition.Y)
else:
return self.SetConsoleCursorPosition(self.hout, self.fixcoord(x, y))
def home(self):
"""Move to home."""
self.pos(0, 0)
# Map ANSI color escape sequences into Windows Console Attributes
terminal_escape = re.compile("(\001?\033\\[[0-9;]+m\002?)")
escape_parts = re.compile("\001?\033\\[([0-9;]+)m\002?")
escape_to_color = {
"0;30": 0x0, # black
"0;31": 0x4, # red
"0;32": 0x2, # green
"0;33": 0x4 + 0x2, # brown?
"0;34": 0x1, # blue
"0;35": 0x1 + 0x4, # purple
"0;36": 0x2 + 0x4, # cyan
"0;37": 0x1 + 0x2 + 0x4, # grey
"1;30": 0x1 + 0x2 + 0x4, # dark gray
"1;31": 0x4 + 0x8, # red
"1;32": 0x2 + 0x8, # light green
"1;33": 0x4 + 0x2 + 0x8, # yellow
"1;34": 0x1 + 0x8, # light blue
"1;35": 0x1 + 0x4 + 0x8, # light purple
"1;36": 0x1 + 0x2 + 0x8, # light cyan
"1;37": 0x1 + 0x2 + 0x4 + 0x8, # white
"0": None,
}
# This pattern should match all characters that change the cursor position differently
# than a normal character.
motion_char_re = re.compile("([\n\r\t\010\007])")
def write_scrolling(self, text, attr=None):
"""write text at current cursor position while watching for scrolling.
If the window scrolls because you are at the bottom of the screen
buffer, all positions that you are storing will be shifted by the
scroll amount. For example, I remember the cursor position of the
prompt so that I can redraw the line but if the window scrolls,
the remembered position is off.
This variant of write tries to keep track of the cursor position
so that it will know when the screen buffer is scrolled. It
returns the number of lines that the buffer scrolled.
"""
text = ensure_unicode(text)
x, y = self.pos()
w, h = self.size()
scroll = 0 # the result
# split the string into ordinary characters and funny characters
chunks = self.motion_char_re.split(text)
for chunk in chunks:
n = self.write_color(chunk, attr)
if len(chunk) == 1: # the funny characters will be alone
if chunk[0] == "\n": # newline
x = 0
y += 1
elif chunk[0] == "\r": # carriage return
x = 0
elif chunk[0] == "\t": # tab
x = 8 * (int(x / 8) + 1)
if x > w: # newline
x -= w
y += 1
elif chunk[0] == "\007": # bell
pass
elif chunk[0] == "\010":
x -= 1
if x < 0:
y -= 1 # backed up 1 line
else: # ordinary character
x += 1
if x == w: # wrap
x = 0
y += 1
if y == h: # scroll
scroll += 1
y = h - 1
else: # chunk of ordinary characters
x += n
l = int(x / w) # lines we advanced
x = x % w # new x value
y += l
if y >= h: # scroll
scroll += y - h + 1
y = h - 1
return scroll
def write_color(self, text, attr=None):
text = ensure_unicode(text)
n, res = self.ansiwriter.write_color(text, attr)
junk = DWORD(0)
for attr, chunk in res:
log("console.attr:%s" % (attr))
log("console.chunk:%s" % (chunk))
self.SetConsoleTextAttribute(self.hout, attr.winattr)
for short_chunk in split_block(chunk):
self.WriteConsoleW(
self.hout, short_chunk, len(short_chunk), byref(junk), None
)
return n
def write_plain(self, text, attr=None):
"""write text at current cursor position."""
text = ensure_unicode(text)
log('write("%s", %s)' % (text, attr))
if attr is None:
attr = self.attr
junk = DWORD(0)
self.SetConsoleTextAttribute(self.hout, attr)
for short_chunk in split_block(chunk):
self.WriteConsoleW(
self.hout,
ensure_unicode(short_chunk),
len(short_chunk),
byref(junk),
None,
)
return len(text)
# This function must be used to ensure functioning with EMACS
# Emacs sets the EMACS environment variable
if "EMACS" in os.environ:
def write_color(self, text, attr=None):
text = ensure_str(text)
junk = DWORD(0)
self.WriteFile(self.hout, text, len(text), byref(junk), None)
return len(text)
write_plain = write_color
# make this class look like a file object
def write(self, text):
text = ensure_unicode(text)
log('write("%s")' % text)
return self.write_color(text)
# write = write_scrolling
def isatty(self):
return True
def flush(self):
pass
def page(self, attr=None, fill=" "):
"""Fill the entire screen."""
if attr is None:
attr = self.attr
if len(fill) != 1:
raise ValueError
info = CONSOLE_SCREEN_BUFFER_INFO()
self.GetConsoleScreenBufferInfo(self.hout, byref(info))
if info.dwCursorPosition.X != 0 or info.dwCursorPosition.Y != 0:
self.SetConsoleCursorPosition(self.hout, self.fixcoord(0, 0))
w = info.dwSize.X
n = DWORD(0)
for y in range(info.dwSize.Y):
self.FillConsoleOutputAttribute(
self.hout, attr, w, self.fixcoord(0, y), byref(n)
)
self.FillConsoleOutputCharacterW(
self.hout, ord(fill[0]), w, self.fixcoord(0, y), byref(n)
)
self.attr = attr
def text(self, x, y, text, attr=None):
"""Write text at the given position."""
if attr is None:
attr = self.attr
pos = self.fixcoord(x, y)
n = DWORD(0)
self.WriteConsoleOutputCharacterW(self.hout, text, len(text), pos, byref(n))
self.FillConsoleOutputAttribute(self.hout, attr, n, pos, byref(n))
def clear_to_end_of_window(self):
top, bot = self._get_top_bot()
pos = self.pos()
w, h = self.size()
self.rectangle((pos[0], pos[1], w, pos[1] + 1))
if pos[1] < bot:
self.rectangle((0, pos[1] + 1, w, bot + 1))
def rectangle(self, rect, attr=None, fill=" "):
"""Fill Rectangle."""
x0, y0, x1, y1 = rect
n = DWORD(0)
if attr is None:
attr = self.attr
for y in range(y0, y1):
pos = self.fixcoord(x0, y)
self.FillConsoleOutputAttribute(self.hout, attr, x1 - x0, pos, byref(n))
self.FillConsoleOutputCharacterW(
self.hout, ord(fill[0]), x1 - x0, pos, byref(n)
)
def scroll(self, rect, dx, dy, attr=None, fill=" "):
"""Scroll a rectangle."""
if attr is None:
attr = self.attr
x0, y0, x1, y1 = rect
source = SMALL_RECT(x0, y0, x1 - 1, y1 - 1)
dest = self.fixcoord(x0 + dx, y0 + dy)
style = CHAR_INFO()
style.Char.AsciiChar = ensure_str(fill[0])
style.Attributes = attr
return self.ScrollConsoleScreenBufferW(
self.hout, byref(source), byref(source), dest, byref(style)
)
def scroll_window(self, lines):
"""Scroll the window by the indicated number of lines."""
info = CONSOLE_SCREEN_BUFFER_INFO()
self.GetConsoleScreenBufferInfo(self.hout, byref(info))
rect = info.srWindow
log("sw: rtop=%d rbot=%d" % (rect.Top, rect.Bottom))
top = rect.Top + lines
bot = rect.Bottom + lines
h = bot - top
maxbot = info.dwSize.Y - 1
if top < 0:
top = 0
bot = h
if bot > maxbot:
bot = maxbot
top = bot - h
nrect = SMALL_RECT()
nrect.Top = top
nrect.Bottom = bot
nrect.Left = rect.Left
nrect.Right = rect.Right
log("sn: top=%d bot=%d" % (top, bot))
r = self.SetConsoleWindowInfo(self.hout, True, byref(nrect))
log("r=%d" % r)
def get(self):
"""Get next event from queue."""
inputHookFunc = c_void_p.from_address(self.inputHookPtr).value
Cevent = INPUT_RECORD()
count = DWORD(0)
while True:
if inputHookFunc:
call_function(inputHookFunc, ())
status = self.ReadConsoleInputW(self.hin, byref(Cevent), 1, byref(count))
if status and count.value == 1:
e = event(self, Cevent)
return e
def getkeypress(self):
"""Return next key press event from the queue, ignoring others."""
while True:
e = self.get()
if e.type == "KeyPress" and e.keycode not in key_modifiers:
log("console.getkeypress %s" % e)
if e.keyinfo.keyname == "next":
self.scroll_window(12)
elif e.keyinfo.keyname == "prior":
self.scroll_window(-12)
else:
return e
elif (e.type == "KeyRelease") and (
e.keyinfo == KeyPress("S", False, True, False, "S")
or e.keyinfo == KeyPress("C", False, True, False, "C")
):
log("getKeypress:%s,%s,%s" % (e.keyinfo, e.keycode, e.type))
return e
def getchar(self):
"""Get next character from queue."""
Cevent = INPUT_RECORD()
count = DWORD(0)
while True:
status = self.ReadConsoleInputW(self.hin, byref(Cevent), 1, byref(count))
if (
status
and (count.value == 1)
and (Cevent.EventType == 1)
and Cevent.Event.KeyEvent.bKeyDown
):
sym = keysym(Cevent.Event.KeyEvent.wVirtualKeyCode)
if len(sym) == 0:
sym = Cevent.Event.KeyEvent.uChar.AsciiChar
return sym
def peek(self):
"""Check event queue."""
Cevent = INPUT_RECORD()
count = DWORD(0)
status = self.PeekConsoleInputW(self.hin, byref(Cevent), 1, byref(count))
if status and count == 1:
return event(self, Cevent)
def title(self, txt=None):
"""Set/get title."""
if txt:
self.SetConsoleTitleW(txt)
else:
buffer = create_unicode_buffer(200)
n = self.GetConsoleTitleW(buffer, 200)
if n > 0:
return buffer.value[:n]
def size(self, width=None, height=None):
"""Set/get window size."""
info = CONSOLE_SCREEN_BUFFER_INFO()
status = self.GetConsoleScreenBufferInfo(self.hout, byref(info))
if not status:
return None
if width is not None and height is not None:
wmin = info.srWindow.Right - info.srWindow.Left + 1
hmin = info.srWindow.Bottom - info.srWindow.Top + 1
# print wmin, hmin
width = max(width, wmin)
height = max(height, hmin)
# print width, height
self.SetConsoleScreenBufferSize(self.hout, self.fixcoord(width, height))
else:
return (info.dwSize.X, info.dwSize.Y)
def cursor(self, visible=None, size=None):
"""Set cursor on or off."""
info = CONSOLE_CURSOR_INFO()
if self.GetConsoleCursorInfo(self.hout, byref(info)):
if visible is not None:
info.bVisible = visible
if size is not None:
info.dwSize = size
self.SetConsoleCursorInfo(self.hout, byref(info))
def bell(self):
self.write("\007")
def next_serial(self):
"""Get next event serial number."""
self.serial += 1
return self.serial
# add the functions from the dll to the class
for func in funcs:
setattr(Console, func, getattr(windll.kernel32, func))
_strncpy = ctypes.windll.kernel32.lstrcpynA
_strncpy.restype = c_char_p
_strncpy.argtypes = [c_char_p, c_char_p, c_size_t]
LPVOID = c_void_p
LPCVOID = c_void_p
FARPROC = c_void_p
LPDWORD = POINTER(DWORD)
Console.AllocConsole.restype = BOOL
Console.AllocConsole.argtypes = [] # void
Console.CreateConsoleScreenBuffer.restype = HANDLE
Console.CreateConsoleScreenBuffer.argtypes = [
DWORD,
DWORD,
c_void_p,
DWORD,
LPVOID,
] # DWORD, DWORD, SECURITY_ATTRIBUTES*, DWORD, LPVOID
Console.FillConsoleOutputAttribute.restype = BOOL
Console.FillConsoleOutputAttribute.argtypes = [
HANDLE,
WORD,
DWORD,
c_int,
LPDWORD,
] # HANDLE, WORD, DWORD, COORD, LPDWORD
Console.FillConsoleOutputCharacterW.restype = BOOL
Console.FillConsoleOutputCharacterW.argtypes = [
HANDLE,
c_ushort,
DWORD,
c_int,
LPDWORD,
] # HANDLE, TCHAR, DWORD, COORD, LPDWORD
Console.FreeConsole.restype = BOOL
Console.FreeConsole.argtypes = [] # void
Console.GetConsoleCursorInfo.restype = BOOL
Console.GetConsoleCursorInfo.argtypes = [
HANDLE,
c_void_p,
] # HANDLE, PCONSOLE_CURSOR_INFO
Console.GetConsoleMode.restype = BOOL
Console.GetConsoleMode.argtypes = [HANDLE, LPDWORD] # HANDLE, LPDWORD
Console.GetConsoleScreenBufferInfo.restype = BOOL
Console.GetConsoleScreenBufferInfo.argtypes = [
HANDLE,
c_void_p,
] # HANDLE, PCONSOLE_SCREEN_BUFFER_INFO
Console.GetConsoleTitleW.restype = DWORD
Console.GetConsoleTitleW.argtypes = [c_wchar_p, DWORD] # LPTSTR , DWORD
Console.GetProcAddress.restype = FARPROC
Console.GetProcAddress.argtypes = [HMODULE, c_char_p] # HMODULE , LPCSTR
Console.GetStdHandle.restype = HANDLE
Console.GetStdHandle.argtypes = [DWORD]
Console.PeekConsoleInputW.restype = BOOL
# HANDLE, PINPUT_RECORD, DWORD, LPDWORD
Console.PeekConsoleInputW.argtypes = [HANDLE, c_void_p, DWORD, LPDWORD]
Console.ReadConsoleInputW.restype = BOOL
# HANDLE, PINPUT_RECORD, DWORD, LPDWORD
Console.ReadConsoleInputW.argtypes = [HANDLE, c_void_p, DWORD, LPDWORD]
Console.ScrollConsoleScreenBufferW.restype = BOOL
Console.ScrollConsoleScreenBufferW.argtypes = [
HANDLE,
c_void_p,
c_void_p,
c_int,
c_void_p,
] # HANDLE, SMALL_RECT*, SMALL_RECT*, COORD, LPDWORD
Console.SetConsoleActiveScreenBuffer.restype = BOOL
Console.SetConsoleActiveScreenBuffer.argtypes = [HANDLE] # HANDLE
Console.SetConsoleCursorInfo.restype = BOOL
Console.SetConsoleCursorInfo.argtypes = [
HANDLE,
c_void_p,
] # HANDLE, CONSOLE_CURSOR_INFO*
Console.SetConsoleCursorPosition.restype = BOOL
Console.SetConsoleCursorPosition.argtypes = [HANDLE, c_int] # HANDLE, COORD
Console.SetConsoleMode.restype = BOOL
Console.SetConsoleMode.argtypes = [HANDLE, DWORD] # HANDLE, DWORD
Console.SetConsoleScreenBufferSize.restype = BOOL
Console.SetConsoleScreenBufferSize.argtypes = [HANDLE, c_int] # HANDLE, COORD
Console.SetConsoleTextAttribute.restype = BOOL
Console.SetConsoleTextAttribute.argtypes = [HANDLE, WORD] # HANDLE, WORD
Console.SetConsoleTitleW.restype = BOOL
Console.SetConsoleTitleW.argtypes = [c_wchar_p] # LPCTSTR
Console.SetConsoleWindowInfo.restype = BOOL
Console.SetConsoleWindowInfo.argtypes = [
HANDLE,
BOOL,
c_void_p,
] # HANDLE, BOOL, SMALL_RECT*
Console.WriteConsoleW.restype = BOOL
# HANDLE, VOID*, DWORD, LPDWORD, LPVOID
Console.WriteConsoleW.argtypes = [HANDLE, c_void_p, DWORD, LPDWORD, LPVOID]
Console.WriteConsoleOutputCharacterW.restype = BOOL
Console.WriteConsoleOutputCharacterW.argtypes = [
HANDLE,
c_wchar_p,
DWORD,
c_int,
LPDWORD,
] # HANDLE, LPCTSTR, DWORD, COORD, LPDWORD
Console.WriteFile.restype = BOOL
# HANDLE, LPCVOID , DWORD, LPDWORD , LPOVERLAPPED
Console.WriteFile.argtypes = [HANDLE, LPCVOID, DWORD, LPDWORD, c_void_p]
VkKeyScan = windll.user32.VkKeyScanA
class event(Event):
"""Represent events from the console."""
def __init__(self, console, input):
"""Initialize an event from the Windows input structure."""
self.type = "??"
self.serial = console.next_serial()
self.width = 0
self.height = 0
self.x = 0
self.y = 0
self.char = ""
self.keycode = 0
self.keysym = "??"
# a tuple with (control, meta, shift, keycode) for dispatch
self.keyinfo = None
self.width = None
if input.EventType == KEY_EVENT:
if input.Event.KeyEvent.bKeyDown:
self.type = "KeyPress"
else:
self.type = "KeyRelease"
self.char = input.Event.KeyEvent.uChar.UnicodeChar
self.keycode = input.Event.KeyEvent.wVirtualKeyCode
self.state = input.Event.KeyEvent.dwControlKeyState
self.keyinfo = make_KeyPress(self.char, self.state, self.keycode)
elif input.EventType == MOUSE_EVENT:
if input.Event.MouseEvent.dwEventFlags & MOUSE_MOVED:
self.type = "Motion"
else:
self.type = "Button"
self.x = input.Event.MouseEvent.dwMousePosition.X
self.y = input.Event.MouseEvent.dwMousePosition.Y
self.state = input.Event.MouseEvent.dwButtonState
elif input.EventType == WINDOW_BUFFER_SIZE_EVENT:
self.type = "Configure"
self.width = input.Event.WindowBufferSizeEvent.dwSize.X
self.height = input.Event.WindowBufferSizeEvent.dwSize.Y
elif input.EventType == FOCUS_EVENT:
if input.Event.FocusEvent.bSetFocus:
self.type = "FocusIn"
else:
self.type = "FocusOut"
elif input.EventType == MENU_EVENT:
self.type = "Menu"
self.state = input.Event.MenuEvent.dwCommandId
def getconsole(buffer=1):
"""Get a console handle.
If buffer is non-zero, a new console buffer is allocated and
installed. Otherwise, this returns a handle to the current
console buffer"""
c = Console(buffer)
return c
# The following code uses ctypes to allow a Python callable to
# substitute for GNU readline within the Python interpreter. Calling
# raw_input or other functions that do input, inside your callable
# might be a bad idea, then again, it might work.
# The Python callable can raise EOFError or KeyboardInterrupt and
# these will be translated into the appropriate outputs from readline
# so that they will then be translated back!
# If the Python callable raises any other exception, a traceback will
# be printed and readline will appear to return an empty line.
# I use ctypes to create a C-callable from a Python wrapper that
# handles the exceptions and gets the result into the right form.
# the type for our C-callable wrapper
HOOKFUNC23 = CFUNCTYPE(c_char_p, c_void_p, c_void_p, c_char_p)
readline_hook = None # the python hook goes here
readline_ref = None # reference to the c-callable to keep it alive
def hook_wrapper_23(stdin, stdout, prompt):
"""Wrap a Python readline so it behaves like GNU readline."""
try:
# call the Python hook
res = ensure_str(readline_hook(prompt))
# make sure it returned the right sort of thing
if res and not isinstance(res, bytes):
raise TypeError("readline must return a string.")
except KeyboardInterrupt:
# GNU readline returns 0 on keyboard interrupt
return 0
except EOFError:
# It returns an empty string on EOF
res = ensure_str("")
except BaseException:
print("Readline internal error", file=sys.stderr)
traceback.print_exc()
res = ensure_str("\n")
# we have to make a copy because the caller expects to free the result
n = len(res)
p = Console.PyMem_Malloc(n + 1)
_strncpy(cast(p, c_char_p), res, n + 1)
return p
def install_readline(hook):
"""Set up things for the interpreter to call
our function like GNU readline."""
global readline_hook, readline_ref
# save the hook so the wrapper can call it
readline_hook = hook
# get the address of PyOS_ReadlineFunctionPointer so we can update it
PyOS_RFP = c_void_p.from_address(
Console.GetProcAddress(
sys.dllhandle, "PyOS_ReadlineFunctionPointer".encode("ascii")
)
)
# save a reference to the generated C-callable so it doesn't go away
readline_ref = HOOKFUNC23(hook_wrapper_23)
# get the address of the function
func_start = c_void_p.from_address(addressof(readline_ref)).value
# write the function address into PyOS_ReadlineFunctionPointer
PyOS_RFP.value = func_start
if __name__ == "__main__":
import sys
import time
def p(char):
return chr(VkKeyScan(ord(char)) & 0xFF)
c = Console(0)
sys.stdout = c
sys.stderr = c
c.page()
print(p("d"), p("D"))
c.pos(5, 10)
c.write("hi there")
print("some printed output")
for i in range(10):
q = c.getkeypress()
print(q)
del c

View File

@ -0,0 +1,15 @@
FOREGROUND_BLUE = 0x0001
FOREGROUND_GREEN = 0x0002
FOREGROUND_RED = 0x0004
FOREGROUND_INTENSITY = 0x0008
BACKGROUND_BLUE = 0x0010
BACKGROUND_GREEN = 0x0020
BACKGROUND_RED = 0x0040
BACKGROUND_INTENSITY = 0x0080
COMMON_LVB_LEADING_BYTE = 0x0100
COMMON_LVB_TRAILING_BYTE = 0x0200
COMMON_LVB_GRID_HORIZONTAL = 0x0400
COMMON_LVB_GRID_LVERTICAL = 0x0800
COMMON_LVB_GRID_RVERTICAL = 0x1000
COMMON_LVB_REVERSE_VIDEO = 0x2000
COMMON_LVB_UNDERSCORE = 0x4000

View File

@ -0,0 +1,50 @@
class baseconsole(object):
def __init__(self):
pass
def bell(self):
raise NotImplementedError
def pos(self, x=None, y=None):
"""Move or query the window cursor."""
raise NotImplementedError
def size(self):
raise NotImplementedError
def rectangle(self, rect, attr=None, fill=" "):
"""Fill Rectangle."""
raise NotImplementedError
def write_scrolling(self, text, attr=None):
"""write text at current cursor position while watching for scrolling.
If the window scrolls because you are at the bottom of the screen
buffer, all positions that you are storing will be shifted by the
scroll amount. For example, I remember the cursor position of the
prompt so that I can redraw the line but if the window scrolls,
the remembered position is off.
This variant of write tries to keep track of the cursor position
so that it will know when the screen buffer is scrolled. It
returns the number of lines that the buffer scrolled.
"""
raise NotImplementedError
def getkeypress(self):
"""Return next key press event from the queue, ignoring others."""
raise NotImplementedError
def write(self, text):
raise NotImplementedError
def page(self, attr=None, fill=" "):
"""Fill the entire screen."""
raise NotImplementedError
def isatty(self):
return True
def flush(self):
pass

View File

@ -0,0 +1,37 @@
class Event(object):
"""Represent events from the console."""
def __init__(self, console, input):
pass
def __repr__(self):
"""Display an event for debugging."""
if self.type in ["KeyPress", "KeyRelease"]:
chr = self.char
if ord(chr) < ord("A"):
chr = "?"
s = "%s char='%s'%d keysym='%s' keycode=%d:%x state=%x keyinfo=%s" % (
self.type,
chr,
ord(self.char),
self.keysym,
self.keycode,
self.keycode,
self.state,
self.keyinfo,
)
elif self.type in ["Motion", "Button"]:
s = "%s x=%d y=%d state=%x" % (self.type, self.x, self.y, self.state)
elif self.type == "Configure":
s = "%s w=%d h=%d" % (self.type, self.width, self.height)
elif self.type in ["FocusIn", "FocusOut"]:
s = self.type
elif self.type == "Menu":
s = "%s state=%x" % (self.type, self.state)
else:
s = "unknown event type"
return s
# def __str__(self):
# return "('%s',%s,%s,%s)"%(self.char,self.key,self.state,self.keyinfo)

View File

@ -0,0 +1,469 @@
# -*- coding: utf-8 -*-
# *****************************************************************************
# Copyright (C) 2003-2006 Gary Bishop.
# Copyright (C) 2006-2020 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
# Copyright (C) 2020 Bassem Girgis. <brgirgis@gmail.com>
#
# Distributed under the terms of the BSD License. The full license is in
# the file COPYING, distributed as part of this software.
# *****************************************************************************
import os
import re
import IronPythonConsole
import System
from pyreadline3.console.ansi import AnsiState
from pyreadline3.keysyms import (
make_keyinfo,
make_KeyPress,
make_KeyPress_from_keydescr,
make_keysym,
)
from pyreadline3.logger import log
from .event import Event
"""Cursor control and color for the .NET console.
"""
#
# Ironpython requires a patch to work do:
#
# In file PythonCommandLine.cs patch line:
# class PythonCommandLine
# {
# to:
# public class PythonCommandLine
# {
#
#
#
# primitive debug printing that won't interfere with the screen
import sys
import clr
clr.AddReferenceToFileAndPath(sys.executable)
color = System.ConsoleColor
ansicolor = {
"0;30": color.Black,
"0;31": color.DarkRed,
"0;32": color.DarkGreen,
"0;33": color.DarkYellow,
"0;34": color.DarkBlue,
"0;35": color.DarkMagenta,
"0;36": color.DarkCyan,
"0;37": color.DarkGray,
"1;30": color.Gray,
"1;31": color.Red,
"1;32": color.Green,
"1;33": color.Yellow,
"1;34": color.Blue,
"1;35": color.Magenta,
"1;36": color.Cyan,
"1;37": color.White,
}
winattr = {
"black": 0,
"darkgray": 0 + 8,
"darkred": 4,
"red": 4 + 8,
"darkgreen": 2,
"green": 2 + 8,
"darkyellow": 6,
"yellow": 6 + 8,
"darkblue": 1,
"blue": 1 + 8,
"darkmagenta": 5,
"magenta": 5 + 8,
"darkcyan": 3,
"cyan": 3 + 8,
"gray": 7,
"white": 7 + 8,
}
class Console(object):
"""Console driver for Windows."""
def __init__(self, newbuffer=0):
"""Initialize the Console object.
newbuffer=1 will allocate a new buffer so the old content will be restored
on exit.
"""
self.serial = 0
self.attr = System.Console.ForegroundColor
self.saveattr = winattr[str(System.Console.ForegroundColor).lower()]
self.savebg = System.Console.BackgroundColor
log("initial attr=%s" % self.attr)
def _get(self):
top = System.Console.WindowTop
log("WindowTop:%s" % top)
return top
def _set(self, value):
top = System.Console.WindowTop
log("Set WindowTop:old:%s,new:%s" % (top, value))
WindowTop = property(_get, _set)
del _get, _set
def __del__(self):
"""Cleanup the console when finished."""
# I don't think this ever gets called
pass
def pos(self, x=None, y=None):
"""Move or query the window cursor."""
if x is not None:
System.Console.CursorLeft = x
else:
x = System.Console.CursorLeft
if y is not None:
System.Console.CursorTop = y
else:
y = System.Console.CursorTop
return x, y
def home(self):
"""Move to home."""
self.pos(0, 0)
# Map ANSI color escape sequences into Windows Console Attributes
terminal_escape = re.compile("(\001?\033\\[[0-9;]*m\002?)")
escape_parts = re.compile("\001?\033\\[([0-9;]*)m\002?")
# This pattern should match all characters that change the cursor position differently
# than a normal character.
motion_char_re = re.compile("([\n\r\t\010\007])")
def write_scrolling(self, text, attr=None):
"""write text at current cursor position while watching for scrolling.
If the window scrolls because you are at the bottom of the screen
buffer, all positions that you are storing will be shifted by the
scroll amount. For example, I remember the cursor position of the
prompt so that I can redraw the line but if the window scrolls,
the remembered position is off.
This variant of write tries to keep track of the cursor position
so that it will know when the screen buffer is scrolled. It
returns the number of lines that the buffer scrolled.
"""
x, y = self.pos()
w, h = self.size()
scroll = 0 # the result
# split the string into ordinary characters and funny characters
chunks = self.motion_char_re.split(text)
for chunk in chunks:
n = self.write_color(chunk, attr)
if len(chunk) == 1: # the funny characters will be alone
if chunk[0] == "\n": # newline
x = 0
y += 1
elif chunk[0] == "\r": # carriage return
x = 0
elif chunk[0] == "\t": # tab
x = 8 * (int(x / 8) + 1)
if x > w: # newline
x -= w
y += 1
elif chunk[0] == "\007": # bell
pass
elif chunk[0] == "\010":
x -= 1
if x < 0:
y -= 1 # backed up 1 line
else: # ordinary character
x += 1
if x == w: # wrap
x = 0
y += 1
if y == h: # scroll
scroll += 1
y = h - 1
else: # chunk of ordinary characters
x += n
l = int(x / w) # lines we advanced
x = x % w # new x value
y += l
if y >= h: # scroll
scroll += y - h + 1
y = h - 1
return scroll
trtable = {
0: color.Black,
4: color.DarkRed,
2: color.DarkGreen,
6: color.DarkYellow,
1: color.DarkBlue,
5: color.DarkMagenta,
3: color.DarkCyan,
7: color.Gray,
8: color.DarkGray,
4 + 8: color.Red,
2 + 8: color.Green,
6 + 8: color.Yellow,
1 + 8: color.Blue,
5 + 8: color.Magenta,
3 + 8: color.Cyan,
7 + 8: color.White,
}
def write_color(self, text, attr=None):
"""write text at current cursor position and interpret color escapes.
return the number of characters written.
"""
log('write_color("%s", %s)' % (text, attr))
chunks = self.terminal_escape.split(text)
log("chunks=%s" % repr(chunks))
bg = self.savebg
n = 0 # count the characters we actually write, omitting the escapes
if attr is None: # use attribute from initial console
attr = self.attr
try:
fg = self.trtable[(0x000F & attr)]
bg = self.trtable[(0x00F0 & attr) >> 4]
except TypeError:
fg = attr
for chunk in chunks:
m = self.escape_parts.match(chunk)
if m:
log(m.group(1))
attr = ansicolor.get(m.group(1), self.attr)
n += len(chunk)
System.Console.ForegroundColor = fg
System.Console.BackgroundColor = bg
System.Console.Write(chunk)
return n
def write_plain(self, text, attr=None):
"""write text at current cursor position."""
log('write("%s", %s)' % (text, attr))
if attr is None:
attr = self.attr
n = c_int(0)
self.SetConsoleTextAttribute(self.hout, attr)
self.WriteConsoleA(self.hout, text, len(text), byref(n), None)
return len(text)
if "EMACS" in os.environ:
def write_color(self, text, attr=None):
junk = c_int(0)
self.WriteFile(self.hout, text, len(text), byref(junk), None)
return len(text)
write_plain = write_color
# make this class look like a file object
def write(self, text):
log('write("%s")' % text)
return self.write_color(text)
# write = write_scrolling
def isatty(self):
return True
def flush(self):
pass
def page(self, attr=None, fill=" "):
"""Fill the entire screen."""
System.Console.Clear()
def text(self, x, y, text, attr=None):
"""Write text at the given position."""
self.pos(x, y)
self.write_color(text, attr)
def clear_to_end_of_window(self):
oldtop = self.WindowTop
lastline = self.WindowTop + System.Console.WindowHeight
pos = self.pos()
w, h = self.size()
length = w - pos[0] + min((lastline - pos[1] - 1), 5) * w - 1
self.write_color(length * " ")
self.pos(*pos)
self.WindowTop = oldtop
def rectangle(self, rect, attr=None, fill=" "):
"""Fill Rectangle."""
oldtop = self.WindowTop
oldpos = self.pos()
# raise NotImplementedError
x0, y0, x1, y1 = rect
if attr is None:
attr = self.attr
if fill:
rowfill = fill[:1] * abs(x1 - x0)
else:
rowfill = " " * abs(x1 - x0)
for y in range(y0, y1):
System.Console.SetCursorPosition(x0, y)
self.write_color(rowfill, attr)
self.pos(*oldpos)
def scroll(self, rect, dx, dy, attr=None, fill=" "):
"""Scroll a rectangle."""
raise NotImplementedError
def scroll_window(self, lines):
"""Scroll the window by the indicated number of lines."""
top = self.WindowTop + lines
if top < 0:
top = 0
if top + System.Console.WindowHeight > System.Console.BufferHeight:
top = System.Console.BufferHeight
self.WindowTop = top
def getkeypress(self):
"""Return next key press event from the queue, ignoring others."""
ck = System.ConsoleKey
while True:
e = System.Console.ReadKey(True)
if e.Key == System.ConsoleKey.PageDown: # PageDown
self.scroll_window(12)
elif e.Key == System.ConsoleKey.PageUp: # PageUp
self.scroll_window(-12)
elif str(e.KeyChar) == "\000": # Drop deadkeys
log("Deadkey: %s" % e)
return event(self, e)
else:
return event(self, e)
def title(self, txt=None):
"""Set/get title."""
if txt:
System.Console.Title = txt
else:
return System.Console.Title
def size(self, width=None, height=None):
"""Set/get window size."""
sc = System.Console
if width is not None and height is not None:
sc.BufferWidth, sc.BufferHeight = width, height
else:
return sc.BufferWidth, sc.BufferHeight
if width is not None and height is not None:
sc.WindowWidth, sc.WindowHeight = width, height
else:
return sc.WindowWidth - 1, sc.WindowHeight - 1
def cursor(self, visible=True, size=None):
"""Set cursor on or off."""
System.Console.CursorVisible = visible
def bell(self):
System.Console.Beep()
def next_serial(self):
"""Get next event serial number."""
self.serial += 1
return self.serial
class event(Event):
"""Represent events from the console."""
def __init__(self, console, input):
"""Initialize an event from the Windows input structure."""
self.type = "??"
self.serial = console.next_serial()
self.width = 0
self.height = 0
self.x = 0
self.y = 0
self.char = str(input.KeyChar)
self.keycode = input.Key
self.state = input.Modifiers
log("%s,%s,%s" % (input.Modifiers, input.Key, input.KeyChar))
self.type = "KeyRelease"
self.keysym = make_keysym(self.keycode)
self.keyinfo = make_KeyPress(self.char, self.state, self.keycode)
def make_event_from_keydescr(keydescr):
def input():
return 1
input.KeyChar = "a"
input.Key = System.ConsoleKey.A
input.Modifiers = System.ConsoleModifiers.Shift
input.next_serial = input
e = event(input, input)
del input.next_serial
keyinfo = make_KeyPress_from_keydescr(keydescr)
e.keyinfo = keyinfo
return e
CTRL_C_EVENT = make_event_from_keydescr("Control-c")
def install_readline(hook):
def hook_wrap():
try:
res = hook()
except KeyboardInterrupt as x: # this exception does not seem to be caught
res = ""
except EOFError:
return None
if res[-1:] == "\n":
return res[:-1]
else:
return res
class IronPythonWrapper(IronPythonConsole.IConsole):
def ReadLine(self, autoIndentSize):
return hook_wrap()
def Write(self, text, style):
System.Console.Write(text)
def WriteLine(self, text, style):
System.Console.WriteLine(text)
IronPythonConsole.PythonCommandLine.MyConsole = IronPythonWrapper()
if __name__ == "__main__":
import sys
import time
c = Console(0)
sys.stdout = c
sys.stderr = c
c.page()
c.pos(5, 10)
c.write("hi there")
c.title("Testing console")
# c.bell()
print()
print("size", c.size())
print(" some printed output")
for i in range(10):
e = c.getkeypress()
print(e.Key, chr(e.KeyChar), ord(e.KeyChar), e.Modifiers)
del c
System.Console.Clear()