Is there a Hex package similar to Apple's NSAttributedString?

I’d like to be able to attach attributes to ranges within a string. Since I’m writing prototypes/spikes in Elixir for an eventual MacOS app, it’d be nice if the capabilities were similar to NSAttributedString - even better, a process that makes a mutable version of that (equivalent to NSMutableAttributedString). However, anything that saves me the trouble of dealing with all the fiddly cases around maintaining attributes as the underlying string changes is more than fine.

In one sense I’m not sure how to answer this question. The entire framing is that you want a thing that doesn’t look anything like an Elixir string in that it has attributes, and those attributes are mutable. Can you expand on the larger problem you are trying to solve, and elaborate on how this specific data structure helps accomplish a solution to that problem?

I’m not quite sure I understand the exact problem you are seeking to solve - but have a look at liveview native https://github.com/liveview-native/live_view_native - they are doing a bunch of stuff/abstractions around Swift (ui) code/modifiers etc. - so you might find inspiration there…

Consider a text editor, which manages a mutable string. Ranges of characters can be assigned particular attributes. Conceptually, every character in the string comes with a key/value map named “attributes”. Some keys are predefined, but apps can define their own custom attributes.

You can ask questions like:

  1. What attributes does the 34th Unicode character in the string have?
  2. What’s the largest contiguous Range of characters that all have the same attributes as character 34?
  3. Is the :italic attribute set for character 34?
  4. For what range around character 34 is the :italic attribute set?

I’m writing an app to explore coding in a style inspired by ecological and embodied cognition. Mainly for fun and to keep me off the streets.

2 Likes

In particular: the editing app is conceptualized as (for example) watching a paragraph the way a cat watches a mousehole. More specifically, there’ll be a process focused on a paragraph. I’m imagining implementing that as the text in the paragraph having a :watcher attribute whose value is a process. The stock NSMutableAttributedString code will handle things like adding and deleting characters while keeping the attributes correctly attached. Code wrapped around that will send a changed message to the watcher process (sort of like Observer), which is watching for certain sorts of changes.

For example, if I split a paragraph in two with two blank lines between the chunks, that’s a signal that I’m revising a paragraph. That’s one of the ways I customarily start revisions, and I want the app to asynchronously do certain things to help me out.

So the “paragraph-observer” process will be replaced by a different, “paragraph-editing-observer” process that is alert for different, editing-specific affordances. In particular, it will act to tag internal sentence fragments that will cause the editor display (in the MacOS version) to sweep them off to the side of main editor window. Because I think actions like that will save me hours of frustration at the cost of mere months of coding.

I cover this app in more detail in the last of five Oddly Influenced podcast episodes on ecological and embodied cognition. (Transcript available.)