Skip to content

Commit 05a417d

Browse files
marcobacisgiacomocavalieri
authored andcommitted
add doc.zero_width_string function
* add doc.zero_width_string function * add doc.zero_width_string example test * update changelog
1 parent 314f5ec commit 05a417d

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## Unreleased
44

55
- Remove the `doc.to_string_builder` method.
6+
- The `doc` module gains the `zero_width_string` function
67

78
## v1.3.0 - 2023-01-03
89

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
version: 1.0.4
3+
title: zero width string 1
4+
---
5+
doc with zero width
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
version: 1.0.4
3+
title: zero width string 2
4+
---
5+
I'm a red, bold text

src/glam/doc.gleam

+27
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,33 @@ pub fn from_string(string: String) -> Document {
236236
Text(string, string.length(string))
237237
}
238238

239+
/// Turns a string into a document whose length is not taken into account when
240+
/// formatting it.
241+
///
242+
/// This kind of string can be used to render non-visible characters like ansi
243+
/// color codes.
244+
///
245+
/// ## Examples
246+
///
247+
/// ```gleam
248+
/// // Should break in two lines, but doesn't because of the zero_width_string
249+
/// // does not contribute to the total line length.
250+
/// [
251+
/// zero_width_string("\u{001b}[1;31m"),
252+
/// from_string("I'm a red"),
253+
/// break(", ", ","),
254+
/// from_string("bold text"),
255+
/// ]
256+
/// |> concat
257+
/// |> group
258+
/// |> to_string(20)
259+
/// // -> "\u{001b}[1;31mI'm a red, bold text"
260+
/// ```
261+
///
262+
pub fn zero_width_string(string: String) -> Document {
263+
Text(string, 0)
264+
}
265+
239266
/// Allows the pretty printer to break the `break` documents inside the given
240267
/// group.
241268
///

test/glam/doc_test.gleam

+28
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import gleam/list
22
import gleam/string
33
import gleeunit/should
44
import glam/doc
5+
import birdie
56

67
pub fn append_test() {
78
doc.from_string("foo")
@@ -200,3 +201,30 @@ pub fn flex_break_with_group_and_nesting_test() {
200201
doc.to_string(list, 1)
201202
|> should.equal("[\n 1,\n 2,\n 3,\n 4,\n 5,\n]")
202203
}
204+
205+
pub fn zero_width_string_test() {
206+
[
207+
doc.from_string("doc"),
208+
doc.break(" ", ""),
209+
doc.from_string("with"),
210+
doc.break(" ", ""),
211+
doc.zero_width_string("zero width"),
212+
]
213+
|> doc.concat
214+
|> doc.group
215+
|> doc.to_string(10)
216+
|> birdie.snap(title: "zero width string 1")
217+
}
218+
219+
pub fn zero_width_string_example_test() {
220+
[
221+
doc.zero_width_string("\u{001b}[1;31m"),
222+
doc.from_string("I'm a red"),
223+
doc.break(", ", ","),
224+
doc.from_string("bold text"),
225+
]
226+
|> doc.concat
227+
|> doc.group
228+
|> doc.to_string(20)
229+
|> birdie.snap(title: "zero width string 2")
230+
}

0 commit comments

Comments
 (0)