use chars instead of splitting the string by index

This fixes an issue where multiple byte UTF-8 characters get split in half and the program panics
This commit is contained in:
OMGeeky
2023-06-17 17:56:19 +02:00
parent 80ec6773d1
commit fbc4ec1ecd
2 changed files with 11 additions and 8 deletions

View File

@@ -1,6 +1,6 @@
[package]
name = "downloader"
version = "0.3.1"
version = "0.3.2"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@@ -6,6 +6,7 @@ use std::future::Future;
use std::io::stdin;
use std::path::{Path, PathBuf};
use std::process::Stdio;
use std::str::Chars;
use anyhow::{anyhow, Context, Result};
use chrono::{Datelike, Duration};
@@ -782,14 +783,16 @@ pub fn get_playlist_title_from_twitch_video(video: &data::VideoData) -> Result<S
}
pub fn cap_long_title<S: Into<String>>(title: S) -> Result<String> {
let title = title.into();
if title.len() > MAX_VIDEO_TITLE_LENGTH - PREFIX_LENGTH - SEPARATOR_LEN {
let shortened = format!(
"{}...",
&title[0..MAX_VIDEO_TITLE_LENGTH - PREFIX_LENGTH - SEPARATOR_LEN - 3]
);
return Ok(shortened);
let mut chars = title.chars();
if chars.clone().count() > MAX_VIDEO_TITLE_LENGTH - PREFIX_LENGTH - SEPARATOR_LEN {
let shortened_chars =
chars.take(MAX_VIDEO_TITLE_LENGTH - PREFIX_LENGTH - SEPARATOR_LEN - DOTDOTDOT_LEN);
let shortened_chars: String = shortened_chars.collect();
let shortened = format!("{}...", shortened_chars);
Ok(shortened)
} else {
Ok(title)
}
Ok(title.to_string())
}
pub fn get_video_prefix_from_twitch_video(