Skip to content

Commit

Permalink
feat: Significantly improved emoji support, now all emoji can be acce…
Browse files Browse the repository at this point in the history
…ssed
  • Loading branch information
Saphereye committed Feb 15, 2024
1 parent 33f3530 commit ead98d0
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 17 deletions.
36 changes: 35 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "lan-chat"
version = "0.10.0"
version = "0.11.0"
edition = "2021"
authors = ["Saphereye <[email protected]>"]
license = "MIT"
Expand Down Expand Up @@ -31,3 +31,4 @@ env_logger = "0.11.1"
clap = { version = "4.5.0", features = ["derive"] }
rand = "0.8.5"
lazy_static = "1.4.0"
emojis = "0.6.1"
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ You will be prompted to enter a pseudonym. Alternatively, you can set the pseudo
lan-chat -s <server-ip> -p <pseudonym>
```

4. To insert emojis in the chat, use the following format: `:<emoji name>:`. For example is you type `That's funny :laugh:` it will be rendered as `That's funny 😂`.

The supported emojis are as follows
| Command | Emoji |
|-------------|-------|
| :smile: | 😊 |
| :laugh: | 😂 |
| :thumbs_up: | 👍 |
| :sad: | 😔 |

## Running Example

![image](https://github.com/Saphereye/lan-chat/assets/59739923/daada2ff-4cf0-4251-8e91-92b867f1c2bc)
53 changes: 39 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,40 @@ use networking::*;
use std::io::{self, stdout};
use std::net::TcpStream;
use std::sync::{Arc, Mutex};
use emojis;

fn replace_keywords_with_emojis(text: &str) -> String {
let mut output = String::new();
let mut current_word = String::new();
let mut inside_keyword = false;

for ch in text.chars() {
match ch {
':' => {
if inside_keyword {
if let Some(emoji) = emojis::get_by_shortcode(&current_word) {
output.push_str(emoji.as_str());
} else {
output.push(':');
output.push_str(&current_word);
output.push(':');
}
current_word.clear();
}
inside_keyword = !inside_keyword;
},
_ => {
if inside_keyword {
current_word.push(ch);
} else {
output.push(ch);
}
},
}
}

output
}

use crossterm::{
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyModifiers},
Expand Down Expand Up @@ -47,7 +81,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}

if args.server_ip.is_empty() {
return Err("Please provide a server IP address or start as server. Try lan-chat --help for more info.".into());
println!("Please provide a server IP address or start as server. Try lan-chat --help for more info");
std::process::exit(1);
}

let message_vector: Arc<Mutex<Vec<MessageType>>> = Arc::new(Mutex::new(Vec::new()));
Expand Down Expand Up @@ -142,10 +177,8 @@ fn handle_events(

let message = message
.trim()
.to_string()
.replace(":smile:", "😊")
.replace(":laugh:", "😂")
.replace(":thumbs_up:", "👍");
.to_string();
let message = replace_keywords_with_emojis(&message);

if let Some(prefix) = message.strip_prefix('/') {
match prefix {
Expand All @@ -166,16 +199,8 @@ fn handle_events(
));

message_vector.push(MessageType::Info("".to_string()));
message_vector
.push(MessageType::Info("Text formatting:".to_string()));
message_vector.push(MessageType::Info(
"Use :smile: to send 😊".to_string(),
));
message_vector.push(MessageType::Info(
"Use :laugh: to send 😂".to_string(),
));
message_vector.push(MessageType::Info(
"Use :thumbs_up: to send 👍".to_string(),
"To put emojis use this format, e.g. :smile: to send 😊".to_string(),
));

message_vector.push(MessageType::Info("".to_string()));
Expand Down
2 changes: 1 addition & 1 deletion src/networking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ lazy_static! {
"Type /help in the chat".to_string(),
"Use arrow keys to see chat history".to_string(),
"Type /quit to leave program".to_string(),
"Use :smile: to insert a smiley, try :laugh: and :thumbs_up: too ;".to_string(),
"Use :smile: to insert a smiley, try :laughing: and :thumbsup: too. Look at 'gemoji' to learn more.".to_string(),
]);
}

Expand Down

0 comments on commit ead98d0

Please sign in to comment.