Skip to content

Commit d3a6fdd

Browse files
Issue/768/support markdown emoji (#866)
1 parent fa1a1c8 commit d3a6fdd

File tree

7 files changed

+109
-2
lines changed

7 files changed

+109
-2
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
**emojified commit message**
11+
12+
![emojified-commit-message](assets/emojified-commit-message.png)
13+
14+
## Added
15+
- added support for markdown emoji's in commits [[@andrewpollack](https://github.com/andrewpollack)] ([#768](https://github.com/extrawurst/gitui/issues/768))
16+
1017
## Fixed
1118
- fix commit msg being broken inside tag list ([#871](https://github.com/extrawurst/gitui/issues/871))
1219

Cargo.lock

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ easy-cast = "0.4"
4747
bugreport = "0.4"
4848
lazy_static = "1.4"
4949
syntect = { version = "4.5", default-features = false, features = ["metadata", "default-fancy"]}
50+
gh-emoji = "1.0.6"
5051

5152
[target.'cfg(all(target_family="unix",not(target_os="macos")))'.dependencies]
5253
which = "4.1"

assets/emojified-commit-message.png

258 KB
Loading

src/components/commitlist.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ impl CommitList {
301301
Cow::from(e.msg.as_str()),
302302
theme.text(true, selected),
303303
));
304+
304305
Spans::from(txt)
305306
}
306307

src/components/utils/logitems.rs

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use asyncgit::sync::{CommitId, CommitInfo};
22
use chrono::{DateTime, Duration, Local, NaiveDateTime, Utc};
33
use std::slice::Iter;
44

5+
use crate::components::utils::emojifi_string;
6+
57
static SLICE_OFFSET_RELOAD_THRESHOLD: usize = 100;
68

79
pub struct LogEntry {
@@ -19,9 +21,15 @@ impl From<CommitInfo> for LogEntry {
1921
NaiveDateTime::from_timestamp(c.time, 0),
2022
Utc,
2123
));
24+
25+
// Replace markdown emojis with Unicode equivalent
26+
let author = c.author;
27+
let mut msg = c.message;
28+
emojifi_string(&mut msg);
29+
2230
Self {
23-
author: c.author,
24-
msg: c.message,
31+
author,
32+
msg,
2533
time,
2634
hash_short: c.id.get_short_string(),
2735
id: c.id,
@@ -98,3 +106,41 @@ impl ItemBatch {
98106
needs_data_bottom || needs_data_top
99107
}
100108
}
109+
110+
#[cfg(test)]
111+
mod tests {
112+
use super::*;
113+
114+
fn test_conversion(s: &str) -> String {
115+
let mut s = s.to_string();
116+
emojifi_string(&mut s);
117+
s
118+
}
119+
120+
#[test]
121+
fn test_emojifi_string_conversion_cases() {
122+
assert_eq!(
123+
&test_conversion("It's :hammer: time!"),
124+
"It's 🔨 time!"
125+
);
126+
assert_eq!(
127+
&test_conversion(":red_circle::orange_circle::yellow_circle::green_circle::large_blue_circle::purple_circle:"),
128+
"🔴🟠🟡🟢🔵🟣"
129+
);
130+
assert_eq!(
131+
&test_conversion("It's raining :cat:s and :dog:s"),
132+
"It's raining 🐱s and 🐶s"
133+
);
134+
assert_eq!(&test_conversion(":crab: rules!"), "🦀 rules!");
135+
}
136+
137+
#[test]
138+
fn test_emojifi_string_no_conversion_cases() {
139+
assert_eq!(&test_conversion("123"), "123");
140+
assert_eq!(
141+
&test_conversion("This :should_not_convert:"),
142+
"This :should_not_convert:"
143+
);
144+
assert_eq!(&test_conversion(":gopher:"), ":gopher:");
145+
}
146+
}

src/components/utils/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use chrono::{DateTime, Local, NaiveDateTime, Utc};
2+
use lazy_static::lazy_static;
3+
use std::borrow::Cow;
24
use unicode_width::UnicodeWidthStr;
35

46
pub mod filetree;
@@ -53,6 +55,21 @@ pub fn string_width_align(s: &str, width: usize) -> String {
5355
}
5456
}
5557

58+
lazy_static! {
59+
static ref EMOJI_REPLACER: gh_emoji::Replacer =
60+
gh_emoji::Replacer::new();
61+
}
62+
63+
// Replace markdown emojis with Unicode equivalent
64+
// :hammer: --> 🔨
65+
#[inline]
66+
pub fn emojifi_string(s: &mut String) {
67+
let resulting_cow = EMOJI_REPLACER.replace_all(s);
68+
if let Cow::Owned(altered_s) = resulting_cow {
69+
*s = altered_s;
70+
}
71+
}
72+
5673
#[inline]
5774
fn find_truncate_point(s: &str, chars: usize) -> usize {
5875
s.chars().take(chars).map(char::len_utf8).sum()

0 commit comments

Comments
 (0)