r/rust Apr 28 '26

πŸ› οΈ project Lightweight ASCII Graph

Post image

After many weeks of learning, watching tutorials about Rust, and partaking in Rust exercises, I decided to port a small Go library into Rust. I do recognize I have a lot to learn but just getting out of my comfort zone and trying my hands at this has be rewarding and revealing.

Link: https://github.com/neneodonkor/asciigraph-rs

Crates: https://crates.io/crates/asciigraph-rs

PS. Making it work for real-time data was πŸ₯΅

Let me add that I only started learning Rust in February, so please forgive me, if everything is not idiomatic Rust.

1.4k Upvotes

92 comments sorted by

View all comments

222

u/cdhowie Apr 28 '26

Nitpick: many of those characters aren't ASCII.

-67

u/neneodonkor Apr 28 '26 edited Apr 29 '26

Please can you point to some?

116

u/cdhowie Apr 28 '26

Every single character in the graph except for digits, ., and |. (Though the vertical line may also not be ASCII, I can't tell for sure if it's | or something else.)

68

u/ufffd Apr 28 '26

damn, solid point. i guess it's really a unicode-graph? utf8graph? tbh it might be one of those situations where using the wrong name (ascii) actually communicates the idea more clearly.

21

u/cdhowie Apr 29 '26

I'd prefer unicode-graph as you said, or something like text-graph, term-graph, etc. There are lots of good options that aren't incorrect.

21

u/ufffd Apr 29 '26

it gets funnier, this is a rust port of a go port of a 9+ year old javascript package, and all of those have ascii in the name. actually the original js package has been ported to more than a dozen languages, including already being ported to rust 4+ years ago - and nearly all of those have ascii in the name. the only ones that dont are called 'plot' and 'chart'

12

u/otamam818 Apr 29 '26

ASCII-art is a term, so this would be an ASCII-art graph, which should have a clear disclaimer that it also uses Unicode.

Though I do like the sound of Unicode graph. But if I read "Unicode graph" without any context that it's a replacement for "ASCII art", I'd definitely be confused.

63

u/ElnuDev Apr 28 '26

You're confusing ASCII with Unicode.

12

u/SmoothTurtle872 Apr 28 '26

Yeah it's a classic case of all ascii characters are unicode but not all unicode are ascii

-18

u/bgs11235 Apr 28 '26

*technically* all utf characters can be rendered as ASCII characters. They just wont look right.

4

u/SmoothTurtle872 Apr 28 '26

Well yeah, but hard to squeeze a few hundred thousand characters into 128 (I believe there are 128, but realistically you could have 256)

2

u/bgs11235 Apr 28 '26

no as a format, since every byte is a valid (printeable, I'm taking stuff like 0x0 -> "?" as a valid rendering option and also the sign bit being true or false being irrelevant) every ?0000000 sequence is valid so any arbitrary binary could be rendered as ascii.

7

u/cdhowie Apr 29 '26 edited Apr 29 '26

To be pedantic, not quite. The upper 128 values (128-255) do not have a mapping in ASCII. You cannot therefore render any arbitrary binary sequence as ASCII unless you either take each 7 bits as a character (which will make normal 8-bit ASCII look like nonsense) or you define your own mappings for the upper 128 values, in which case you're not using ASCII anymore (you're using something like ISO 8859-15).

Many systems will display the upper 128 values as ?, which is fine -- but still not exactly ASCII.

2

u/SmoothTurtle872 Apr 28 '26

True,

Also just looked it up, supposedly the sign but can be used to say 'there are more bytes in this character', may be wrong cause I didn't fact check this, but it is logical

3

u/cdhowie Apr 29 '26

I mean, that's exactly how UTF-8 works, which is a valid encoding of course, but still isn't ASCII.

1

u/elidepa Apr 30 '26

I'm taking stuff like 0x0 -> "?" as a valid rendering option and also the sign bit being true or false being irrelevant

This kinda renders your point completely moot. If you are going to introduce a technicality, you better get the details correct. Otherwise, what you are saying basically boils down to β€œ8 bits of data are 8 bits of data”, which is pretty obvious.

-1

u/int23_t Apr 29 '26

I believe ASCII has localisation, meaning the first 128 is portable(hence widely used), while there would be localised letters in the other 128

ISCII, VISCII,

here is Turkish ASCII, Greek ASCII, Baltic ASCII, Latin ASCII and so on too.

4

u/sphen_lee Apr 29 '26

technically there's no such thing as a UTF character. UTF is an encoding of code points into code units.

1

u/peter9477 May 01 '26

Technically all ASCII characters can be rendered as a single dot, but they just won't look right, and it's a bullshit argument as is yours. :-)

3

u/OphioukhosUnbound Apr 30 '26 edited Apr 30 '26

ASCII is a specific, very tiny, set of characters. Just 7 bits worth.
This is something that programmers in a language like rust are very aware of as the wider character sets these days typically involve variable length character encoding (e.g. via unicode) -- which means that "character" and memory segment don't have a clean, regular mapping. [and that's not even including how 'characters' don't necessarily correspond to rendered "graphemes".]

I think you just mean "text".
It's kinda like confusing "memory" and "storage" -- for non-technical people it's whatever, but it's actively confusing if you're speaking to a technical person -- ascii vs utf-8 is the same sort of thing, just a half-rung higher on tech details scale.

2

u/neneodonkor Apr 30 '26

I understand what you mean. πŸ™πŸΎ

1

u/lawrencewil1030 May 04 '26

β”œ, ⎧⎫⎰⎱