kroitor / asciichart

Nice-looking lightweight console ASCII line charts ╭┈╯ for NodeJS, browsers and terminal, no dependencies
MIT License
1.83k stars 94 forks source link

Adding support for colours to Python, copied from JavaScript #52

Closed jwbensley closed 3 years ago

jwbensley commented 4 years ago

Hi @kroitor,

Sorry this is so late. I said I'd make a pull request to add colour support to the Python version several weeks ago!

This request just copies the colour code from the JS file to the Python file, implementing colour support in Python exactly the same way it's implemented in JavaScript, to keep the Python version of asciichart consistent with the JS version.

Below is the script I used to generate a small test with multiple data series (which is why colouring is needed);

#!/usr/bin/env python3

import asciichartpy
import random
from time import sleep

S1 = [random.randint(-100, -25) for x in range(1, 60)]
S2 = [random.randint(-25, 50) for x in range(1, 60)]
S3 = [random.randint(25, 100) for x in range(1, 60)]

while True:

    print("\033[H\033[J", end='') # clear screen

    config = {
        "height": 20,
        "colors": [
            asciichartpy.green,
            asciichartpy.blue,
            asciichartpy.red
        ]
    }

    for i in range(0, len(S1)-1):
        S1[i] = S1[i+1]
    S1[-1] = random.randint(-100, 0)

    for i in range(0, len(S2)-1):
        S2[i] = S2[i+1]
    S2[-1] = random.randint(-50, 50)

    for i in range(0, len(S3)-1):
        S3[i] = S3[i+1]
    S3[-1] = random.randint(0, 100)

    print(asciichartpy.plot([S1, S2, S3], config))
    sleep(1)

Here is a screenshot showing the output:

jwbensley commented 4 years ago

I will look into these failures and update the pull request.

kroitor commented 4 years ago

@jwbensley i think this is due to doctest + line endings, however, it requires a bit more investigation. Your help would be very much appreciated!

jwbensley commented 4 years ago

Hi @kroitor,

After looking into this issue a bit more the problem relates to the way the colour codes are always used in my pull request.

I tried to implement the colouring in the Python version in the same way it is implemented in the JS version to keep the to files aligned. When no colour is specified in the JS version it prints the chart with no colour symbols:

    function colored (char, color) {
        // do not color it if color is not specified
        return (color === undefined) ? char : (color + char + exports.reset)
    }

In the python version I was defaulting to the reset colour which is effectively no colour but it stll prints a colour symbol:

reset = "\033[0m"
...
colors = cfg.get('colors', [reset])

It can't be seen in any diff output for example (f1 is the current master branch and f2 is my original pull request):

$diff ./f1 ../asciichart2/f2
1,4c1,4
<     4.00  ┤  ╭╴╶╮
<     3.00  ┤ ╭╯  ╰╮
<     2.00  ┤╭╯    ╰╮
<     1.00  ┼╯      ╰
---
>     4.00  ┤  ╭╴╶╮
>     3.00  ┤ ╭╯  ╰╮
>     2.00  ┤╭╯    ╰╮
>     1.00  ┼╯      ╰

But it can be seen with hexdump:

hexdump -C f1
00000000  20 20 20 20 34 2e 30 30  20 20 e2 94 a4 20 20 e2  |    4.00  ...  .|
00000010  95 ad e2 95 b4 e2 95 b6  e2 95 ae 0a 20 20 20 20  |............    |
00000020  33 2e 30 30 20 20 e2 94  a4 20 e2 95 ad e2 95 af  |3.00  ... ......|
00000030  20 20 e2 95 b0 e2 95 ae  0a 20 20 20 20 32 2e 30  |  .......    2.0|
00000040  30 20 20 e2 94 a4 e2 95  ad e2 95 af 20 20 20 20  |0  .........    |
00000050  e2 95 b0 e2 95 ae 0a 20  20 20 20 31 2e 30 30 20  |.......    1.00 |
00000060  20 e2 94 bc e2 95 af 20  20 20 20 20 20 e2 95 b0  | ......      ...|
00000070  0a                                                |.|
00000071

$hexdump -C ../asciichart2/f2
00000000  20 20 20 20 34 2e 30 30  20 20 e2 94 a4 20 20 1b  |    4.00  ...  .|
00000010  5b 30 6d e2 95 ad 1b 5b  30 6d 1b 5b 30 6d e2 95  |[0m....[0m.[0m..|
00000020  b4 1b 5b 30 6d 1b 5b 30  6d e2 95 b6 1b 5b 30 6d  |..[0m.[0m....[0m|
00000030  1b 5b 30 6d e2 95 ae 1b  5b 30 6d 0a 20 20 20 20  |.[0m....[0m.    |
00000040  33 2e 30 30 20 20 e2 94  a4 20 1b 5b 30 6d e2 95  |3.00  ... .[0m..|
00000050  ad 1b 5b 30 6d 1b 5b 30  6d e2 95 af 1b 5b 30 6d  |..[0m.[0m....[0m|
00000060  20 20 1b 5b 30 6d e2 95  b0 1b 5b 30 6d 1b 5b 30  |  .[0m....[0m.[0|
00000070  6d e2 95 ae 1b 5b 30 6d  0a 20 20 20 20 32 2e 30  |m....[0m.    2.0|
00000080  30 20 20 e2 94 a4 1b 5b  30 6d e2 95 ad 1b 5b 30  |0  ....[0m....[0|
00000090  6d 1b 5b 30 6d e2 95 af  1b 5b 30 6d 20 20 20 20  |m.[0m....[0m    |
000000a0  1b 5b 30 6d e2 95 b0 1b  5b 30 6d 1b 5b 30 6d e2  |.[0m....[0m.[0m.|
000000b0  95 ae 1b 5b 30 6d 0a 20  20 20 20 31 2e 30 30 20  |...[0m.    1.00 |
000000c0  20 e2 94 bc 1b 5b 30 6d  e2 95 af 1b 5b 30 6d 20  | ....[0m....[0m |
000000d0  20 20 20 20 20 1b 5b 30  6d e2 95 b0 1b 5b 30 6d  |     .[0m....[0m|
000000e0  0a                                                |.|
000000e1

I've adjusted my commit to match the JS version of asciichart, no colour symbols are printed when no colours are specified rather than defaulting to a "blank" colour. This is from my updated pull request:

$hexdump -C ../asciichart2/f2
00000000  20 20 20 20 34 2e 30 30  20 20 e2 94 a4 20 20 e2  |    4.00  ...  .|
00000010  95 ad e2 95 b4 e2 95 b6  e2 95 ae 0a 20 20 20 20  |............    |
00000020  33 2e 30 30 20 20 e2 94  a4 20 e2 95 ad e2 95 af  |3.00  ... ......|
00000030  20 20 e2 95 b0 e2 95 ae  0a 20 20 20 20 32 2e 30  |  .......    2.0|
00000040  30 20 20 e2 94 a4 e2 95  ad e2 95 af 20 20 20 20  |0  .........    |
00000050  e2 95 b0 e2 95 ae 0a 20  20 20 20 31 2e 30 30 20  |.......    1.00 |
00000060  20 e2 94 bc e2 95 af 20  20 20 20 20 20 e2 95 b0  | ......      ...|
00000070  0a                                                |.|
00000071
coveralls commented 4 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 731a258636101bd9118a74add992913d78fd5e35 on jwbensley:colours into 412354275471fdb9946cc9aac2622664b001a538 on kroitor:master.

jwbensley commented 3 years ago

Bump

jwbensley commented 3 years ago

Thanks!