astoff1 · 2022-07-28 15:40
Every so often I need to do some type of data processing where strings (mutable or not) with text properties, as in Emacs, would be the perfect tool.
So, are there packages in other programming languages that provide a "string with text properties" data structure? I'd be specifically interested in Python, which is what I end up having to use for work.
Hagge5 · 2022-07-28 18:37
https://en.m.wikipedia.org/wiki/ANSI_escape_code is a thing, and similar stuff exists for programs outside of shells, on a case to case basis.
astoff1 · 2022-07-28 20:11
ANSI escape sequences are actually the perfect example of the kind of trouble I want to _avoid_. You can't just slice and splice them with impunity. (Consider what happens if you have some red text RRRR, white text WW, and stick the one inside the other as in RRWWRR. You won't get the flag of Peru, sadly.)
What I'm looking for is something on which I can operate like a string, but where each character has some additional information (say a color) attached to it and the additional information is carried around automatically.
loopsdeer · 2022-07-28 20:27
You can do that, just not naively (eg simple text concatenation). You just have to describe an algebra of your special strings (eg an operator/function that does the "splice this inside that" operation) then implement it to preserve the qualities of your algebra (eg the colors of the output make sense given the input colors). I'm not saying ANSI is what you want, but it's absolutely achievable, probably less work than you're imagining, and a strong, historied, portable standard.
That's what Emacs does with its text properties, with something not so different from ANSI. So this does exist.
I'm confused by how Python and Emacs are related in this project. What do you want to happen in Emacs? What do you want to do in Python?
kovrik · 2022-07-28 20:38
Not sure I understand what you are asking about....
But say in Java there's a String class. One can easily create another class like StringWithProperties which would wrap around String (cannot extend from String because that Java class is final for security reasons) and add required Properties there or literally anything you want:
`class StringWithProperties {`
`private final String string;`
`private final StringProperties properties;`
`}`
And that's basically it. Same thing could easily be implemented in pretty much any other programming language. So I guess that's why it's usually not included?
eploko · 2022-07-28 23:51
Apple’s Cocoa has NSAttributedString: https://developer.apple.com/documentation/foundation/nsattributedstring
Phil-Hudson · 2022-07-29 07:46
Available on other platforms via GNUStep:
http://www.gnustep.org/resources/documentation/Developer/Base/Reference/NSAttributedString.html
Also NSMutableAttributedString, I notice.
deaddyfreddy · 2022-08-12 00:53
Clojure has https://clojure.org/reference/metadata
astoff1 · 2022-08-12 06:39
But that's metadata you could attach to an entire string as opposed to individual characters (are carried around automatically as you operate on strings), right?
deaddyfreddy · 2022-08-12 08:51
correct