手でボックスを描画しようとしないでください。 それは壊れます。
私はここにクリーンバージョンを掲載していドキュメンテーション上の理由から、私は一度、ボックスを描画するために、いくつかの機能が必要:結果は次のようになります
UL, UR = '╔', '╗'
SL, SR = '╠', '║'
DL, DR = '╚', '╝'
AL, AR = '═', '>'
def padded(
line, info=None, width=42, intro='>', outro='<', filler='.', chopped='..'
):
# cleanup input
line = ''.join([' ', line.strip()]) if line else ''
info = info.strip() if info else ''
# determine available width
width -= sum([len(intro), len(outro), len(line), len(info)])
if width < 0:
# chop off overflowing text
line = line[:len(line)+width]
if chopped:
# place chopped characters (if set)
chopped = chopped.strip()
line = ' '.join([line[:len(line)-(len(chopped)+1)], chopped])
return ''.join(e for e in [
intro,
info,
line,
''.join(filler for _ in range(width)),
outro
] if e)
def box(rnum, nbeds, *extras):
arrow = (AL+AR)
res = [
# head line
padded(
'Stanza n. {:03d} <'.format(rnum), (AL+AL+arrow),
intro=UL, outro=UR, filler=AL
),
# first line
padded(
'Num letti: {:3d}'.format(nbeds), arrow,
intro=SL, outro=SR, filler=' '
),
]
# following lines
res.extend(padded(e, arrow, intro=SL, outro=SR, filler=' ') for e in extras)
# bottom line
res.append(padded(None, None, intro=DL, outro=DR, filler=AL))
return '\n'.join(res)
print(
box(485, 3, 'Fumatori', 'Televisione')
)
print(
box(123, 4, 'Fumatori', 'Televisione', 'Aria Condizionata')
)
print(
box(1, 1, 'this is so much text it will be chopped off')
)
:
╔═══> Stanza n. 485 <════════════════════╗
╠═> Num letti: 3 ║
╠═> Fumatori ║
╠═> Televisione ║
╚════════════════════════════════════════╝
╔═══> Stanza n. 123 <════════════════════╗
╠═> Num letti: 4 ║
╠═> Fumatori ║
╠═> Televisione ║
╠═> Aria Condizionata ║
╚════════════════════════════════════════╝
╔═══> Stanza n. 001 <════════════════════╗
╠═> Num letti: 1 ║
╠═> this is so much text it will be ch ..║
╚════════════════════════════════════════╝
それは私の作品。ターミナル設定に何か問題がありますか?モノスペースフォントですか? – leovp
@leovp同じ問題があります –
@Christian Cundari最下行を長くするのはどうですか?可変文字列長の場合は、フレームのエントリのリストを作成し、最長のものを選択して、最下行を同じ長さにすることができます。縦線の整列に関しては、おそらく 'some_entry_in_your_list + =(maxlength-len(some_entry_in_your_list))*" "+ ||' –