jazzband/prettytable

Add color to field names

allenweiss opened this issue · 4 comments

I think you should add a way to color the field names.

table.field_names

Right now, anything I do to color the field names messes up the entire table.

For example, if I try this:

from colorama import Fore, Back, Style, init

table.field_names = [Fore.CYAN+"CLASS SPECIFIC COMMANDS"+Style.RESET_ALL, "OTHER COMMANDS"]
  1. it forces everything to be centered (instead left left-aligned like I want)
  2. the color doesn't work

In short, it would be nice to be table to color the headings in the field_names.

Please could you include a minimal, complete code example that reproduces? It'll make it easier to investigate.

Sure, here is an example

from colorama import Fore, Back, Style, init
from prettytable import PLAIN_COLUMNS, PrettyTable

column1_commands = [
    Fore.CYAN+"############## SEND EMAIL ##############"+Style.RESET_ALL,
    Fore.CYAN+"-s Send Email to the Class"+Style.RESET_ALL,
    "-so  Send Single Email to a Student (Add to Class - Optional)",
    "-st  Send Test to Me",
    "-f  Send Zoom Info to Student",
    "-gse Get List of Sent Emails",
     Fore.CYAN+"############## FIND STUDENT ##############"+Style.RESET_ALL,
    Fore.CYAN+"-f  Find a Current Student"+Style.RESET_ALL
]

column2_commands = [
      '############## BBP #################',
      Fore.CYAN+'-BBP  Send to Leads'+Style.RESET_ALL,
      '-usBBP  Process Leads UnSubs',
      '-upBBP  Upload BBP Leads to DB', 
      Fore.CYAN+'############## UPLOAD OR TRANSFER TO HISTORY ##############'+Style.RESET_ALL,
      '-ath  Transfer Class Students to History',
      '-ah  Upload General Spreadsheet to History (from Presentation, etc.)'
]

table = PrettyTable()  

table.field_names = [Fore.CYAN+"CLASS COMMANDS"+Style.RESET_ALL, "OTHER COMMANDS"]

table.align["CLASS COMMANDS"] = "l"
table.align["OTHER COMMANDS"] = "l"
table.set_style(PLAIN_COLUMNS)
table.left_padding_width = 3

# Since the lists can be of different lengths, find the longer length
max_length = max(len(column1_commands), len(column2_commands))
table.vertical_char = ' '  # Make sure vertical separation is minimal
table.horizontal_char = '-'  # Horizontal separation
table.junction_char = ' '  # Junction character

# Fill in the table, row by row
for i in range(max_length):
    col1 = column1_commands[i] if i < len(column1_commands) else ""
    col2 = column2_commands[i] if i < len(column2_commands) else ""
    table.add_row([col1, col2])

print(table)

Thanks, when adding the imports and printing the table at the end, I get:

image

The problem is here:

table.field_names = [Fore.CYAN+"CLASS COMMANDS"+Style.RESET_ALL, "OTHER COMMANDS"]

table.align["CLASS COMMANDS"] = "l"
table.align["OTHER COMMANDS"] = "l"

The first column isn't called "CLASS COMMANDS", it's really called "Fore.CYAN+"CLASS COMMANDS"+Style.RESET_ALL".

So the quick fix is:

table.field_names = [Fore.CYAN+"CLASS COMMANDS"+Style.RESET_ALL, "OTHER COMMANDS"]

table.align[Fore.CYAN+"CLASS COMMANDS"+Style.RESET_ALL] = "l"
table.align["OTHER COMMANDS"] = "l"

Which gives:

image

How is that for you?

Thanks that works perfectly.