annacrombie / plot

plot on the command line
MIT License
113 stars 9 forks source link

Integration with ncurses? #21

Closed alextoind closed 3 years ago

alextoind commented 3 years ago

Hello there,

I've just come across your porting and it seems really interesting!

I need a few time to check your code and see if it can fit my needs, but I'd like to know first if it is possible to integrate a plot like this inside a ncurses window interface. For example something like some text and a data table at the top of the window and the plot with the flowing data at the bottom.

Many thanks for your time!

annacrombie commented 3 years ago

Thank you. I think that plot will work OK with ncurses, when #20 is merged and we get plot_string into master, which should be in a few days. I have written an example of interaction with ncurses.

Unfortunately, the way colored output is currently implemented won't work with ncurses. Compatibility might be considered in the future though. You can of course color the whole plot, or write an ANSI color escape parser if you really want.

So, in summary, yes you can use plot inside of ncurses if you wait until #20 is merged and are okay with monotone output.

alextoind commented 3 years ago

Sounds great! Thank you very much!

alextoind commented 3 years ago

@annacrombie I've tried the ncurses example after the #20 merge, but it seems to not plot correctly the special characters required for the plot.

I've tried to pause at start to better understand, and this is the outcome after the first plot_fetch:

amp: 5,000000, freq: 0,200000

       8,00 M-b~TM-$

       7,53 M-b~TM-$

       7,06 M-b~TM-$

       6,59 M-b~TM-$

       6,12 M-b~TM-$

       5,65 M-b~TM-$

       5,18 M-b~TM-$

       4,71 M-b~TM-$

       4,24 M-b~TM-$

If I start it and pause it after a while it does something like this:

amp: 5,000000, freq: 0,200000

       5,00 M-b~TM-$      M-b~UM--M-b~T~@M-b~T~@M-b~UM-.                           M-b~UM--M-b~T~@M-b~T~@M-b~UM-.

       4,41 M-b~TM-$    M-b~UM--M-b~T~@M-b~UM-/  M-b~UM-0M-b~UM-.                         M-b~UM--M-b~UM-/  M-b~UM-0M-b~
T~@M-b~UM-.
       3,82 M-b~TM-$   M-b~UM--M-b~UM-/     M-b~UM-0M-b~UM-.                       M-b~UM--M-b~UM-/     M-b~UM-0M-b~UM-.

       3,24 M-b~TM-$   M-b~T~B       M-b~UM-0M-b~UM-.                     M-b~UM--M-b~UM-/       M-b~T~B

       2,65 M-b~TM-$  M-b~UM--M-b~UM-/        M-b~UM-0M-b~UM-.                   M-b~UM--M-b~UM-/        M-b~UM-0M-b~T~@

       2,06 M-b~TM-$ M-b~UM--M-b~UM-/          M-b~T~B                   M-b~T~B

       1,48 M-b~TM-$ M-b~T~B           M-b~UM-0M-b~UM-.                 M-b~UM--M-b~UM-/

       0,89 M-b~TM-$M-b~UM--M-b~UM-/            M-b~UM-0M-b~UM-.                M-b~T~B

       0,30 M-b~TM-$M-b~T~B              M-b~T~B               M-b~UM--M-b~UM-/

Have you got some suggestion to where it is better to investigate?

Thank you for your help

annacrombie commented 3 years ago

You probably need to call setlocale

[Curses] uses the locale which the calling program has initialized. That is normally done with setlocale:

       setlocale(LC_ALL, "");

If the locale is not initialized, the library assumes that characters are printable as in ISO-8859-1, to work with certain legacy programs. You should initialize the locale and not rely on specific details of the library when the locale has not been setup.

source

Also note that you must call this before any curses initialization, for example simple_curses.c calls it here.

If that isn't it, maybe you could post your code so I can investigate further.

alextoind commented 3 years ago

Thanks for the prompt response.

Actually I just run your simple_curses.c example. And I only changed the paused variable to start in paused mode. The setlocale(LC_ALL, ""); is already set in your example.

Moreover, the other simple.c example works fine as for the CLI plot tool.

If it can help, I'm on an old Ubuntu 16.04 4.15.0-39-generic with libncurses5 6.0+20160213-1ubuntu1

annacrombie commented 3 years ago

Hmm. Can you post the output of the locale command?

alextoind commented 3 years ago

Here you are:

LC_CTYPE=en_US.UTF-8;LC_NUMERIC=it_IT.UTF-8;LC_TIME=it_IT.UTF-8;LC_COLLATE=en_US.UTF-8;LC_MONETARY=it_IT.UTF-8;LC_MESSAG
ES=en_US.UTF-8;LC_PAPER=it_IT.UTF-8;LC_NAME=it_IT.UTF-8;LC_ADDRESS=it_IT.UTF-8;LC_TELEPHONE=it_IT.UTF-8;LC_MEASUREMENT=i
t_IT.UTF-8;LC_IDENTIFICATION=it_IT.UTF-8

Is there any problem?

EDIT: The previous output is returned by the setlocale inside the example, here is the output of the locale command from the terminal:

LANG=en_US.UTF-8
LANGUAGE=en
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC=it_IT.UTF-8
LC_TIME=it_IT.UTF-8
LC_COLLATE="en_US.UTF-8"
LC_MONETARY=it_IT.UTF-8
LC_MESSAGES="en_US.UTF-8"
LC_PAPER=it_IT.UTF-8
LC_NAME=it_IT.UTF-8
LC_ADDRESS=it_IT.UTF-8
LC_TELEPHONE=it_IT.UTF-8
LC_MEASUREMENT=it_IT.UTF-8
LC_IDENTIFICATION=it_IT.UTF-8
LC_ALL=
annacrombie commented 3 years ago

Okay, so I set up a ubuntu 18 image and installed plot on it, and saw the same error more or less. The unicode characters were being displayed wrong.

First, I tried installing libncursesw5-dev (notice the w). This is the wide character variant of curses. The result was that the output was still wrong, but a little better.

After some more research I came across this stack overflow post.

Following these instructions finally fixed output for me:

To create a USA english locale with UTF-8 support, just do:

sudo localedef -i en_US -c -f UTF-8 en_US.UTF-8

then, be sure the locale varaibles are set and exported:

export LOCALE=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
alextoind commented 3 years ago

Thanks for your suggestions, but it seems not solving even after installing libncursesw5-dev. If I find a solution I'll come back to you.

annacrombie commented 3 years ago

I'm sorry to hear that. I'm pretty sure it is a locale issue, but if all else fails, you can use an alternate plot charset:


// This function accepts either plot_charset_unicode, or plot_charset_ascii
// as the charset option.  If you use ascii, then your encoding issues 
// won't matter as plot will use only output ascii characters
void plot_set_charset(struct plot *plot, enum plot_charset charset);

// If you don't like the look of plot_charset_ascii, you can design your 
// own with plot_set_custom_charset.  

void plot_set_custom_charset(struct plot *plot, char *str, uint32_t len);

// For example, to get the default unicode charset:
char *custom_charset = " ╭│╰├╮─┬╯┤┴┼";
plot_set_custom_charset(plot, custom_charset, strlen(custom_charset));

// If you pass an insufficient number of codepoints, the missing ones will
// show up as '?'.
alextoind commented 3 years ago

@annacrombie I've made it work on another PC! There's maybe some problem on my current configuration, but it is not a problem. Thank you for your help!