2016-05-21 7 views
0

すべてのarround文字列を印刷したいと思います。私は185から188までと200から206までのASCII文字を使用したいと思っています。このようなものが欲しいですが、右下に間違ったアライメントがあるため、最後の行が気に入らないのです。それを改善することは可能でしょうか?フレーミングされた文字列の印刷方法

retString = "\n╔══> Stanza n. %03d <══╗" % nRoom 
retString += "\n╠═> Num letti: %-3d  ║" % nBeds 
retString += "\n╠═> Fumatori   ║" 
retString += "\n╠═> Televisione   ║" 
retString += "\n╠═> Aria Condizionata ║" 
retString += "\n╚══════════════╝" 
return retString 

Output

+2

それは私の作品。ターミナル設定に何か問題がありますか?モノスペースフォントですか? – leovp

+0

@leovp同じ問題があります –

+0

@Christian Cundari最下行を長くするのはどうですか?可変文字列長の場合は、フレームのエントリのリストを作成し、最長のものを選択して、最下行を同じ長さにすることができます。縦線の整列に関しては、おそらく 'some_entry_in_your_list + =(maxlength-len(some_entry_in_your_list))*" "+ ||' –

答えて

1

手でボックスを描画しようとしないでください。 それは壊れます。

私はここにクリーンバージョンを掲載していドキュメンテーション上の理由から、私は一度、ボックスを描画するために、いくつかの機能が必要:結果は次のようになります

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 ..║ 
╚════════════════════════════════════════╝ 
+0

ありがとうございます。これは非常に便利です –

関連する問題