繁体   English   中英

ASCII 字符文本对齐

[英]ASCII Characters Text Align

我正在用 python 编写一个非常小的和基本的基于文​​本的冒险游戏,我希望能够对齐我的标题(这是在 ASCII 中)我已经查过了,但我要么不理解,要么不理解”工作。

这是ASCII:

  ________            ___                         
 /_  __/ /_  ___     /   |  ________  ____  ____ _
  / / / __ \/ _ \   / /| | / ___/ _ \/ __ \/ __ `/
 / / / / / /  __/  / ___ |/ /  /  __/ / / / /_/ / 
/_/ /_/ /_/\___/  /_/  |_/_/   \___/_/ /_/\__,_/  

(这不是很有创意,我知道)

我想让它与中心对齐,这可能吗?

您可以使用str.center方法。 下面的代码假设屏幕宽度为 80 个字符:

s = '''  ________            ___                         
 /_  __/ /_  ___     /   |  ________  ____  ____ _
  / / / __ \/ _ \   / /| | / ___/ _ \/ __ \/ __ `/
 / / / / / /  __/  / ___ |/ /  /  __/ / / / /_/ / 
/_/ /_/ /_/\___/  /_/  |_/_/   \___/_/ /_/\__,_/ '''
print('\n'.join(l.center(80) for l in s.splitlines()))

这输出:

                 ________            ___                                        
                /_  __/ /_  ___     /   |  ________  ____  ____ _               
                 / / / __ \/ _ \   / /| | / ___/ _ \/ __ \/ __ `/               
                / / / / / /  __/  / ___ |/ /  /  __/ / / / /_/ /                
               /_/ /_/ /_/\___/  /_/  |_/_/   \___/_/ /_/\__,_/                 

我会:

  • 获取终端窗口的大小(主要是列) - linux 命令: “stty -a” ,windows 命令: “mode con” - 并解析 cli 输出,或在此处执行操作: How to get Linux console window width in Python

  • 获取文本的最大大小(需要最多列的行)

  • 然后: padding_left = (terminal_columns - text-max_columns) / 2 , ceil 或 floor 你想要的数字
  • 在所有文本行前面加上空格,因为padding_left值是

编辑

这是一个示例(适用于 Linux 和 Windows):

import shutil
import math


def str_repeat(string, length):
    return (string * (int(length / len(string)) + 1))[:length]


def print_center(lines_out):
    columns, rows = shutil.get_terminal_size()
    max_line_size = 0
    left_padding = 0

    for line in lines_out:
        if max_line_size == 0 or max_line_size < len(line):
            max_line_size = len(line)

    if columns > max_line_size:
        left_padding = math.floor((columns - max_line_size) / 2)

    if left_padding > 0:
        for line in lines_out:
            print(str_repeat(' ', left_padding) + line)


lines = [
    '  ________            ___',
    ' /_  __/ /_  ___     /   |  ________  ____  ____ _',
    '  / / / __ \/ _ \   / /| | / ___/ _ \/ __ \/ __ `/',
    ' / / / / / /  __/  / ___ |/ /  /  __/ / / / /_/ /',
    '/_/ /_/ /_/\___/  /_/  |_/_/   \___/_/ /_/\__,_/'
]

print_center(lines)

输出:

                 ________            ___
                /_  __/ /_  ___     /   |  ________  ____  ____ _
                 / / / __ \/ _ \   / /| | / ___/ _ \/ __ \/ __ `/
                / / / / / /  __/  / ___ |/ /  /  __/ / / / /_/ /
               /_/ /_/ /_/\___/  /_/  |_/_/   \___/_/ /_/\__,_/

Process finished with exit code 0

输出截图

您可以逐行添加将文本居中所需的空白数量。

这是一种方法,您必须提供要居中的文本以及可用的宽度:

def align_center(text, width):
    lext = text.split('\n')
    for line in lext:
        assert len(line) < width, 'insufficient width'
    max_w = max(len(line) for line in lext)
    res = []
    for line in lext:
        res.append(' ' * ((width - max_w) // 2))
        res.append(line)
        res.append('\n')
    return ''.join(res)


text = """  ________            ___                         
 /_  __/ /_  ___     /   |  ________  ____  ____ _
  / / / __ \/ _ \   / /| | / ___/ _ \/ __ \/ __ `/
 / / / / / /  __/  / ___ |/ /  /  __/ / / / /_/ / 
/_/ /_/ /_/\___/  /_/  |_/_/   \___/_/ /_/\__,_/  """

centered = align_center(text, 90)
print(centered)

输出:

              ________            ___                         
             /_  __/ /_  ___     /   |  ________  ____  ____ _
              / / / __ \/ _ \   / /| | / ___/ _ \/ __ \/ __ `/
             / / / / / /  __/  / ___ |/ /  /  __/ / / / /_/ / 
            /_/ /_/ /_/\___/  /_/  |_/_/   \___/_/ /_/\__,_/  

不要自己输入 ASCII 艺术 - 使用 text2art:

from art import *

text2art("The Arena")

对此的替代方案是:

from art import *

tprint("The Arena")

要将其居中,请执行以下操作:

from art import *

string="The Arena"
new_string = string.center(90)

tprint(new_string)

或(使用text2art代替):

text2art(new_string)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM