2022-07-10 05:57:43 +00:00
|
|
|
use std::{
|
2022-07-10 06:10:26 +00:00
|
|
|
collections::HashMap,
|
2022-07-10 05:57:43 +00:00
|
|
|
fmt::Display,
|
|
|
|
ops::{Index, IndexMut},
|
|
|
|
};
|
2022-07-09 07:25:15 +00:00
|
|
|
|
|
|
|
use chrono::prelude::*;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2022-07-09 07:33:59 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct SwaybarHeader {
|
|
|
|
pub version: u32,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub click_events: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub cont_signal: Option<u16>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub stop_signal: Option<u16>,
|
|
|
|
}
|
|
|
|
|
2022-07-09 07:25:15 +00:00
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
pub struct SwaybarObject {
|
|
|
|
pub full_text: String,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub short_text: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub color: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub background: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub border: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub border_top: Option<u16>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub border_bottom: Option<u16>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub border_left: Option<u16>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub border_right: Option<u16>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
2022-10-18 12:04:28 +00:00
|
|
|
pub min_width: Option<String>,
|
2022-07-09 07:25:15 +00:00
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub align: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub name: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub instance: Option<String>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub urgent: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub separator: Option<bool>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub separator_block_width: Option<u16>,
|
|
|
|
#[serde(skip_serializing_if = "Option::is_none")]
|
|
|
|
pub markup: Option<String>,
|
|
|
|
}
|
|
|
|
|
2022-07-10 06:10:26 +00:00
|
|
|
#[derive(Debug)]
|
2022-07-09 07:25:15 +00:00
|
|
|
pub struct SwaybarArray {
|
|
|
|
objects: Vec<SwaybarObject>,
|
2022-07-10 06:10:26 +00:00
|
|
|
objects_idx_map: HashMap<String, usize>,
|
2022-07-09 07:25:15 +00:00
|
|
|
}
|
|
|
|
|
2022-07-09 07:33:59 +00:00
|
|
|
impl SwaybarHeader {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
version: 1,
|
|
|
|
click_events: None,
|
|
|
|
cont_signal: None,
|
|
|
|
stop_signal: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-09 07:25:15 +00:00
|
|
|
impl SwaybarObject {
|
2022-07-10 05:57:43 +00:00
|
|
|
pub fn new(name: String) -> Self {
|
2022-07-09 07:25:15 +00:00
|
|
|
Self {
|
|
|
|
full_text: String::new(),
|
|
|
|
short_text: None,
|
|
|
|
color: None,
|
|
|
|
background: None,
|
2022-07-09 09:18:12 +00:00
|
|
|
border: Some("#ffffffff".into()),
|
2022-07-09 07:25:15 +00:00
|
|
|
border_top: None,
|
|
|
|
border_bottom: None,
|
|
|
|
border_left: None,
|
|
|
|
border_right: None,
|
|
|
|
min_width: None,
|
|
|
|
align: None,
|
2022-07-10 05:57:43 +00:00
|
|
|
name: Some(name),
|
2022-07-09 07:25:15 +00:00
|
|
|
instance: None,
|
|
|
|
urgent: None,
|
|
|
|
separator: None,
|
|
|
|
separator_block_width: None,
|
|
|
|
markup: None,
|
|
|
|
}
|
|
|
|
}
|
2022-07-09 07:57:49 +00:00
|
|
|
|
2022-07-10 05:57:43 +00:00
|
|
|
pub fn from_string(name: String, string: String) -> Self {
|
2022-07-09 07:57:49 +00:00
|
|
|
Self {
|
|
|
|
full_text: string,
|
|
|
|
short_text: None,
|
|
|
|
color: None,
|
|
|
|
background: None,
|
2022-07-09 09:18:12 +00:00
|
|
|
border: Some("#ffffffff".into()),
|
2022-07-09 07:57:49 +00:00
|
|
|
border_top: None,
|
|
|
|
border_bottom: None,
|
|
|
|
border_left: None,
|
|
|
|
border_right: None,
|
|
|
|
min_width: None,
|
|
|
|
align: None,
|
2022-07-10 05:57:43 +00:00
|
|
|
name: Some(name),
|
2022-07-09 07:57:49 +00:00
|
|
|
instance: None,
|
|
|
|
urgent: None,
|
|
|
|
separator: None,
|
|
|
|
separator_block_width: None,
|
|
|
|
markup: None,
|
|
|
|
}
|
|
|
|
}
|
2022-07-10 05:57:43 +00:00
|
|
|
|
2022-07-10 08:58:10 +00:00
|
|
|
pub fn from_error_string(name: String, msg: String) -> Self {
|
|
|
|
Self {
|
|
|
|
full_text: msg,
|
|
|
|
short_text: None,
|
|
|
|
color: Some("#ff2222ff".into()),
|
|
|
|
background: None,
|
|
|
|
border: Some("#ffffffff".into()),
|
|
|
|
border_top: None,
|
|
|
|
border_bottom: None,
|
|
|
|
border_left: None,
|
|
|
|
border_right: None,
|
|
|
|
min_width: None,
|
|
|
|
align: None,
|
|
|
|
name: Some(name),
|
|
|
|
instance: None,
|
|
|
|
urgent: None,
|
|
|
|
separator: None,
|
|
|
|
separator_block_width: None,
|
|
|
|
markup: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 05:57:43 +00:00
|
|
|
pub fn update_as_net_down(&mut self, metric: String) {
|
|
|
|
self.full_text = metric;
|
|
|
|
self.color = Some("#ff8888ff".to_owned());
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_as_net_up(&mut self, metric: String) {
|
|
|
|
self.full_text = metric;
|
|
|
|
self.color = Some("#88ff88ff".to_owned());
|
|
|
|
}
|
|
|
|
|
2022-08-11 02:47:49 +00:00
|
|
|
pub fn update_as_date(&mut self, format_str: &str) {
|
2022-07-10 05:57:43 +00:00
|
|
|
let current_time: DateTime<Local> = Local::now();
|
2022-08-11 02:47:49 +00:00
|
|
|
let current_time = current_time.format(format_str);
|
2022-07-10 05:57:43 +00:00
|
|
|
self.full_text = current_time.to_string();
|
|
|
|
self.color = None;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_as_generic(&mut self, metric: String, color: Option<String>) {
|
|
|
|
self.full_text = metric;
|
|
|
|
self.color = color;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update_as_error(&mut self, msg: String) {
|
|
|
|
self.full_text = msg;
|
|
|
|
self.color = Some("#ff2222ff".to_owned());
|
|
|
|
}
|
|
|
|
|
2022-08-11 02:51:12 +00:00
|
|
|
#[allow(dead_code)]
|
2022-07-10 05:57:43 +00:00
|
|
|
pub fn set_name(&mut self, name: Option<String>) {
|
|
|
|
self.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_name(&self) -> Option<&str> {
|
|
|
|
self.name.as_deref()
|
|
|
|
}
|
2022-07-09 07:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SwaybarArray {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
objects: Vec::new(),
|
2022-07-10 06:10:26 +00:00
|
|
|
objects_idx_map: HashMap::new(),
|
2022-07-09 07:25:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push_object(&mut self, object: SwaybarObject) {
|
|
|
|
self.objects.push(object);
|
2022-07-10 06:10:26 +00:00
|
|
|
self.refresh_map();
|
2022-07-09 07:25:15 +00:00
|
|
|
}
|
2022-07-10 05:57:43 +00:00
|
|
|
|
|
|
|
pub fn is_empty(&self) -> bool {
|
|
|
|
self.objects.is_empty()
|
|
|
|
}
|
|
|
|
|
2022-08-11 02:51:12 +00:00
|
|
|
#[allow(dead_code)]
|
2022-07-10 05:57:43 +00:00
|
|
|
pub fn get_by_name(&self, name: &str) -> Option<&SwaybarObject> {
|
2022-07-10 06:10:26 +00:00
|
|
|
if let Some(idx) = self.objects_idx_map.get(name) {
|
|
|
|
return Some(&self.objects[*idx]);
|
2022-07-10 05:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_by_name_mut(&mut self, name: &str) -> Option<&mut SwaybarObject> {
|
2022-07-10 06:10:26 +00:00
|
|
|
if let Some(idx) = self.objects_idx_map.get(name) {
|
|
|
|
return Some(&mut self.objects[*idx]);
|
2022-07-10 05:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2022-07-10 06:10:26 +00:00
|
|
|
|
|
|
|
fn refresh_map(&mut self) {
|
|
|
|
self.objects_idx_map.clear();
|
|
|
|
for (idx, object) in self.objects.iter().enumerate() {
|
|
|
|
if let Some(name) = object.get_name() {
|
|
|
|
self.objects_idx_map.insert(name.to_owned(), idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-10 05:57:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Index<usize> for SwaybarArray {
|
|
|
|
type Output = SwaybarObject;
|
|
|
|
|
|
|
|
fn index(&self, index: usize) -> &Self::Output {
|
|
|
|
self.objects.index(index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IndexMut<usize> for SwaybarArray {
|
|
|
|
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
|
|
|
|
self.objects.index_mut(index)
|
|
|
|
}
|
2022-07-09 07:25:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for SwaybarArray {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
let mut s = serde_json::to_string(&self.objects)
|
|
|
|
.expect("Should be able to serialize SwaybarArray::objects");
|
|
|
|
s.push(',');
|
|
|
|
f.write_str(&s)
|
|
|
|
}
|
|
|
|
}
|