|
1 | 1 | # AttributedString
|
| 2 | +[](https://github.com/instruct-rb/attributed-string/actions/workflows/ruby.yml) |
2 | 3 |
|
3 | 4 | An attributed string implementation for Ruby.
|
4 | 5 |
|
5 |
| -An attributed string contains key-value pairs known as attributes that specify additional information related to ranges of characters within the string. Attributed strings support any key-value pair, but are often used for: |
| 6 | +-- |
| 7 | +🚧 This gem is in development, and its behaviour might still change before 1.0 🚧 |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | + |
| 12 | +An attributed string contains key-value pairs known as attributes that specify additional information related to ranges of characters within the string. Attributed strings support any key-value pair, but are often used for: |
6 | 13 |
|
7 | 14 | - Rendering attributes such as font, color, and other details.
|
8 |
| -- Attributes for inline-attachments such as images, videos, files, etc. |
| 15 | +- Attributes for inline-attachments such as images, videos, files, etc. |
9 | 16 | - Semantic attributes such as link URLs or tool-tip information
|
10 |
| -- Language attributes to support automatic gender agreement or verb agreement. |
| 17 | +- Language attributes to support automatic gender agreement or verb agreement. |
11 | 18 | - Accessibility attributes that provide information for assistive technologies
|
12 | 19 | - Custom attributes you define
|
13 | 20 |
|
14 |
| -You will typically need to create a presenter for an attributed string, as the default shows no attribute information and inspect shows all attributes. |
| 21 | +You will typically need to create a presenter for an attributed string, as the default shows no attribute information and inspect shows all attributes. |
| 22 | + |
| 23 | +This gem is inspired by Apple's [NSAttributedString](https://developer.apple.com/documentation/foundation/nsattributedstring). |
15 | 24 |
|
16 | 25 | ## Installation
|
17 | 26 |
|
18 | 27 | Add this line to your application's Gemfile:
|
19 | 28 |
|
20 | 29 | ```ruby
|
21 |
| - gem 'attributed-string', github: 'instruct-rb/attributed-string', branch: 'main' |
| 30 | + gem 'attributed-string' |
22 | 31 | ```
|
23 | 32 |
|
24 | 33 |
|
25 | 34 | ## Usage
|
26 | 35 |
|
27 |
| -🚧 This gem is a work in progress and the API may change before 1.0. |
28 | 36 |
|
29 | 37 | ```ruby
|
30 | 38 | using AttributedString::Refinements
|
31 | 39 | hello_world1 = AttributedString.new('Hello, World!', color: :red, font: 'Helvetica' )
|
32 | 40 | hello_world2 = 'Hello, World!'.to_attr_s(color: :red, font: 'Helvetica')
|
33 |
| - puts hello_world1 == hello_world2 # true |
| 41 | + puts hello_world1 == hello_world2 |
| 42 | + # => true |
| 43 | + |
| 44 | + hello_world1.add_attrs(0..4, color: :blue) |
| 45 | + hello_world1 += "!!" |
| 46 | + puts hello_world1.inspect |
| 47 | + # => { color: blue, font: "Helvetica" }Hello{ color: red}, World!{ -color, -font }!! |
| 48 | + |
| 49 | + puts hello_world1.attrs_at(0) |
| 50 | + # => { color: blue, font: "Helvetica" } |
34 | 51 | ```
|
35 | 52 |
|
36 | 53 | ## Attachments
|
37 | 54 |
|
38 | 55 | ```ruby
|
39 | 56 | hello_world = AttributedString.new('Hello, World!')
|
| 57 | + |
40 | 58 | hello_world.add_attachment("any ruby object", position: string.length)
|
41 |
| - puts hello_world # => "Hello, World! " |
| 59 | + |
| 60 | + # Adding an attachment inserts the Object Replacement Character into the |
| 61 | + # string, in most fonts it will show as a space |
| 62 | + puts hello_world |
| 63 | + # => "Hello, World! " |
42 | 64 | puts hello_world.attachments # => ["any ruby object"]
|
| 65 | + |
| 66 | + # Deleting the character from the string removes the attachment |
43 | 67 | hello_world[-1..-1] = ''
|
44 | 68 | puts hello_world.attachments # => []
|
45 | 69 | ```
|
| 70 | + |
| 71 | +## More Examples |
| 72 | +See the [test suite](./test). |
| 73 | + |
| 74 | +## Used By |
| 75 | +- [Instruct](https://github.com/instruct-rb/instruct) - Instruct LLMs to do what you want in Ruby |
0 commit comments