# ==============================================================================
"""TXTFILE : demo for 'read_txt/write_txt' functions from the 'ezCLI' toolbox"""
# ==============================================================================
__author__  = "Christophe Schlick modified by Philippe Blasi"
__version__ = "1.0"
__date__    = "2022-11-12"
__usage__   = """
Simply press <ENTER> at each pause""" 
# ==============================================================================
from ezCLI import *
# ------------------------------------------------------------------------------
# Sample use cases for 'read_txt'
# ------------------------------------------------------------------------------
# read the whole content from file
txt = read_txt('test-txt.txt')
pause(f">>> read all lines :\n{txt}")

# return line at index 4 from file
txt = read_txt('test-txt.txt', 4)
pause(f">>> read line at index 4 :\n{txt}")

# return lines in range(4,7) from file
txt = read_txt('test-txt.txt', 4, 7)
pause(f">>> read lines in range(4,7) :\n{txt}")

# return the first 3 lines from file
txt = read_txt('test-txt.txt', None, 3)
pause(f">>> read the first 3 lines :\n{txt}")

# return the last 5 lines from file
txt = read_txt('test-txt.txt', -5, None)
pause(f">>> read the last 5 lines :\n{txt}")

# ------------------------------------------------------------------------------
# Sample use cases for 'write_txt'
# ------------------------------------------------------------------------------
# replace the whole file and return the new file content
txt = write_txt('test.txt', 'ccc\nccc\nccc')
pause(f">>> replace whole content :\n{txt}")

# insert line at index 2 and return new file content
txt = write_txt('test.txt', 'ddd', 2)
pause(f">>> insert line at index 2 :\n{txt}")

# insert line at end of file and return new file content
txt = write_txt('test.txt', 'eee', -1)
pause(f">>> insert line at end of file :\n{txt}")

# replace first line and return new file content
txt = write_txt('test.txt', 'aaa\nbbb', None, 1)
pause(f">>> replace the first line :\n{txt}")

# replace the last 2 lines and return new file content
txt = write_txt('test.txt', 'eee\nfff\nggg', -2, None)
pause(f">>> replace the last 2 lines :\n{txt}")
# ==============================================================================
