Better formatting

Variale columns size in curses
This commit is contained in:
Johann Dreo 2016-12-13 22:40:13 +01:00
commit ca68388f7e
2 changed files with 42 additions and 53 deletions

View file

@ -1 +0,0 @@
ereshkigal.py

View file

@ -60,7 +60,7 @@ class Tunnel:
# list of tunnels linked to this process # list of tunnels linked to this process
rep = "" rep = ""
for c in self.connections: for c in self.connections:
rep += "\n\t%s" % c rep += "\n\t%s" % c
return rep return rep
def __repr__(self): def __repr__(self):
@ -113,20 +113,20 @@ class Connection:
def __repr__(self): def __repr__(self):
# do not logging.debug all the informations by default # do not logging.debug all the informations by default
if self.foreign_address and self.out_port: if self.foreign_address and self.out_port:
return "%s:%i -> %s:%i\t%s\t%s" % ( return "%s\t%s\t%s:%i%s:%i" % (
self.family_rep[self.family],
self.status,
self.local_address, self.local_address,
self.in_port, self.in_port,
self.foreign_address, self.foreign_address,
self.out_port, self.out_port,
self.family_rep[self.family],
self.status,
) )
else: else:
return "%s:%i\t%s\t%s" % ( return "%s\t%s\t%s:%i" % (
self.local_address,
self.in_port,
self.family_rep[self.family], self.family_rep[self.family],
self.status, self.status,
self.local_address,
self.in_port,
) )
@ -144,7 +144,7 @@ class TunnelsParser:
self.re_forwarding = re.compile(r"-L(\d+):(.+):(\d+)") self.re_forwarding = re.compile(r"-L(\d+):(.+):(\d+)")
self.header = 'TYPE\tPID\tIN_PORT\tVIA_HOST\tTARGET_HOST\tOUT_PORT' self.header = 'TYPE\tSSH_PID\tIN_PORT\tVIA_HOST\tTARGET_HOST\tOUT_PORT'
def get_tunnel(self, pos): def get_tunnel(self, pos):
@ -227,31 +227,6 @@ class TunnelsParser:
################################################################################################# #################################################################################################
# INTERFACES # INTERFACES
################################################################################################# #################################################################################################
@ -286,9 +261,11 @@ class CursesMonitor:
# colors # colors
# FIXME different colors for different types of tunnels (auto or raw) # FIXME different colors for different types of tunnels (auto or raw)
self.colors_tunnel = {'kind_auto':4, 'kind_raw':5, 'ssh_pid':0, 'in_port':3, 'via_host':2, 'target_host':2, 'out_port':3, 'tunnels_nb':4, 'tunnels_nb_none':1} self.colors_tunnel = {'kind_auto':4, 'kind_raw':5, 'ssh_pid':0, 'in_port':3, 'via_host':2, 'target_host':2, 'out_port':3, 'tunnels_nb':4, 'tunnels_nb_none':1}
self.colors_highlight = {'kind_auto':9, 'kind_raw':5, 'ssh_pid':9, 'in_port':9, 'via_host':9, 'target_host':9, 'out_port':9, 'tunnels_nb':9, 'tunnels_nb_none':9} self.colors_highlight = {'kind_auto':9, 'kind_raw':9, 'ssh_pid':9, 'in_port':9, 'via_host':9, 'target_host':9, 'out_port':9, 'tunnels_nb':9, 'tunnels_nb_none':9}
self.colors_connection = {'ssh_pid':0, 'autossh_pid':0, 'status':4, 'status_out':1, 'local_address':2, 'in_port':3, 'foreign_address':2, 'out_port':3} self.colors_connection = {'ssh_pid':0, 'autossh_pid':0, 'status':4, 'status_out':1, 'local_address':2, 'in_port':3, 'foreign_address':2, 'out_port':3}
self.header = ("TYPE","SSHPID","INPORT","VIA","TARGET","OUTPORT")
def do_Q(self): def do_Q(self):
"""Quit""" """Quit"""
logging.debug("Waited: %s" % self.log_ticks) logging.debug("Waited: %s" % self.log_ticks)
@ -449,6 +426,19 @@ class CursesMonitor:
# end of the loop # end of the loop
def format(self):
reps = [self.tp.tunnels[t].repr_tunnel() for t in self.tp.tunnels]
tuns = [t.split() for t in reps]
tuns.append(self.header)
logging.debug(tuns)
cols = zip(*tuns)
widths = [max(len(s) for s in col) for col in cols]
logging.debug(widths)
fmt = ['{{: <{}}}'.format(w) for w in widths]
logging.debug(fmt)
return fmt
def display(self): def display(self):
"""Generate the interface screen""" """Generate the interface screen"""
@ -482,9 +472,10 @@ class CursesMonitor:
self.cur_pid = -1 self.cur_pid = -1
# header line # header line
header_msg = "TYPE\tINPORT\tVIA \tTARGET \tOUTPORT" # header_msg = "TYPE\tINPORT\tVIA \tTARGET \tOUTPORT"
# if os.geteuid() == 0: # if os.geteuid() == 0:
header_msg += "\tCONNECTIONS" header_msg = " ".join(self.format()).format(*self.header)
header_msg += " CONNECTIONS"
self.scr.addstr( header_msg, curses.color_pair(color) ) self.scr.addstr( header_msg, curses.color_pair(color) )
self.scr.clrtoeol() self.scr.clrtoeol()
@ -544,17 +535,18 @@ class CursesMonitor:
colors = self.colors_highlight colors = self.colors_highlight
if type(self.tp.get_tunnel(line)) == AutoTunnel: if type(self.tp.get_tunnel(line)) == AutoTunnel:
self.scr.addstr( 'auto', curses.color_pair(colors['kind_auto']) ) self.scr.addstr( self.format()[0].format('auto'), curses.color_pair(colors['kind_auto']) )
self.scr.addstr( '\t', curses.color_pair(colors['kind_auto']) ) self.scr.addstr( ' ', curses.color_pair(colors['kind_auto']) )
else: else:
self.scr.addstr( 'ssh', curses.color_pair(colors['kind_raw']) ) self.scr.addstr( self.format()[0].format('ssh'), curses.color_pair(colors['kind_raw']) )
self.scr.addstr( '\t', curses.color_pair(colors['kind_raw']) ) self.scr.addstr( ' ', curses.color_pair(colors['kind_raw']) )
# self.add_tunnel_info('ssh_pid', line) # self.add_tunnel_info('ssh_pid', line)
self.add_tunnel_info('in_port', line) self.add_tunnel_info('ssh_pid', line, 1)
self.add_tunnel_info('via_host', line) self.add_tunnel_info('in_port', line, 2)
self.add_tunnel_info('target_host', line) self.add_tunnel_info('via_host', line, 3)
self.add_tunnel_info('out_port', line) self.add_tunnel_info('target_host', line, 4)
self.add_tunnel_info('out_port', line, 5)
nb = len(self.tp.get_tunnel(line).connections ) nb = len(self.tp.get_tunnel(line).connections )
if nb > 0: if nb > 0:
@ -574,7 +566,8 @@ class CursesMonitor:
self.scr.clrtoeol() self.scr.clrtoeol()
def add_tunnel_info( self, key, line ):
def add_tunnel_info( self, key, line, col ):
"""Add an information of an autossh process, in the configured color""" """Add an information of an autossh process, in the configured color"""
colors = self.colors_tunnel colors = self.colors_tunnel
@ -585,12 +578,10 @@ class CursesMonitor:
txt = eval("str(self.tp.get_tunnel(line).%s)" % key) txt = eval("str(self.tp.get_tunnel(line).%s)" % key)
if key == 'target_host' or key == 'via_host': if key == 'target_host' or key == 'via_host':
# limit the size of the line to 20 txt = eval("str(self.tp.get_tunnel(line).%s)" % key)
# FIXME avoid hard-coded constants
txt = eval("str(self.tp.get_tunnel(line).%s).ljust(20)[:20]" % key)
self.scr.addstr( txt, curses.color_pair(colors[key]) ) self.scr.addstr(self.format()[col].format(txt), curses.color_pair(colors[key]) )
self.scr.addstr( '\t', curses.color_pair(colors[key]) ) self.scr.addstr( ' ', curses.color_pair(colors[key]) )
@ -738,8 +729,7 @@ if __name__ == "__main__":
# do not call update() bu only get autossh processes # do not call update() bu only get autossh processes
print(tp.header) print(tp.header)
for t in tp.tunnels: for t in tp.tunnels:
if type(tp.tunnels[t]) == AutoTunnel: print(tp.tunnels[t].repr_tunnel())
print(tp.tunnels[t].repr_tunnel())
else: else: