2

I have a Python program that creates an indicator notice in Ubuntu. How does one figure out how many tabs one needs to add in order to make sure the output is always vertically in-line?

I tried counting the characters used and inserting spaces instead of tabs, but this does not work as a non-mono font is used.

Output shown in this picture:

Enter image description here

The code that outputs the lines:

for processName in processStatuses:
    if processName in cachedProcessStatuses:
        if processStatuses[processName] != cachedProcessStatuses[processName]:
            output += processName
            output += " : \t"
            output += processStatuses[processName]
            output += "\n"
    else:
        output += processName
        output += " : \t"
        output += processStatuses[processName]
        output += "\n"
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Programster
  • 12,242
  • 9
  • 49
  • 55

1 Answers1

3

… as a non-mono font is used.

In this case you need to know the font geometry (not a trivial task).

The string "iiii" takes less space than "MMMM", but without knowing the font geometry (exact width of individual characters as well as their relative horizontal position), you cannot know, whether a specific string is wider than a tabwidth or not.

eumiro
  • 207,213
  • 34
  • 299
  • 261
  • 3
    even then chances are there might not be correct integer counts which would align the text – none Jan 02 '13 at 14:35
  • 1
    There's http://stackoverflow.com/questions/2922295/calculating-the-pixel-size-of-a-string-with-python which is probably about as good as you can get, and that will only be a best guess, since this isn't using tkinter to do the actual display. – Silas Ray Jan 02 '13 at 14:37
  • 1
    Just wondering if forcing the first column to a certain width (char wise) would make sense/work... `'{:50}: {}'.format('a', 'b')` – Jon Clements Jan 02 '13 at 14:43
  • Alternatively, since the OP says he's targeting Ubuntu, he could try to push the output from Python through a terminal/OS based formatting utility via a subprocess or something, since that would presumably already know font settings and spacings for the display interface. – Silas Ray Jan 02 '13 at 14:44
  • perhaps someone knows how to get python to set the indicator font to a mono font instead then? I.e. somewhere near my call to pynotify.Notification(title, text) – Programster Jan 02 '13 at 15:15
  • @JonClements Tried it, didn't work (on IDLE's terminal). It seems `format` operates on chars but `\t` works on font width, even if the width of the tab itself is the same regardless. – jadkik94 Jan 02 '13 at 15:26
  • Perhaps there is a way to set ubuntu to use a mono font in the indicator? Even better if the notifier application could tell ubuntu to use a mono font in the indicator message. – Programster Jun 26 '15 at 08:54