Search code examples
pythonterminalconsolealignmentascii

How would I format console output in python correctly?


from colorama import Fore
import platform
import distro
import time
import os

# Functions
def get_uptime():
    return os.popen('uptime -p').read()[:-1]

def get_os():
    return platform.system() + " " + platform.release()

# Main Entry
def main():
    linux_distro = distro.name()
    os_string = f"OS: {get_os()}"
    uptime_str = f"Uptime: {get_uptime()}"
    if 'pop' in linux_distro.lower():
        print(Fore.CYAN + f"""


            ←←←←←←←←←←←              ╠═══════════════ ROGUE FETCH ═════════════════════════════╣
        ←←←←←←←←←←←←←←←←←←←          ╠  {os_string}                                            ╣
      ←←←←←←    ←←←←←←←←←←←←←        ╠                                                         ╣
    ←←←←←         ↖←←←←←←←←←←←←      ╠  {uptime_str}                                           ╣
   ←←←←     ←←      ←←←←←←←←←←←←     ╠                                                         ╣
 ↖←←←←←     ←←←     ←←←←←←←←←←←←←↖   ╠                                                         ╣
 ←←←←←←←     ←←←    ←←←    ↖←←←←←←   ╠                                                         ╣
←←←←←←←←←     ←↖   ↖←←↖    ←←←←←←←←  ╠═════════════════════════════════════════════════════════╣
←←←←←←←←←←        ←←←←    ↖←←←←←←←←
←←←←←←←←←←←    ↖←←←←←←   ↖←←←←←←←←←
←←←←←←←←←←←←    ←←←←←←  ←←←←←←←←←←←
←←←←←←←←←←←←←    ←←←←←↖←←←←←←←←←←←←
 ←←←←←←←←←←←←←   ↖←←←↖ ←←←←←←←←←←← 
 ↖←←←←←←←←←←←←←  ←←←←  ←←←←←←←←←←↖ 
   ←←←←←←←←←←←←←←←←←←←←←←←←←←←←←   
    ←←←←                   ←←←←    
      ←←←←←←←←←←←←←←←←←←←←←←←      
        ←←←←←←←←←←←←←←←←←←←        
            ←←←←←←←←←←←            
  
        """)

I made the frame with the symbols, however on running, it looks like unaligned and looks wrong: link: https://media.discordapp.net/attachments/1164305090144915556/1170754515495030895/image.png?ex=655a311c&is=6547bc1c&hm=41877fdc735ae2b06a1c7588f7c7ba397c2dd69cf8229d2ef6df6348379263f9&=&width=1160&height=487


Solution

  • You can format the string within the f string variable definition. For example all of these print with a bar on each side with a 25 character fields between them. figure out how wide you need for the var string you are using and make it that big and add remaining spaces as needed.

    print(f"|{variable:25}|")    # default formating, string left justified, numeric right
    print(f"|{variable:>25}|")   # right justifed
    print(f"|{variable:<25}|")   # left justifed
    print(f"|{variable:^25}|")   # center justified
    

    I reworked your code so it would work locally to give you idea what I mean:

    import platform
    import os
    
    # Functions
    
    def get_os():
        return platform.system() + " " + platform.release()
    
    # Main Entry
    def main():
        os_string = f"OS: {get_os()}"
        print( f"""
    
    
                ←←←←←←←←←←←              ╠═══════════════ ROGUE FETCH ═════════════════════════════╣
            ←←←←←←←←←←←←←←←←←←←          ╠  {os_string:<25}                              ╣
          ←←←←←←    ←←←←←←←←←←←←←        ╠                                                         ╣
        ←←←←←         ↖←←←←←←←←←←←←      ╠                                                         ╣
       ←←←←     ←←      ←←←←←←←←←←←←     ╠                                                         ╣
     ↖←←←←←     ←←←     ←←←←←←←←←←←←←↖   ╠                                                         ╣
     ←←←←←←←     ←←←    ←←←    ↖←←←←←←   ╠                                                         ╣
    ←←←←←←←←←     ←↖   ↖←←↖    ←←←←←←←←  ╠═════════════════════════════════════════════════════════╣
    ←←←←←←←←←←        ←←←←    ↖←←←←←←←←
    ←←←←←←←←←←←    ↖←←←←←←   ↖←←←←←←←←←
    ←←←←←←←←←←←←    ←←←←←←  ←←←←←←←←←←←
    ←←←←←←←←←←←←←    ←←←←←↖←←←←←←←←←←←←
     ←←←←←←←←←←←←←   ↖←←←↖ ←←←←←←←←←←← 
     ↖←←←←←←←←←←←←←  ←←←←  ←←←←←←←←←←↖ 
       ←←←←←←←←←←←←←←←←←←←←←←←←←←←←←   
        ←←←←                   ←←←←    
          ←←←←←←←←←←←←←←←←←←←←←←←      
            ←←←←←←←←←←←←←←←←←←←        
                ←←←←←←←←←←←            
      
            """)
        
    
    main()
    

    i received the following as output:

                ←←←←←←←←←←←              ╠═══════════════ ROGUE FETCH ═════════════════════════════╣
            ←←←←←←←←←←←←←←←←←←←          ╠  OS: Windows 11                                         ╣
          ←←←←←←    ←←←←←←←←←←←←←        ╠                                                         ╣
        ←←←←←         ↖←←←←←←←←←←←←      ╠                                                         ╣
       ←←←←     ←←      ←←←←←←←←←←←←     ╠                                                         ╣
     ↖←←←←←     ←←←     ←←←←←←←←←←←←←↖   ╠                                                         ╣
     ←←←←←←←     ←←←    ←←←    ↖←←←←←←   ╠                                                         ╣
    ←←←←←←←←←     ←↖   ↖←←↖    ←←←←←←←←  ╠═════════════════════════════════════════════════════════╣
    ←←←←←←←←←←        ←←←←    ↖←←←←←←←←
    ←←←←←←←←←←←    ↖←←←←←←   ↖←←←←←←←←←
    ←←←←←←←←←←←←    ←←←←←←  ←←←←←←←←←←←
    ←←←←←←←←←←←←←    ←←←←←↖←←←←←←←←←←←←
     ←←←←←←←←←←←←←   ↖←←←↖ ←←←←←←←←←←←
     ↖←←←←←←←←←←←←←  ←←←←  ←←←←←←←←←←↖
       ←←←←←←←←←←←←←←←←←←←←←←←←←←←←←
        ←←←←                   ←←←←
          ←←←←←←←←←←←←←←←←←←←←←←←
            ←←←←←←←←←←←←←←←←←←←
                ←←←←←←←←←←←