Skip to content

Commit 70e6744

Browse files
committed
Updates.
1 parent 4ee490d commit 70e6744

13 files changed

Lines changed: 196 additions & 32 deletions

File tree

.github/workflows/build-manual.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: manual
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches:
7+
- main
8+
- release/*
9+
pull_request:
10+
11+
permissions:
12+
contents: read
13+
pages: write
14+
id-token: write
15+
16+
concurrency:
17+
group: pages
18+
cancel-in-progress: false
19+
20+
jobs:
21+
build:
22+
name: Build manual
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: Checkout sources
26+
uses: actions/checkout@v6
27+
with:
28+
fetch-depth: 0
29+
30+
- name: Install latest mdBook and mdbook-yapp
31+
run: |
32+
tag_mdbook=$(curl -sSL 'https://api.github.com/repos/rust-lang/mdbook/releases/latest'| jq -r '.tag_name')
33+
url_mdbook="https://github.com/rust-lang/mdBook/releases/download/${tag_mdbook}/mdbook-${tag_mdbook}-x86_64-unknown-linux-gnu.tar.gz"
34+
tag_mdbook_yapp=$(curl -sSL 'https://api.github.com/repos/EngosSoftware/mdbook-yapp/releases/latest' | jq -r '.tag_name')
35+
url_mdbook_yapp="https://github.com/EngosSoftware/mdbook-yapp/releases/download/${tag_mdbook_yapp}/mdbook-yapp-${tag_mdbook_yapp}-x86_64-unknown-linux-gnu.tar.gz"
36+
mkdir mdbook
37+
curl -sSL $url_mdbook | tar -xz --directory=./mdbook
38+
curl -sSL $url_mdbook_yapp | tar -xz --directory=./mdbook
39+
echo `pwd`/mdbook >> $GITHUB_PATH
40+
41+
- name: Build manual
42+
run: cd manual && mdbook build
43+
44+
- name: Setup Pages
45+
uses: actions/configure-pages@v6
46+
47+
- name: Upload artifact
48+
uses: actions/upload-pages-artifact@v5
49+
with:
50+
path: manual/book
51+
52+
deploy:
53+
name: Deploy manual
54+
environment:
55+
name: github-pages
56+
url: ${{ steps.deployment.outputs.page_url }}
57+
needs: build
58+
runs-on: ubuntu-latest
59+
steps:
60+
- name: Deploy to GitHub Pages
61+
id: deployment
62+
uses: actions/deploy-pages@v5

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
[package]
22
name = "antex"
3-
version = "0.2.2"
3+
version = "0.2.3"
44
authors = ["Dariusz Depta <depta@engos.de>"]
55
description = "Styled text and tree in terminal"
66
documentation = "https://docs.rs/antex"
77
repository = "https://github.com/EngosSoftware/antex.git"
88
keywords = ["ansi", "ascii", "text", "tree", "styled"]
9+
homepage = "https://engossoftware.github.io/antex/"
910
categories = ["text-processing", "visualization", "encoding"]
1011
license = "MIT OR Apache-2.0"
1112
edition = "2024"

src/text.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ pub trait StyledText: Sized {
3838
/// Fills the content with the given character until the given width is reached.
3939
fn fill(self, ch: char, width: usize) -> Self;
4040
/// Adds new content based on the condition.
41-
fn choose<T: Display>(self, condition: bool, when_true: T, when_false: T) -> Self;
41+
fn choose<T: Display, F: Display>(self, condition: bool, when_true: T, when_false: F) -> Self;
42+
/// Adds new content based on the first matching condition.
43+
fn matches<C: IntoIterator<Item = (bool, impl Display)>>(self, conditions: C) -> Self;
4244
/// Adds indented content.
4345
fn indent<T: Display>(self, indent: usize, s: T) -> Self;
4446
/// Style text as bold.
@@ -237,6 +239,12 @@ pub struct Text {
237239
content: Content,
238240
}
239241

242+
impl AsRef<Text> for Text {
243+
fn as_ref(&self) -> &Text {
244+
self
245+
}
246+
}
247+
240248
impl Display for Text {
241249
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
242250
if let Some(width) = f.width() {
@@ -317,6 +325,14 @@ impl Text {
317325
pub fn count(&self) -> usize {
318326
self.chars().count()
319327
}
328+
329+
/// Returns text containing only characters,
330+
/// all control sequences (if present) are omitted.
331+
///
332+
/// It is an equivalent of calling `chars().collect::<String>()`.
333+
pub fn characters(&self) -> String {
334+
self.content.chars().collect()
335+
}
320336
}
321337

322338
impl StyledText for Text {
@@ -385,8 +401,17 @@ impl StyledText for Text {
385401
self
386402
}
387403

388-
fn choose<T: Display>(self, condition: bool, when_true: T, when_false: T) -> Self {
389-
self.s(if condition { when_true } else { when_false })
404+
fn choose<T: Display, F: Display>(self, condition: bool, when_true: T, when_false: F) -> Self {
405+
self.s(if condition { when_true.to_string() } else { when_false.to_string() })
406+
}
407+
408+
fn matches<C: IntoIterator<Item = (bool, impl Display)>>(self, conditions: C) -> Self {
409+
for (condition, content) in conditions.into_iter() {
410+
if condition {
411+
return self.s(content);
412+
}
413+
}
414+
self
390415
}
391416

392417
fn indent<T: Display>(self, indent: usize, s: T) -> Self {

src/tree.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,6 @@ impl StyledText for LeafLineBuilder {
161161
self
162162
}
163163

164-
fn indent<T: Display>(self, indent: usize, s: T) -> Self {
165-
self.pad(' ', indent, s)
166-
}
167-
168164
fn align_left<T: Display>(self, s: T, width: usize) -> Self {
169165
self.pad_right(' ', s, width)
170166
}
@@ -197,11 +193,25 @@ impl StyledText for LeafLineBuilder {
197193
self
198194
}
199195

200-
fn choose<T: Display>(mut self, condition: bool, when_true: T, when_false: T) -> Self {
196+
fn fill(mut self, ch: char, width: usize) -> Self {
197+
self.text = self.text.fill(ch, width);
198+
self
199+
}
200+
201+
fn choose<T: Display, F: Display>(mut self, condition: bool, when_true: T, when_false: F) -> Self {
201202
self.text = self.text.choose(condition, when_true, when_false);
202203
self
203204
}
204205

206+
fn matches<C: IntoIterator<Item = (bool, impl Display)>>(mut self, conditions: C) -> Self {
207+
self.text = self.text.matches(conditions);
208+
self
209+
}
210+
211+
fn indent<T: Display>(self, indent: usize, s: T) -> Self {
212+
self.pad(' ', indent, s)
213+
}
214+
205215
fn bold(mut self) -> Self {
206216
self.text = self.text.bold();
207217
self
@@ -436,11 +446,6 @@ impl StyledText for LeafLineBuilder {
436446
self.text = self.text.bg_color_rgb(c);
437447
self
438448
}
439-
440-
fn fill(mut self, ch: char, width: usize) -> Self {
441-
self.text = self.text.fill(ch, width);
442-
self
443-
}
444449
}
445450

446451
/// Builder for [TreeNode::Node].
@@ -543,10 +548,6 @@ impl StyledText for NodeLineBuilder {
543548
self
544549
}
545550

546-
fn indent<T: Display>(self, indent: usize, s: T) -> Self {
547-
self.pad(' ', indent, s)
548-
}
549-
550551
fn align_left<T: Display>(self, s: T, width: usize) -> Self {
551552
self.pad_right(' ', s, width)
552553
}
@@ -579,11 +580,25 @@ impl StyledText for NodeLineBuilder {
579580
self
580581
}
581582

582-
fn choose<T: Display>(mut self, condition: bool, when_true: T, when_false: T) -> Self {
583+
fn fill(mut self, ch: char, width: usize) -> Self {
584+
self.text = self.text.fill(ch, width);
585+
self
586+
}
587+
588+
fn choose<T: Display, F: Display>(mut self, condition: bool, when_true: T, when_false: F) -> Self {
583589
self.text = self.text.choose(condition, when_true, when_false);
584590
self
585591
}
586592

593+
fn matches<C: IntoIterator<Item = (bool, impl Display)>>(mut self, conditions: C) -> Self {
594+
self.text = self.text.matches(conditions);
595+
self
596+
}
597+
598+
fn indent<T: Display>(self, indent: usize, s: T) -> Self {
599+
self.pad(' ', indent, s)
600+
}
601+
587602
fn bold(mut self) -> Self {
588603
self.text = self.text.bold();
589604
self
@@ -818,9 +833,4 @@ impl StyledText for NodeLineBuilder {
818833
self.text = self.text.bg_color_rgb(c);
819834
self
820835
}
821-
822-
fn fill(mut self, ch: char, width: usize) -> Self {
823-
self.text = self.text.fill(ch, width);
824-
self
825-
}
826836
}

tests/test_text/characters.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,32 @@ fn any_char_should_work() {
55
let text = Text::default().s("Hello").s('^').s("world!");
66
assert_eq!("Hello^world!", text.to_string());
77
assert_eq!("Hello^world!", text.chars().collect::<String>());
8+
assert_eq!("Hello^world!", text.characters());
9+
assert_eq!(12, text.count());
810
}
911

1012
#[test]
1113
fn unicode_char_should_work() {
1214
let text = Text::default().s("Hello").s('☺').s("world!");
1315
assert_eq!("Hello☺world!", text.to_string());
1416
assert_eq!("Hello☺world!", text.chars().collect::<String>());
17+
assert_eq!("Hello☺world!", text.characters());
18+
assert_eq!(12, text.count());
1519
}
1620

1721
#[test]
1822
fn repeat_should_work() {
1923
let text = Text::default().s("Hello").repeat('☺', 5).s("world!");
2024
assert_eq!("Hello☺☺☺☺☺world!", text.to_string());
2125
assert_eq!("Hello☺☺☺☺☺world!", text.chars().collect::<String>());
26+
assert_eq!("Hello☺☺☺☺☺world!", text.characters());
27+
assert_eq!(16, text.count());
2228
}
2329

2430
#[test]
2531
fn colored_repeat_should_work() {
2632
let text = Text::default().blue().s("Hello").reset().green().repeat('☺', 5).reset().red().s("world!").reset();
2733
assert_eq!("Hello☺☺☺☺☺world!", text.chars().collect::<String>());
34+
assert_eq!("Hello☺☺☺☺☺world!", text.characters());
2835
assert_eq!(16, text.count());
2936
}
30-
31-
#[test]
32-
fn plural_should_work() {
33-
assert_eq!("Hello world!", Text::default().s("Hello ").plural("world", 1).s('!').to_string());
34-
assert_eq!("Hello worlds!", Text::default().s("Hello ").plural("world", 2).s('!').to_string());
35-
}

tests/test_text/choose.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ fn if_else_should_work() {
55
assert_eq!("1", format!("{}", never().choose(true, "1", "0")));
66
assert_eq!("0", format!("{}", never().choose(false, "1", "0")));
77
}
8+
9+
#[test]
10+
fn if_else_with_different_types_should_work() {
11+
assert_eq!("1", format!("{}", never().choose(true, 1, "0")));
12+
assert_eq!("0", format!("{}", never().choose(false, '1', 0)));
13+
}

tests/test_text/matches.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use antex::{StyledText, never};
2+
3+
#[test]
4+
fn matches_should_work() {
5+
assert_eq!("a", format!("{}", never().matches([(true, "a"), (true, "b"), (true, "c")])));
6+
assert_eq!("a", format!("{}", never().matches([(true, "a"), (true, "b"), (false, "c")])));
7+
assert_eq!("a", format!("{}", never().matches([(true, "a"), (false, "b"), (true, "c")])));
8+
assert_eq!("a", format!("{}", never().matches([(true, "a"), (false, "b"), (false, "c")])));
9+
assert_eq!("b", format!("{}", never().matches([(false, "a"), (true, "b"), (true, "c")])));
10+
assert_eq!("b", format!("{}", never().matches([(false, "a"), (true, "b"), (false, "c")])));
11+
assert_eq!("c", format!("{}", never().matches([(false, "a"), (false, "b"), (true, "c")])));
12+
assert_eq!("", format!("{}", never().matches([(false, "a"), (false, "b"), (false, "c")])));
13+
}

tests/test_text/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ mod colors;
88
mod filling;
99
mod format;
1010
mod formatting;
11+
mod matches;
1112
mod padding;
13+
mod plural;
1214
mod writer;
1315

1416
#[test]
@@ -106,3 +108,19 @@ fn text_coloured_from_cm_on_should_work() {
106108
assert_eq!("\x1b[33mHello", text.to_string());
107109
assert_eq!("\x1b[33mHello", format!("{}", text));
108110
}
111+
112+
#[test]
113+
fn text_as_ref_should_work() {
114+
let t1 = Text::default().yellow().s("Hello").reset();
115+
let t2 = Text::default().red().s("Hello").reset();
116+
117+
fn a(t: impl AsRef<Text>) -> String {
118+
t.as_ref().characters()
119+
}
120+
121+
fn b(t: impl AsRef<Text>) -> String {
122+
t.as_ref().characters()
123+
}
124+
125+
assert_eq!(a(t1), b(t2));
126+
}

tests/test_text/plural.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
use antex::{StyledText, Text};
2+
3+
#[test]
4+
fn plural_should_work() {
5+
assert_eq!("Hello world!", Text::default().s("Hello ").plural("world", 1).s('!').to_string());
6+
assert_eq!("Hello worlds!", Text::default().s("Hello ").plural("world", 2).s('!').to_string());
7+
}

0 commit comments

Comments
 (0)