# ==============================================================================
"""NUMLINES : print a list of text files with line numbering"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "1.0"
__date__    = "2022-11-12"
__usage__   = """
User input : <filename> [filename ...]
App output : content of all provided files with line numbering"""
# ==============================================================================
from ezCLI import *
# ------------------------------------------------------------------------------
def numlines(name:str) -> str:
  """add line numbering to all lines of file 'name'"""
  text = read_txt(name) # read whole content of file 'name'
  lines = text.split('\n')
  size = len(str(len(lines))) # max number of digits for counter
  lines = [f"{p+1:>{size}} - {line}" for p,line in enumerate(lines)]
  rule = '\n' + '\u2500'*80 + '\n' # horizontal rule (used as a separator)
  return name + ' : ' + rule + '\n'.join(lines) + rule
# ------------------------------------------------------------------------------
def parser(command:str) -> str:
  """parse 'command' and return content of provided files with line numbering"""
  names = parse(command); #inspect()
  return '\n'.join(numlines(name) for name in names)
# ==============================================================================
if __name__ == '__main__':
  userloop(parser, "Enter <filename> [filename ...]")
# ==============================================================================
