Skip to content
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

Add support for long emoji names between colons (so copy paste from slack can work) #7

Open
wants to merge 1 commit into
base: need_top
Choose a base branch
from
Open
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
31 changes: 29 additions & 2 deletions src/bottom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,22 @@ pub fn encode_string(input: &dyn AsRef<str>) -> String {
input.as_ref().bytes().map(encode_byte).collect::<String>()
}

pub fn decode_string(input: &dyn AsRef<str>) -> Result<String, TranslationError> {
let input = input.as_ref();
pub fn delongate(input: &str) -> String {
return input
.replace(":sparkling_heart:", "💖")
.replace(":pleading_face:", "🥺")
.replace(":point_left:", "👈")
.replace(":point_right:", "👉")
.replace(":sparkles:", "✨");
}

pub fn decode_string_long<'a>(input: &dyn AsRef<str>, do_delongate: bool) -> Result<String, TranslationError> {
let mut input = input.as_ref();
let new_input;
if do_delongate{
new_input = delongate(input);
input = new_input.as_ref();
}
let result = {
// Older versions used a ZWSP as a character separator, instead of `👉👈`.
let split_char = input
Expand All @@ -56,6 +70,10 @@ pub fn decode_string(input: &dyn AsRef<str>) -> Result<String, TranslationError>
mod tests {
use super::*;

fn decode_string<'a>(input: &dyn AsRef<str>) -> Result<String, TranslationError> {
return decode_string_long(input, false);
}

#[test]
fn test_string_encode() {
assert_eq!(
Expand Down Expand Up @@ -119,4 +137,13 @@ mod tests {
"がんばれ",
);
}

#[test]
fn test_long_names() {
assert_eq!(
decode_string_long(&":sparkling_heart::sparkles::sparkles::pleading_face:,:point_right::point_left::sparkling_heart::sparkling_heart::sparkles:,:point_right::point_left::sparkling_heart::sparkling_heart::sparkles::point_right::point_left::sparkling_heart::sparkling_heart:,,,:point_right::point_left:", true)
.unwrap(),
"Long",
);
}
}
6 changes: 5 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ fn main() -> Result<()> {
.arg(Arg::from_usage(
"regress -r --regress 'Translate bottom to human-readable text (futile)'",
))
.arg(Arg::from_usage(
"-d --decode-long 'When using regress mode, decode emojis in long-name form (e.g. :sparkling_heart:, useful for pasting from slack)'",
))
.group(
ArgGroup::with_name("action")
.required(true)
Expand Down Expand Up @@ -54,7 +57,8 @@ fn main() -> Result<()> {
let result = if args.is_present("bottomify") {
bottom::encode_string(&input)
} else {
bottom::decode_string(&input).context("The input was invalid.")?
let delongate = args.is_present("decode-long");
bottom::decode_string_long(&input, delongate).context("The input was invalid.")?
};

if file_output {
Expand Down