Skip to content

Issue/768/support markdown emoji #866

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

**emojified commit message**

![emojified-commit-message](assets/emojified-commit-message.png)

## Added
- added support for markdown emoji's in commits [[@andrewpollack](https://github.com/andrewpollack)] ([#768](https://github.com/extrawurst/gitui/issues/768))

## Fixed
- fix commit msg being broken inside tag list ([#871](https://github.com/extrawurst/gitui/issues/871))

Expand Down
35 changes: 35 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ easy-cast = "0.4"
bugreport = "0.4"
lazy_static = "1.4"
syntect = { version = "4.5", default-features = false, features = ["metadata", "default-fancy"]}
gh-emoji = "1.0.6"

[target.'cfg(all(target_family="unix",not(target_os="macos")))'.dependencies]
which = "4.1"
Expand Down
Binary file added assets/emojified-commit-message.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions src/components/commitlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ impl CommitList {
Cow::from(e.msg.as_str()),
theme.text(true, selected),
));

Spans::from(txt)
}

Expand Down
50 changes: 48 additions & 2 deletions src/components/utils/logitems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use asyncgit::sync::{CommitId, CommitInfo};
use chrono::{DateTime, Duration, Local, NaiveDateTime, Utc};
use std::slice::Iter;

use crate::components::utils::emojifi_string;

static SLICE_OFFSET_RELOAD_THRESHOLD: usize = 100;

pub struct LogEntry {
Expand All @@ -19,9 +21,15 @@ impl From<CommitInfo> for LogEntry {
NaiveDateTime::from_timestamp(c.time, 0),
Utc,
));

// Replace markdown emojis with Unicode equivalent
let author = c.author;
let mut msg = c.message;
emojifi_string(&mut msg);

Self {
author: c.author,
msg: c.message,
author,
msg,
time,
hash_short: c.id.get_short_string(),
id: c.id,
Expand Down Expand Up @@ -98,3 +106,41 @@ impl ItemBatch {
needs_data_bottom || needs_data_top
}
}

#[cfg(test)]
mod tests {
use super::*;

fn test_conversion(s: &str) -> String {
let mut s = s.to_string();
emojifi_string(&mut s);
s
}

#[test]
fn test_emojifi_string_conversion_cases() {
assert_eq!(
&test_conversion("It's :hammer: time!"),
"It's 🔨 time!"
);
assert_eq!(
&test_conversion(":red_circle::orange_circle::yellow_circle::green_circle::large_blue_circle::purple_circle:"),
"🔴🟠🟡🟢🔵🟣"
);
assert_eq!(
&test_conversion("It's raining :cat:s and :dog:s"),
"It's raining 🐱s and 🐶s"
);
assert_eq!(&test_conversion(":crab: rules!"), "🦀 rules!");
}

#[test]
fn test_emojifi_string_no_conversion_cases() {
assert_eq!(&test_conversion("123"), "123");
assert_eq!(
&test_conversion("This :should_not_convert:"),
"This :should_not_convert:"
);
assert_eq!(&test_conversion(":gopher:"), ":gopher:");
}
}
17 changes: 17 additions & 0 deletions src/components/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use chrono::{DateTime, Local, NaiveDateTime, Utc};
use lazy_static::lazy_static;
use std::borrow::Cow;
use unicode_width::UnicodeWidthStr;

pub mod filetree;
Expand Down Expand Up @@ -53,6 +55,21 @@ pub fn string_width_align(s: &str, width: usize) -> String {
}
}

lazy_static! {
static ref EMOJI_REPLACER: gh_emoji::Replacer =
gh_emoji::Replacer::new();
}

// Replace markdown emojis with Unicode equivalent
// :hammer: --> 🔨
#[inline]
pub fn emojifi_string(s: &mut String) {
let resulting_cow = EMOJI_REPLACER.replace_all(s);
if let Cow::Owned(altered_s) = resulting_cow {
*s = altered_s;
}
}

#[inline]
fn find_truncate_point(s: &str, chars: usize) -> usize {
s.chars().take(chars).map(char::len_utf8).sum()
Expand Down