|
1 | 1 | # [StyledStrings](@id stdlib-styledstrings) |
2 | 2 |
|
| 3 | +```@meta |
| 4 | +CurrentModule = StyledStrings |
| 5 | +DocTestSetup = quote |
| 6 | + using StyledStrings |
| 7 | +end |
| 8 | +``` |
| 9 | + |
3 | 10 | !!! note |
4 | 11 | The API for StyledStrings and AnnotatedStrings is considered experimental and is subject to change between |
5 | 12 | Julia versions. |
@@ -41,6 +48,52 @@ using StyledStrings |
41 | 48 | styled"{yellow:hello} {blue:there}" |
42 | 49 | ``` |
43 | 50 |
|
| 51 | +## [Annotated Strings](@id man-annotated-strings) |
| 52 | + |
| 53 | +It is sometimes useful to be able to hold metadata relating to regions of a |
| 54 | +string. A [`AnnotatedString`](@ref Base.AnnotatedString) wraps another string and |
| 55 | +allows for regions of it to be annotated with labelled values (`:label => value`). |
| 56 | +All generic string operations are applied to the underlying string. However, |
| 57 | +when possible, styling information is preserved. This means you can manipulate a |
| 58 | +[`AnnotatedString`](@ref Base.AnnotatedString) —taking substrings, padding them, |
| 59 | +concatenating them with other strings— and the metadata annotations will "come |
| 60 | +along for the ride". |
| 61 | + |
| 62 | +This string type is fundamental to the [StyledStrings stdlib](@ref |
| 63 | +stdlib-styledstrings), which uses `:face`-labelled annotations to hold styling |
| 64 | +information. |
| 65 | + |
| 66 | +When concatenating a [`AnnotatedString`](@ref Base.AnnotatedString), take care to use |
| 67 | +[`annotatedstring`](@ref StyledStrings.annotatedstring) instead of [`string`](@ref) if you want |
| 68 | +to keep the string annotations. |
| 69 | + |
| 70 | +```jldoctest |
| 71 | +julia> str = AnnotatedString("hello there", [(1:5, :word => :greeting), (7:11, :label => 1)]) |
| 72 | +"hello there" |
| 73 | +
|
| 74 | +julia> length(str) |
| 75 | +11 |
| 76 | +
|
| 77 | +julia> lpad(str, 14) |
| 78 | +" hello there" |
| 79 | +
|
| 80 | +julia> typeof(lpad(str, 7)) |
| 81 | +AnnotatedString{String} |
| 82 | +
|
| 83 | +julia> str2 = AnnotatedString(" julia", [(2:6, :face => :magenta)]) |
| 84 | +" julia" |
| 85 | +
|
| 86 | +julia> annotatedstring(str, str2) |
| 87 | +"hello there julia" |
| 88 | +
|
| 89 | +julia> str * str2 == annotatedstring(str, str2) # *-concatenation works |
| 90 | +true |
| 91 | +``` |
| 92 | + |
| 93 | +The annotations of a [`AnnotatedString`](@ref Base.AnnotatedString) can be accessed |
| 94 | +and modified via the [`annotations`](@ref StyledStrings.annotations) and |
| 95 | +[`annotate!`](@ref StyledStrings.annotate!) functions. |
| 96 | + |
44 | 97 | ## Styling via [`AnnotatedString`](@ref Base.AnnotatedString)s |
45 | 98 |
|
46 | 99 | ## [Faces](@id stdlib-styledstrings-faces) |
@@ -153,7 +206,7 @@ them to the properties list afterwards, or use the convenient [Styled String |
153 | 206 | literals](@ref stdlib-styledstring-literals). |
154 | 207 |
|
155 | 208 | ```@repl demo |
156 | | -str1 = Base.AnnotatedString("blue text", [(1:9, :face => :blue)]) |
| 209 | +str1 = AnnotatedString("blue text", [(1:9, :face => :blue)]) |
157 | 210 | str2 = styled"{blue:blue text}" |
158 | 211 | str1 == str2 |
159 | 212 | sprint(print, str1, context = :color => true) |
@@ -275,14 +328,17 @@ arbitrarily nest and overlap, \colorbox[HTML]{3a3a3a}{\color[HTML]{33d079}like |
275 | 328 |
|
276 | 329 | ## [API reference](@id stdlib-styledstrings-api) |
277 | 330 |
|
| 331 | + |
| 332 | +### Styling and Faces |
| 333 | + |
278 | 334 | ```@docs |
279 | 335 | StyledStrings.@styled_str |
280 | 336 | StyledStrings.styled |
281 | 337 | StyledStrings.Face |
282 | 338 | StyledStrings.addface! |
283 | 339 | StyledStrings.withfaces |
284 | 340 | StyledStrings.SimpleColor |
285 | | -Base.parse(::Type{StyledStrings.SimpleColor}, ::String) |
286 | | -Base.tryparse(::Type{StyledStrings.SimpleColor}, ::String) |
287 | | -Base.merge(::StyledStrings.Face, ::StyledStrings.Face) |
| 341 | +StyledStrings.parse(::Type{StyledStrings.SimpleColor}, ::String) |
| 342 | +StyledStrings.tryparse(::Type{StyledStrings.SimpleColor}, ::String) |
| 343 | +StyledStrings.merge(::StyledStrings.Face, ::StyledStrings.Face) |
288 | 344 | ``` |
0 commit comments