Skip to content

Commit

Permalink
refactor(dynamic string): use vec instead of indexmap
Browse files Browse the repository at this point in the history
  • Loading branch information
JakeStanger committed Mar 1, 2023
1 parent 0125ce5 commit 7212bbc
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions src/dynamic_string.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::script::{OutputStream, Script};
use crate::{lock, send};
use gtk::prelude::*;
use indexmap::IndexMap;
use std::sync::{Arc, Mutex};
use tokio::spawn;

Expand Down Expand Up @@ -60,13 +59,13 @@ impl DynamicString {
chars.drain(..skip);
}

let label_parts = Arc::new(Mutex::new(IndexMap::new()));
let label_parts = Arc::new(Mutex::new(Vec::new()));
let (tx, rx) = glib::MainContext::channel(glib::PRIORITY_DEFAULT);

for (i, segment) in segments.into_iter().enumerate() {
match segment {
DynamicStringSegment::Static(str) => {
lock!(label_parts).insert(i, str);
lock!(label_parts).push(str);
}
DynamicStringSegment::Dynamic(script) => {
let tx = tx.clone();
Expand All @@ -78,13 +77,9 @@ impl DynamicString {
if let OutputStream::Stdout(out) = out {
let mut label_parts = lock!(label_parts);

label_parts.insert(i, out);

let string = label_parts
.iter()
.map(|(_, part)| part.as_str())
.collect::<String>();
let _ = std::mem::replace(&mut label_parts[i], out);

let string = label_parts.join("");
send!(tx, string);
}
})
Expand All @@ -96,11 +91,7 @@ impl DynamicString {

// initialize
{
let label_parts = lock!(label_parts)
.iter()
.map(|(_, part)| part.as_str())
.collect::<String>();

let label_parts = lock!(label_parts).join("");
send!(tx, label_parts);
}

Expand Down

0 comments on commit 7212bbc

Please sign in to comment.