mirror of
https://github.com/OMGeeky/downloader.git
synced 2025-12-27 06:29:26 +01:00
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:
@@ -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
|
||||
|
||||
17
src/lib.rs
17
src/lib.rs
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user