detect columns and lines

This commit is contained in:
chrys 2018-03-24 18:48:04 +01:00
parent 78dd5c6b65
commit ded473542f

View File

@ -19,8 +19,8 @@ class Terminal:
self.stream.attach(self.screen) self.stream.attach(self.screen)
def feed(self, data): def feed(self, data):
self.stream.feed(data) self.stream.feed(data)
def resize(self, rows, columns): def resize(self, lines, columns):
self.screen.resize(rows, columns) self.screen.resize(lines, columns)
self.setCursor() self.setCursor()
def setCursor(self, x = -1, y = -1): def setCursor(self, x = -1, y = -1):
xPos = x xPos = x
@ -49,7 +49,7 @@ class driver(screenDriver):
self.bgColorNames = {0: _('black'), 1: _('blue'), 2: _('green'), 3: _('cyan'), 4: _('red'), 5: _('Magenta'), 6: _('brown/yellow'), 7: _('white')} self.bgColorNames = {0: _('black'), 1: _('blue'), 2: _('green'), 3: _('cyan'), 4: _('red'), 5: _('Magenta'), 6: _('brown/yellow'), 7: _('white')}
self.fgColorNames = {0: _('Black'), 1: _('Blue'), 2: _('Green'), 3: _('Cyan'), 4: _('Red'), 5: _('Magenta'), 6: _('brown/yellow'), 7: _('Light gray'), 8: _('Dark gray'), 9: _('Light blue'), 10: ('Light green'), 11: _('Light cyan'), 12: _('Light red'), 13: _('Light magenta'), 14: _('Light yellow'), 15: _('White')} self.fgColorNames = {0: _('Black'), 1: _('Blue'), 2: _('Green'), 3: _('Cyan'), 4: _('Red'), 5: _('Magenta'), 6: _('brown/yellow'), 7: _('Light gray'), 8: _('Dark gray'), 9: _('Light blue'), 10: ('Light green'), 11: _('Light cyan'), 12: _('Light red'), 13: _('Light magenta'), 14: _('Light yellow'), 15: _('White')}
self.signalPipe = os.pipe() self.signalPipe = os.pipe()
signal.signal(signal.SIGWINCH, self.handle_sigwinch) signal.signal(signal.SIGWINCH, self.handleSigwinch)
def initialize(self, environment): def initialize(self, environment):
self.env = environment self.env = environment
self.env['runtime']['processManager'].addCustomEventThread(self.terminalEmulation) self.env['runtime']['processManager'].addCustomEventThread(self.terminalEmulation)
@ -64,20 +64,20 @@ class driver(screenDriver):
self.env['screen']['autoIgnoreScreens'] = [] self.env['screen']['autoIgnoreScreens'] = []
self.env['general']['prevUser'] = 'chrys' self.env['general']['prevUser'] = 'chrys'
self.env['general']['currUser'] = 'chrys' self.env['general']['currUser'] = 'chrys'
def read_all(self,fd): def readAll(self,fd):
bytes = os.read(fd, 65536) bytes = os.read(fd, 65536)
if bytes == b'': if bytes == b'':
raise EOFError raise EOFError
while self.has_more(fd): while self.hasMore(fd):
data = os.read(fd, 65536) data = os.read(fd, 65536)
if data == b'': if data == b'':
raise EOFError raise EOFError
bytes += data bytes += data
return bytes return bytes
def has_more(self,fd): def hasMore(self,fd):
r, w, e = select.select([fd], [], [], 0.02) r, w, e = select.select([fd], [], [], 0.02)
return (fd in r) return (fd in r)
def open_terminal(self,command="bash", columns=138, lines=37): def openTerminal(self, columns, lines, command="bash"):
p_pid, master_fd = pty.fork() p_pid, master_fd = pty.fork()
if p_pid == 0: # Child. if p_pid == 0: # Child.
argv = shlex.split(command) argv = shlex.split(command)
@ -87,17 +87,17 @@ class driver(screenDriver):
# File-like object for I/O with the child process aka command. # File-like object for I/O with the child process aka command.
p_out = os.fdopen(master_fd, "w+b", 0) p_out = os.fdopen(master_fd, "w+b", 0)
return Terminal(columns, lines, p_out), p_pid, p_out return Terminal(columns, lines, p_out), p_pid, p_out
def resize_terminal(self,fd): def resizeTerminal(self,fd):
s = struct.pack('HHHH', 0, 0, 0, 0) s = struct.pack('HHHH', 0, 0, 0, 0)
s = fcntl.ioctl(0, termios.TIOCGWINSZ, s) s = fcntl.ioctl(0, termios.TIOCGWINSZ, s)
fcntl.ioctl(fd, termios.TIOCSWINSZ, s) fcntl.ioctl(fd, termios.TIOCSWINSZ, s)
rows, columns, _, _ = struct.unpack('hhhh', s) lines, columns, _, _ = struct.unpack('hhhh', s)
return rows, columns return lines, columns
def get_terminal_size(self, fd): def getTerminalSize(self, fd):
s = struct.pack('HHHH', 0, 0, 0, 0) s = struct.pack('HHHH', 0, 0, 0, 0)
rows, columns, _, _ = struct.unpack('HHHH', fcntl.ioctl(fd, termios.TIOCGWINSZ, s)) lines, columns, _, _ = struct.unpack('HHHH', fcntl.ioctl(fd, termios.TIOCGWINSZ, s))
return rows, columns return lines, columns
def handle_sigwinch(self, *args): def handleSigwinch(self, *args):
os.write(self.signalPipe[1], b'w') os.write(self.signalPipe[1], b'w')
def terminalEmulation(self,active , eventQueue): def terminalEmulation(self,active , eventQueue):
debug = False debug = False
@ -105,7 +105,8 @@ class driver(screenDriver):
try: try:
old_attr = termios.tcgetattr(sys.stdin) old_attr = termios.tcgetattr(sys.stdin)
tty.setraw(0) tty.setraw(0)
terminal, p_pid, p_out = self.open_terminal() lines, columns = self.getTerminalSize(0)
terminal, p_pid, p_out = self.openTerminal(columns, lines)
std_out = os.fdopen(sys.stdout.fileno(), "w+b", 0) std_out = os.fdopen(sys.stdout.fileno(), "w+b", 0)
while active: while active:
@ -116,14 +117,14 @@ class driver(screenDriver):
# signals # signals
if self.signalPipe[0] in r: if self.signalPipe[0] in r:
os.read(self.signalPipe[0], 1) os.read(self.signalPipe[0], 1)
rows, columns = self.resize_terminal(p_out) lines, columns = self.resizeTerminal(p_out)
terminal.resize(rows, columns) terminal.resize(lines, columns)
# output # output
if p_out in r: if p_out in r:
if debug: if debug:
print('pre p_out') print('pre p_out')
try: try:
msgBytes = self.read_all(p_out.fileno()) msgBytes = self.readAll(p_out.fileno())
except (EOFError, OSError): except (EOFError, OSError):
running = False running = False
break break
@ -143,7 +144,7 @@ class driver(screenDriver):
if debug: if debug:
print('pre stdin') print('pre stdin')
try: try:
msgBytes = self.read_all(sys.stdin.fileno()) msgBytes = self.readAll(sys.stdin.fileno())
except (EOFError, OSError): except (EOFError, OSError):
running = False running = False
break break