summaryrefslogtreecommitdiff
path: root/src/parser.rs
diff options
context:
space:
mode:
authorNamilskyy <alive6863@gmail.com>2025-11-22 22:17:43 +0300
committerNamilskyy <alive6863@gmail.com>2025-11-22 22:52:57 +0300
commit2e6320185ba358368506fd8b8f2fdae9763447b0 (patch)
tree98a8340970b9872eaea6c0628d6a168a53fb1ffc /src/parser.rs
parent48c25e1fe126d45f5c75ea184c3d3ed244194a66 (diff)
Fixed tearing in output
Diffstat (limited to 'src/parser.rs')
-rw-r--r--src/parser.rs36
1 files changed, 34 insertions, 2 deletions
diff --git a/src/parser.rs b/src/parser.rs
index 8e17682..8def9c2 100644
--- a/src/parser.rs
+++ b/src/parser.rs
@@ -7,7 +7,7 @@ use reqwest::Client;
use toml;
use serde::{Deserialize, Serialize};
use serde_json;
-use serde_yml;
+use serde_yml;
// use crate::configmanager::Config;
use crate::shared::*;
@@ -162,7 +162,7 @@ fn load_arts(debug: bool) -> Result<ArtsData, Box<dyn std::error::Error>> {
fn process_placeholders(art: &str) -> String {
let strart = art.to_string();
- let mut processed_art = strart.replace("{0}", " ")
+ let processed_art = strart.replace("{0}", " ")
.replace("<Yellow>", "\x1b[0;33m")
.replace("<Blue>", "\x1b[0;34m")
.replace("<Purple>", "\x1b[0;35m")
@@ -206,3 +206,35 @@ pub fn prepare_art(weather_data: &WeatherData, debug: bool) -> Result<String, Bo
}
Ok(processed_art)
}
+
+/// Remove ANSI escape codes from string and return visible length
+pub fn visible_length(s: &str) -> usize {
+ let mut count = 0;
+ let mut chars = s.chars().peekable();
+
+ while let Some(ch) = chars.next() {
+ if ch == '\x1b' && chars.peek() == Some(&'[') {
+ // Found ANSI escape sequence, skip until 'm'
+ chars.next(); // consume '['
+ while let Some(&next_ch) = chars.peek() {
+ chars.next(); // consume character
+ if next_ch == 'm' {
+ break; // end of ANSI sequence
+ }
+ }
+ } else {
+ count += 1;
+ }
+ }
+
+ count
+}
+
+
+/// Left-pad a string with spaces to the specified visible width, accounting for ANSI codes
+pub fn pad_with_ansi(s: &str, width: usize) -> String {
+ let visible_len = visible_length(s);
+ let padding_needed = if width > visible_len { width - visible_len } else { 0 };
+ let padding = " ".repeat(padding_needed);
+ format!("{}{}", s, padding)
+} \ No newline at end of file