mirror of
https://github.com/Xithrius/twitch-tui.git
synced 2024-09-11 13:45:30 +03:00
Add twitch badges (#108)
This commit is contained in:
parent
9fea3b91a8
commit
fc660349fb
@ -29,3 +29,5 @@ palette = "pastel"
|
||||
title_shown = true
|
||||
# Show padding around chat frame
|
||||
padding = true
|
||||
# Show twitch badges
|
||||
badges = true
|
||||
|
@ -33,6 +33,10 @@ pub struct Cli {
|
||||
/// Username color palette
|
||||
#[clap(short, long, possible_values = &["pastel", "vibrant", "warm", "cool"])]
|
||||
pub palette: Option<Palette>,
|
||||
|
||||
/// Twitch badges support
|
||||
#[clap(short, long)]
|
||||
pub badges: bool,
|
||||
}
|
||||
|
||||
pub fn merge_args_into_config(config: &mut CompleteConfig, args: Cli) {
|
||||
@ -62,4 +66,5 @@ pub fn merge_args_into_config(config: &mut CompleteConfig, args: Cli) {
|
||||
if let Some(palette) = args.palette {
|
||||
config.frontend.palette = palette;
|
||||
}
|
||||
config.frontend.badges = args.badges;
|
||||
}
|
||||
|
@ -80,6 +80,8 @@ pub struct FrontendConfig {
|
||||
pub title_shown: bool,
|
||||
/// Show padding around chat frame
|
||||
pub padding: bool,
|
||||
/// Show twitch badges
|
||||
pub badges: bool,
|
||||
}
|
||||
|
||||
impl CompleteConfig {
|
||||
|
@ -11,6 +11,11 @@ use crate::handlers::{
|
||||
data::{Data, DataBuilder},
|
||||
};
|
||||
|
||||
const VIP_BADGE: char = '\u{1F48E}';
|
||||
const MODERATOR_BADGE: char = '\u{1F528}';
|
||||
const SUBSCRIBER_BADGE: char = '\u{2B50}';
|
||||
const PRIME_GAMING_BADGE: char = '\u{1F451}';
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Action {
|
||||
Privmsg(String),
|
||||
@ -105,14 +110,54 @@ pub async fn twitch_irc(mut config: CompleteConfig, tx: Sender<Data>, mut rx: Re
|
||||
Some(username) => username.to_string(),
|
||||
None => "Undefined username".to_string(),
|
||||
};
|
||||
// try to get username from message tags
|
||||
if let Some(ref tags) = message.tags {
|
||||
for tag in tags {
|
||||
if tag.0 == *"display-name" {
|
||||
if let Some(ref value) = tag.1 {
|
||||
name = value.to_string();
|
||||
if config.frontend.badges {
|
||||
let mut badges = String::new();
|
||||
if let Some(ref tags) = message.tags {
|
||||
let mut vip_badge = None;
|
||||
let mut moderator_badge = None;
|
||||
let mut subscriber_badge = None;
|
||||
let mut prime_badge = None;
|
||||
let mut display_name = None;
|
||||
for tag in tags {
|
||||
if tag.0 == *"display-name" {
|
||||
if let Some(ref value) = tag.1 {
|
||||
display_name = Some(value.to_string());
|
||||
}
|
||||
}
|
||||
break;
|
||||
if tag.0 == *"badges" {
|
||||
if let Some(ref value) = tag.1 {
|
||||
if !value.is_empty() && value.contains("vip") {
|
||||
vip_badge = Some(VIP_BADGE);
|
||||
}
|
||||
if !value.is_empty() && value.contains("moderator") {
|
||||
moderator_badge = Some(MODERATOR_BADGE);
|
||||
}
|
||||
if !value.is_empty() && value.contains("subscriber") {
|
||||
subscriber_badge = Some(SUBSCRIBER_BADGE);
|
||||
}
|
||||
if !value.is_empty() && value.contains("premium") {
|
||||
prime_badge = Some(PRIME_GAMING_BADGE);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(display_name) = display_name {
|
||||
name = display_name;
|
||||
}
|
||||
if let Some(badge) = vip_badge {
|
||||
badges.push(badge);
|
||||
}
|
||||
if let Some(badge) = moderator_badge {
|
||||
badges.push(badge);
|
||||
}
|
||||
if let Some(badge) = subscriber_badge {
|
||||
badges.push(badge);
|
||||
}
|
||||
if let Some(badge) = prime_badge {
|
||||
badges.push(badge);
|
||||
}
|
||||
if !badges.is_empty() {
|
||||
name = badges.clone() + &name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
use std::vec::IntoIter;
|
||||
|
||||
use rustyline::line_buffer::LineBuffer;
|
||||
use textwrap::core::display_width;
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
@ -9,18 +10,18 @@ pub fn align_text(text: &str, alignment: &str, maximum_length: u16) -> String {
|
||||
panic!("Parameter of 'maximum_length' cannot be below 1.");
|
||||
}
|
||||
|
||||
// Compute the display width of `text` with support of emojis and CJK characters
|
||||
let mut dw = display_width(text);
|
||||
if dw > maximum_length as usize {
|
||||
dw = maximum_length as usize;
|
||||
}
|
||||
match alignment {
|
||||
"left" => text.to_string(),
|
||||
"right" => format!(
|
||||
"{text:>width$}",
|
||||
text = text,
|
||||
width = std::cmp::max(maximum_length, text.len() as u16) as usize
|
||||
),
|
||||
"center" => format!(
|
||||
"{text:^width$}",
|
||||
text = text,
|
||||
width = std::cmp::max(maximum_length, text.len() as u16) as usize
|
||||
),
|
||||
"right" => format!("{}{}", " ".repeat(maximum_length as usize - dw), text),
|
||||
"center" => {
|
||||
let side_spaces =
|
||||
" ".repeat(((maximum_length / 2) - (((dw / 2) as f32).floor() as u16)) as usize);
|
||||
format!("{}{}{}", side_spaces, text, side_spaces)
|
||||
}
|
||||
_ => text.to_string(),
|
||||
}
|
||||
}
|
||||
@ -102,6 +103,8 @@ mod tests {
|
||||
format!("{}{}", " ".repeat(9), "a")
|
||||
);
|
||||
assert_eq!(align_text("a", "right", 1), "a".to_string());
|
||||
assert_eq!(align_text("你好", "right", 5), " 你好");
|
||||
assert_eq!(align_text("👑123", "right", 6), " 👑123");
|
||||
}
|
||||
|
||||
#[test]
|
||||
@ -111,6 +114,8 @@ mod tests {
|
||||
format!("{}{}{}", " ".repeat(5), "a", " ".repeat(5))
|
||||
);
|
||||
assert_eq!(align_text("a", "center", 1), "a".to_string());
|
||||
assert_eq!(align_text("你好", "center", 6), " 你好 ");
|
||||
assert_eq!(align_text("👑123", "center", 7), " 👑123 ");
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user