Home > Enterprise >  How would I format console output in python correctly?
How would I format console output in python correctly?

Time:11-06

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

CodePudding user response:

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                                         ╣
      ←←←←←←    ←←←←←←←←←←←←←        ╠                                                         ╣
    ←←←←←         ↖←←←←←←←←←←←←      ╠                                                         ╣
   ←←←←     ←←      ←←←←←←←←←←←←     ╠                                                         ╣
 ↖←←←←←     ←←←     ←←←←←←←←←←←←←↖   ╠                                                         ╣
 ←←←←←←←     ←←←    ←←←    ↖←←←←←←   ╠                                                         ╣
←←←←←←←←←     ←↖   ↖←←↖    ←←←←←←←←  ╠═════════════════════════════════════════════════════════╣
←←←←←←←←←←        ←←←←    ↖←←←←←←←←
←←←←←←←←←←←    ↖←←←←←←   ↖←←←←←←←←←
←←←←←←←←←←←←    ←←←←←←  ←←←←←←←←←←←
←←←←←←←←←←←←←    ←←←←←↖←←←←←←←←←←←←
 ←←←←←←←←←←←←←   ↖←←←↖ ←←←←←←←←←←←
 ↖←←←←←←←←←←←←←  ←←←←  ←←←←←←←←←←↖
   ←←←←←←←←←←←←←←←←←←←←←←←←←←←←←
    ←←←←                   ←←←←
      ←←←←←←←←←←←←←←←←←←←←←←←
        ←←←←←←←←←←←←←←←←←←←
            ←←←←←←←←←←←

CodePudding user response:

You have aligned the text according to "{uptime_str}" length instead of the actual length of the string contained. There is mismatch between the alignment in your source code and the visual output.

Use len(uptime_str) and then some padding mechanism like:

"mystring".rjust(how_much_to_pad) or

"mystring".ljust(how_much_to_pad) or

" " * len(uptime_str)

The same problem applies to your os_string

CodePudding user response:

You just need to specify the minimum and maximum field width to be the same.

I have selected 50 chars here:

                    print(Fore.CYAN   f"""


            ←←←←←←←←←←←              ╠═══════════════ ROGUE FETCH ═════════════════════════════╣
        ←←←←←←←←←←←←←←←←←←←          ╠  {                                 os_string:50.50}     ╣
      ←←←←←←    ←←←←←←←←←←←←←        ╠                                                         ╣
    ←←←←←         ↖←←←←←←←←←←←←      ╠  {                                uptime_str:50.50}     ╣
   ←←←←     ←←      ←←←←←←←←←←←←     ╠                                                         ╣
 ↖←←←←←     ←←←     ←←←←←←←←←←←←←↖   ╠                                                         ╣
 ←←←←←←←     ←←←    ←←←    ↖←←←←←←   ╠                                                         ╣
←←←←←←←←←     ←↖   ↖←←↖    ←←←←←←←←  ╠═════════════════════════════════════════════════════════╣
←←←←←←←←←←        ←←←←    ↖←←←←←←←←
←←←←←←←←←←←    ↖←←←←←←   ↖←←←←←←←←←
←←←←←←←←←←←←    ←←←←←←  ←←←←←←←←←←←
←←←←←←←←←←←←←    ←←←←←↖←←←←←←←←←←←←
 ←←←←←←←←←←←←←   ↖←←←↖ ←←←←←←←←←←← 
 ↖←←←←←←←←←←←←←  ←←←←  ←←←←←←←←←←↖ 
   ←←←←←←←←←←←←←←←←←←←←←←←←←←←←←   
    ←←←←                   ←←←←    
      ←←←←←←←←←←←←←←←←←←←←←←←      
        ←←←←←←←←←←←←←←←←←←←        
            ←←←←←←←←←←←            
  
        """)
  • Related