# tabel.py
#
# MIT-License
#
# Copyright © 2023 Theo van Haandel  py39@x4a.nl
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.

# versie 1.0  2023-10-19

# formatteren tabel

# eerst max breedte bepalen
# daarna printen

def formt(tabel):
    print('zonder ')
    for rij in tabel:
        # print('len', len(rij), end = ' ')
        for cel in rij:
            print(cel, end = ' ')
        print()
    print()
    # return

    breedte = [ 0 ] * len(tabel[0]) # variabele rijen?
    for rij in tabel:
        col = 0
        for cel in rij:
        # for col, cel in enumerate(rij):
            ln = len(str(cel))
            if breedte[col] < ln:
                breedte[col] = ln
            # breedte[col] = max(breedte[col], len(str(cel)))
            col += 1
    for rij in tabel:
        col = 0
        for cel in rij:
            width = breedte[col]
            if isinstance(cel, int):
                val = str(cel).rjust(width) # , '0')  # fillchar
            elif isinstance(cel, float):
                val = str(cel).rjust(width)
            else:
                val = str(cel).ljust(width)
            print(val, end = ' ')
            col += 1
        print()
                            

tabel1 = (
    ( 3, -100, 2, 5, 17, 12345, -12, 0),
    ( "'", 'x', "nog wat langer", '"', '--', 'krt', 'X', 'o'),
    ( 2.7, -3.0, 127.4, 4e100, 23., 23, 4j, 1j ), # 1+4j ),
    )

formt(tabel1)
