1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::io::{BufWriter, Write};
use serde::{Serialize, Serializer};
use serde_derive::Serialize;
#[derive(Serialize, Default)]
pub struct Header {
version: u32,
#[serde(skip_serializing_if = "Option::is_none")]
stop_signal: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
cont_signal: Option<u32>,
#[serde(skip_serializing_if = "Option::is_none")]
click_events: Option<bool>,
}
impl Header {
pub fn new(version:u32) -> Self {
let mut ret = Self::default();
ret.version = version;
return ret;
}
}
#[derive(Clone)]
pub struct ColorRGB(pub u8,pub u8,pub u8);
impl Serialize for ColorRGB {
fn serialize<S:Serializer>(&self, s:S) -> Result<S::Ok, S::Error> {
let color_string = format!("#{:.2x}{:02x}{:02x}", self.0, self.1, self.2);
s.serialize_str(&color_string)
}
}
impl ColorRGB {
pub fn red() -> Self {
ColorRGB(255, 0, 0)
}
pub fn green() -> Self {
ColorRGB(0, 255, 0)
}
pub fn blue() -> Self {
ColorRGB(0, 0, 255)
}
pub fn yellow() -> Self {
ColorRGB(255, 255, 0)
}
}
#[derive(Debug, Clone)]
pub enum MarkupLang {
Text,
Pango,
}
impl Serialize for MarkupLang {
fn serialize<S:Serializer>(&self, s:S) -> Result<S::Ok, S::Error> {
match self {
MarkupLang::Text => s.serialize_str("none"),
MarkupLang::Pango => s.serialize_str("pango"),
}
}
}
#[derive(Serialize, Clone)]
pub struct Block {
full_text : String,
#[serde(skip_serializing_if = "String::is_empty")]
short_text: String,
#[serde(skip_serializing_if = "Option::is_none")]
color : Option<ColorRGB>,
markup : MarkupLang,
}
impl Block {
pub fn new() -> Self {
Self {
full_text: "".to_string(),
short_text: "".to_string(),
color: None,
markup: MarkupLang::Text
}
}
#[allow(dead_code)]
pub fn full_text(&mut self, text:&str) -> &mut Self {
self.full_text = text.to_string();
self
}
pub fn append_full_text(&mut self, text:&str) -> &mut Self {
self.full_text.push_str(text);
self
}
pub fn short_text(&mut self, text:&str) -> &mut Self {
self.short_text = text.to_string();
self
}
pub fn color(&mut self, color:ColorRGB) -> &mut Self {
self.color = Some(color);
self
}
pub fn clear_color(&mut self) -> &mut Self {
self.color = None;
self
}
pub fn use_pango(&mut self) -> &mut Self {
self.markup = MarkupLang::Pango;
self
}
pub fn use_plain_text(&mut self) -> &mut Self {
self.markup = MarkupLang::Text;
self
}
}
pub struct I3Protocol<T:Write>(BufWriter<T>);
impl <T:Write> I3Protocol<T> {
fn write<S:AsRef<str>>(&mut self, data:S) {
self.0.write_all(AsRef::<str>::as_ref(&data).as_bytes()).expect("Cannot write");
self.0.write_all(b"\n").expect("Cannot write");
self.0.flush().ok();
}
fn write_json<S:Serialize>(&mut self, data:&S) {
if let Ok(serialized) = serde_json::to_string(data) {
self.write(serialized);
}
}
pub fn new(header:Header, wr:T) -> Self {
let mut ret = I3Protocol(BufWriter::new(wr));
ret.write_json(&header);
ret.write("[ []");
ret
}
pub fn refresh(&mut self, status:&Vec<Block>) {
self.write(",");
self.write_json(status)
}
}
impl <T:Write> Drop for I3Protocol<T> {
fn drop(&mut self) {
self.write("]");
}
}