Comments (17)
Hi! I am experiencing the same issue here, with Ubuntu 22.04, gnome-terminal, LANG=en_US.UTF-8
, and LC_CTYPE unset. The command:
$ { ./torture | head -75; sleep 1m; } | ./ttyplot -c '┆'
displays this:
The displayed image is the same whatever non-ASCII character I pass to -c
. I tried a bunch of them: ², Œ, à, ß, £, µ, ≤, ÷, →, ©, ™.
Looking at the source code, it is clear that non-ASCII characters cannot possibly work. The option -c
is parsed like this:
case 'c':
plotchar=optarg[0];
break;
Here, optarg
is an array of char
holding the UTF-8 code units of the provided character, and only the first one is stored in plotchar
. In the example above, I used the character “╎” (U+254E Box drawings light double dash vertical), which in UTF-8 is encoded as the sequence {0xe2, 0x95, 0x8e}, then plotchar
got initialized with 0xe2 (sign-extended to 32 bits).
from ttyplot.
@MIvanchev: I think we could use mbtowc()
to convert the character to a wchar_t
. This function does not add any dependency: it is in libc since C99. Then use mvvline_set()
instead of mvvline()
. The difference between these two functions is that the former takes the character as a cchar_t
(instead of chtype
), which is an array of wchar_t
with attributes.
Note that mbtowc()
only works after calling setlocale()
. I wrote this small code for testing mbtowc()
:
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
int main(int argc, char *argv[])
{
if (argc != 2) {
fprintf(stderr, "Usage: %s character\n", argv[0]);
return EXIT_FAILURE;
}
setlocale(LC_ALL, "");
wchar_t c;
if (mbtowc(&c, argv[1], MB_CUR_MAX) < 1) {
fprintf(stderr, "Could not convert %s\n", argv[1]);
return EXIT_FAILURE;
}
printf("U+%04X\n", c);
return EXIT_SUCCESS;
}
It successfully converts non-ASCII characters:
$ ./read-wchar ┆
U+2506
from ttyplot.
Yeah @edgar-bonet that sounds like the way to go! Let's draw straws to see who has to write the patch. I'll go first.
This is my straw: ~=====~
from ttyplot.
How would you draw vertical lines with this char *
then?
ttyplot@master uses mvvline()
for this. All the functions from the vline()
family take either a chtype
(an integer holding an ASCII character, with attributes in the upper bits) or a const cchar_t *
(which is based on wchar_t
). None of them takes a multibyte sequence as a char *
.
Maybe replacing mvvline()
with a loop of mvaddstr()
? It may be worth a try. If this can work with a non-wchar version of ncurses, this could be a win! Would you write that patch?
from ttyplot.
What OS and terminal is this on?
from ttyplot.
Hey, it's on Void Linux, Fish shell.
from ttyplot.
What is the terminal app, font and locale setting, LANG=
thanks
from ttyplot.
Alacritty, LiterationMono Nerd Font Mono from https://www.nerdfonts.com/, en_US.UTF-8
from ttyplot.
@edgar-bonet nice analysis! 👍
from ttyplot.
Yes, excellent find! Sadly a 100% correct solution will require something like iconv and a library dependency. A solution for the maybe most common case UTF-8 would require calling nl_langinfo
and a UTF-8 parser like this. If the locale is not UTF-8 ttyplot could print a warning and use the default char.
from ttyplot.
@MIvanchev: Here is mine ====
. OK, I'll write the patch...
from ttyplot.
You're too fast! I just had another idea that doesn't use wide strings and thus no change in dependencies. We could use mblen
to find out how many bytes the first character takes, copy them to a new buffer plus \0
and pass this to ncurses instead. So in essence
int char_len = mblen(argv[1], strlen(argv[1]));
assert(char_len < SIZE_MAX)
char *buf = malloc(char_len + 1);
strncpy(buf, argv[1], char_len + 1);
/* Use buf with ncurses. */
from ttyplot.
Let me see if I remember how programming works...
from ttyplot.
Scrape my idea, I tested extensively over the weekend and it doesn't really work, ncurses doesn't seem to support multi-byte chars through char.
from ttyplot.
@MIvanchev: In the mean time, multi-byte characters work file on the development
branch.
from ttyplot.
Yeah, I know, they have worked for a long time thanks to your effort :D I was just curious whether ncursesw is really necessary but it seems it really is ¯\(ツ)/¯
from ttyplot.
Closing as fixed by #99…
from ttyplot.
Related Issues (20)
- arguments broken HOT 22
- Please add a Fish shell completion file HOT 1
- [1.5.2] `ttyplot.c:129:9: runtime error: signed integer overflow: 0 - -2147483648 cannot be represented in type 'int'` HOT 5
- [1.5.2] `ttyplot -c X -2` fails to use ASCII character "X" for drawing? HOT 2
- (Plans and remaing todos for) Release 1.6.0 HOT 6
- ttyplot doesn't build (in general and on Void Linux) because of issues in the Makefile HOT 15
- Use of `M_PI` in `stresstest.c` is not C99 and unfortunately breaks compilation with musl libc HOT 5
- [1.6.1] Chart output corrupts after a few minutes on macOS? HOT 20
- Shortcut for live plot of memory usage? HOT 7
- [1.6.3] Pretends to be 1.6.2. Re-release as 1.6.4 after a fix? HOT 2
- [1.6.4] CI is broken on `master` HOT 3
- use hline() vline() instead of drawing by hand HOT 1
- better SIGWINCH handler HOT 2
- stdin closes on SIGWINCH on some systems HOT 3
- [1.5.1] Minimum window size assumptions can be violated? HOT 4
- error: format not a string literal and no format arguments HOT 6
- handle window resize on older systems HOT 2
- Average should be calculated from the available samples instead of the total number of potentially availalbe values HOT 4
- Release a new version and support -v option? HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ttyplot.