Skip to content

Commit

Permalink
Merge pull request #452 from squidowl/feat/underline-strikethrough
Browse files Browse the repository at this point in the history
Add underline and strikethrough support
  • Loading branch information
tarkah authored Jul 29, 2024
2 parents eb19c0c + 45eb724 commit 6c8dc58
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 54 deletions.
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ windows_exe_info = "0.4"
members = ["data", "ipc", "irc", "irc/proto"]

[patch.crates-io]
iced = { git = "https://github.com/tarkah/iced", rev = "1feb29ab4badb35a57ff5c612f25009e8e175766" }
iced_core = { git = "https://github.com/tarkah/iced", rev = "1feb29ab4badb35a57ff5c612f25009e8e175766" }
iced = { git = "https://github.com/iced-rs/iced", rev = "6734d183594ebf89b8e6c030ea69d53ecb6b72db" }
iced_core = { git = "https://github.com/iced-rs/iced", rev = "6734d183594ebf89b8e6c030ea69d53ecb6b72db" }
12 changes: 7 additions & 5 deletions book/src/guides/text-formatting.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Below is a table with the supported text attributes.
| _Italics_ | `_italic text_` | `$iitalic text$i` |
| **Bold** | `__bold text__` | `$bbold text$b` |
| **_Italic and Bold_** | `___italic and bold___` | `$b$iitalic and bold$i$b` |
| ~~Strikethrough~~ | `~~strikethrough~~` | `$sstrikethrough$s` |
| Underline | - | `$uunderline$u` |
| Code | `` `code` `` | `$mcode$m` |
| Spoiler | `\|\|spoiler\|\|` | - |

Expand All @@ -20,8 +22,9 @@ Example
/format __this is bold__ $iand this is italic$i
```

Will render the following:
> __this is bold__ _and this is italic_
Will render the following:

> **this is bold** _and this is italic_
## Color

Expand Down Expand Up @@ -50,7 +53,7 @@ Colors
<span style="display:inline-block;width:12px;height:12px;background-color:#0000fc;"></span> - 12 - lightblue
<span style="display:inline-block;width:12px;height:12px;background-color:#ff00ff;"></span> - 13 - pink
<span style="display:inline-block;width:12px;height:12px;background-color:#7f7f7f;"></span> - 14 - grey
<span style="display:inline-block;width:12px;height:12px;background-color:#d2d2d2;"></span> - 15 - lightgrey
<span style="display:inline-block;width:12px;height:12px;background-color:#d2d2d2;"></span> - 15 - lightgrey

Example

Expand All @@ -59,13 +62,12 @@ Example
/format $c04,09foobar$c
```

Will both render the following:
Will both render the following:

<span style="display: inline-block; background-color: #00fc00; color: #ff0000;">
foobar
</span>


## Configuration

By default, Halloy will only format text when using the `/format` command. This, however, can be changed with the `auto_format` configuration option:
Expand Down
21 changes: 21 additions & 0 deletions data/src/message/formatting/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ fn markdown<'a>(
let italic = alt((relaxed_run('*', 1), strict_run('_', 1)));
let bold = alt((relaxed_run('*', 2), strict_run('_', 2)));
let italic_bold = alt((relaxed_run('*', 3), strict_run('_', 3)));
let strikethrough = relaxed_run('~', 2);
let spoiler = relaxed_run('|', 2);
let code = map(
alt((
Expand All @@ -232,6 +233,7 @@ fn markdown<'a>(
map(italic_bold, Markdown::ItalicBold),
map(bold, Markdown::Bold),
map(italic, Markdown::Italic),
map(strikethrough, Markdown::Strikethrough),
map(spoiler, Markdown::Spoiler),
map(code, Markdown::Code),
))
Expand Down Expand Up @@ -282,6 +284,8 @@ fn dollar(input: &str) -> IResult<&str, Dollar> {
map(tag("$b"), |_| Dollar::Bold),
map(tag("$i"), |_| Dollar::Italics),
map(tag("$m"), |_| Dollar::Monospace),
map(tag("$s"), |_| Dollar::Strikethrough),
map(tag("$u"), |_| Dollar::Underline),
map(tag("$r"), |_| Dollar::Reset),
map(start_color, |(fg, bg)| Dollar::StartColor(fg, bg)),
// No valid colors after code == end
Expand Down Expand Up @@ -346,6 +350,14 @@ impl Token {
}
out.push(c);
}
Markdown::Strikethrough(tokens) => {
let m = Modifier::Strikethrough.char();
out.push(m);
for token in tokens {
token.encode(out);
}
out.push(m);
}
},
Token::Dollar(dollar) => match dollar {
Dollar::Bold => {
Expand All @@ -357,6 +369,12 @@ impl Token {
Dollar::Monospace => {
out.push(Modifier::Monospace.char());
}
Dollar::Strikethrough => {
out.push(Modifier::Strikethrough.char());
}
Dollar::Underline => {
out.push(Modifier::Underline.char());
}
Dollar::Reset => {
out.push(Modifier::Reset.char());
}
Expand All @@ -383,6 +401,7 @@ enum Markdown {
Bold(Vec<Token>),
Italic(Vec<Token>),
ItalicBold(Vec<Token>),
Strikethrough(Vec<Token>),
Code(Vec<Token>),
Spoiler(Vec<Token>),
}
Expand All @@ -392,6 +411,8 @@ enum Dollar {
Bold,
Italics,
Monospace,
Strikethrough,
Underline,
Reset,
StartColor(Color, Option<Color>),
EndColor,
Expand Down
3 changes: 3 additions & 0 deletions src/buffer/input_view/format_tooltip.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ __bold__
___italic & bold___
`code`
||spoiler||
~~strikethrough~~

Toggles:
$b - bold
$i - italic
$m - monospace
$s - strikethrough
$u - underline
$r - reset

Color:
Expand Down
26 changes: 12 additions & 14 deletions src/widget.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![allow(dead_code)]
use data::message;
use iced::advanced::text::Background;
use iced::border;
use iced::widget::span;

Expand Down Expand Up @@ -62,23 +61,22 @@ pub fn message_content<'a, M: 'a>(
.color_maybe(
formatting
.fg
.and_then(|color| color.into_iced(theme.colors()))
.or_else(|| {
formatting.monospace.then_some(theme.colors().error.darker)
}),
.and_then(|color| color.into_iced(theme.colors())),
)
.background_maybe(
formatting
.bg
.and_then(|color| color.into_iced(theme.colors()))
.map(Background::from)
.or_else(|| {
formatting.monospace.then_some(Background {
color: theme.colors().background.lighter,
border: border::rounded(3),
})
}),
);
.and_then(|color| color.into_iced(theme.colors())),
)
.underline(formatting.underline)
.strikethrough(formatting.strikethrough);

if formatting.monospace {
span = span
.color(theme.colors().error.darker)
.background(theme.colors().background.lighter)
.border(border::rounded(3));
}

match (formatting.bold, formatting.italics) {
(true, true) => {
Expand Down
Loading

0 comments on commit 6c8dc58

Please sign in to comment.