Pi66

pino-rs
Rust notification daemon.
git clone https://git.pi66.xyz/pino-rs

← back to log

V1.1.2 | add Unix-Socket

author: pixel
date: 2025-04-30 16:56
hash: 8153b0b9902fafff87363c1a615ef7f62d43ca7d

Diffstat:

M

Cargo.toml
2

M

src/colors.rs
19 ++--

M

src/config.rs
22 ++--

M

src/main.rs
153 +++++++++++++++---------------

M

src/screen.rs
2

M

src/ui.rs
121 +++++++++++++++---------

D

src/utils.rs
18 ----
  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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
diff --git a/Cargo.toml b/Cargo.toml
index 66a737a..9c9158e 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "pino"
-version = "1.1.1"
+version = "1.1.2"
 edition = "2024"

 [dependencies]
diff --git a/src/colors.rs b/src/colors.rs
index 97f8df4..6d1aa8b 100644
--- a/src/colors.rs
+++ b/src/colors.rs
@@ -12,7 +12,7 @@ pub fn get_config_dir() -> PathBuf {
     panic!("Could not determine config directory");
 }

-fn create_template(app:&str) {
+fn create_template(app: &str) {
     let config = get_config_dir()
         .join(app)
         .join("templates")
@@ -105,27 +105,30 @@ pub fn pywal(
     border_color: String,
     title_color: String,
     message_color: String,
-) ->(String,String,String,String) {
+) -> (String, String, String, String) {
     let home = env::var("HOME").expect("Can't Find Home Dir");

-    if !get_config_dir().join("wal/templates/colors-pino.toml").exists(){
+    if !get_config_dir()
+        .join("wal/templates/colors-pino.toml")
+        .exists()
+    {
         create_template("wal");
     }

-
-
-    if !get_config_dir().join("walrs/templates/colors-pino.toml").exists(){
+    if !get_config_dir()
+        .join("walrs/templates/colors-pino.toml")
+        .exists()
+    {
         create_template("walrs");
     }

-
     let cache_colors = PathBuf::from(home)
         .join(".cache")
         .join("wal")
         .join("colors-pino.toml");
     let content = fs::read_to_string(&cache_colors).expect("Can't Read Colors File!!!");
     let colors: Pywal = toml::from_str(&content).expect("Invalid TOML Format!!!");
-     (
+    (
         get_color(&colors, background_color.as_str()).to_string(),
         get_color(&colors, border_color.as_str()).to_string(),
         get_color(&colors, title_color.as_str()).to_string(),
diff --git a/src/config.rs b/src/config.rs
index d78ba8c..8f26304 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -1,13 +1,13 @@
-use std::{path::PathBuf,fs::{create_dir_all,OpenOptions},io::Write};
+use std::{
+    fs::{OpenOptions, create_dir_all},
+    io::Write,
+    path::PathBuf,
+};

+pub fn generate_config(config_path: PathBuf) {
+    let config = config_path.join("pino").join("config.toml");

-pub fn generate_config(config_path: PathBuf){
-    let config = config_path
-        .join("pino")
-        .join("config.toml");
-
-    create_dir_all(config.parent().unwrap())
-        .expect("Failed to create directories for template");
+    create_dir_all(config.parent().unwrap()).expect("Failed to create directories for template");
     println!("Creating config file at: {}", config.display());
     let mut template = OpenOptions::new()
         .write(true)
@@ -65,9 +65,7 @@ border_color      = \"color1\"
 title_color       = \"fg\"
 message_color     = \"color8\"

-")
+",
+        )
         .expect("Can't Create Template File !!!");
 }
-
-
-
diff --git a/src/main.rs b/src/main.rs
index 73d5f1d..2dc5782 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,51 +1,53 @@
-use serde::Deserialize;
-use std::{fs::{self, File}, io::Write};
 use argh::FromArgs;
-use utils::is_running;
-
+use serde::Deserialize;
+use std::{fs, io::Write, os::unix::net::UnixStream, path::Path};

-mod utils;
 mod colors;
 mod config;
 mod screen;
 mod ui;

-
-// Get Args
 #[derive(FromArgs)]
-
-#[argh(description = "This tool lets you display notification with customizable options. you can also use a configuration file to set theme
-and everything easily (conf path = ~/.config/pino)")]
+#[argh(
+    description = "This tool lets you display notification with customizable options. you can also use a configuration file to set theme
+and everything easily (conf path = ~/.config/pino)"
+)]
 struct Arg {
-
-    #[argh(option,short = 't', description = "set the notification title content")]
-    title:Option<String> ,
-
-    #[argh(option,short = 'm', description = "set the notification message content")]
-    message:Option<String>,
-
-
-    #[argh(option,short = 'd', description = "set the delay before program closes with secends")]
-    delay:Option<i32> ,
-
-    #[argh(switch,short = 'f', description = "print all the fonts that you can use it")]
-    font:bool ,
-
-    #[argh(option,short = 'c', description = "set a custom configuration file")]
-    config:Option<String> ,
-
-
-    #[argh(switch,short = 'v', description = "set a custom configuration file")]
-    version:bool ,
+    #[argh(
+        option,
+        short = 't',
+        description = "set the notification title content"
+    )]
+    title: Option<String>,
+
+    #[argh(
+        option,
+        short = 'm',
+        description = "set the notification message content"
+    )]
+    message: Option<String>,
+
+    #[argh(
+        option,
+        short = 'd',
+        description = "set the delay before program closes with secends"
+    )]
+    delay: Option<u64>,
+
+    #[argh(
+        switch,
+        short = 'f',
+        description = "print all the fonts that you can use it"
+    )]
+    font: bool,
+
+    #[argh(option, short = 'c', description = "set a custom configuration file")]
+    config: Option<String>,
+
+    #[argh(switch, short = 'v', description = "set a custom configuration file")]
+    version: bool,
 }

-
-
-
-
-
-
-// Read the Config File
 #[derive(Debug, Deserialize)]
 struct Config {
     screen: Screen,
@@ -65,7 +67,7 @@ struct Screen {
     y: i32,
     width: i32,
     height: i32,
-    delay: i32,
+    delay: u64,
 }
 #[derive(Debug, Deserialize)]
 struct Frame {
@@ -101,53 +103,58 @@ struct Pywal {
     message_color: String,
 }

-
-
 fn main() {
     let config_folder = colors::get_config_dir();
-    let args:Arg = argh::from_env();
+    let args: Arg = argh::from_env();

     if args.version {
-        println!("v1.1.1");
-        return ;
+        println!("v1.1.2");
+        return;
     }

-    if args.font{
+    if args.font {
         ui::print_fonts();
-        return ;
+        return;
     }

-
     let config_file = match args.config {
         Some(path) => path,
         None => {
             if !config_folder.join("pino").exists() {
                 config::generate_config(config_folder.clone());
             }
-            config_folder.join("pino").join("config.toml").to_string_lossy().into_owned()
+            config_folder
+                .join("pino")
+                .join("config.toml")
+                .to_string_lossy()
+                .into_owned()
         }
     };

     let config_content = fs::read_to_string(config_file).expect("Faild ");
     let config: Config = toml::from_str(&config_content).expect("Faild");

-
-
-    let mut pino_check = File::create("/tmp/pino-check").unwrap();
-
-    writeln!(pino_check ,"{}",args.title.unwrap_or("Title".to_string())).unwrap();
-    writeln!(pino_check ,"{}",args.message.unwrap_or("you didn't set the title or message".to_string())).unwrap();
-    writeln!(pino_check ,"{}",args.delay.unwrap_or(config.screen.delay)).unwrap();
-
-
-    if !is_running("pino"){
-
+    if Path::new("/tmp/pino-check.sock").exists() {
+        let mut stream = UnixStream::connect("/tmp/pino-check.sock").unwrap();
+        stream
+            .write_all(
+                format!(
+                    "{}|+|{}|+|{}",
+                    args.title.unwrap_or("Title".to_string()),
+                    args.message
+                        .unwrap_or("you didn't set the title or message".to_string()),
+                    args.delay.unwrap_or(config.screen.delay)
+                )
+                .as_bytes(),
+            )
+            .unwrap()
+    } else {
         let colors = match config.pywal.pywal {
             true => colors::pywal(
                 config.pywal.background_color.to_string(),
                 config.pywal.border_color.to_string(),
                 config.pywal.title_color.to_string(),
-                config.pywal.message_color.to_string()
+                config.pywal.message_color.to_string(),
             ),
             false => (
                 config.frame.fg_color,
@@ -155,10 +162,8 @@ fn main() {
                 config.title.color,
                 config.message.color,
             ),
-
         };

-        // Get Screen Placment
         let screen = screen::get_size(
             config.screen.monitor,
             config.screen.vertical.as_str(),
@@ -169,25 +174,19 @@ fn main() {
             config.screen.height,
         );

-        //UI
         ui::ui(
-            screen,config.frame.font_family,
-            (
-                config.border.weight,
-                config.border.radius,
-            ),
+            screen,
+            config.frame.font_family,
+            (config.border.weight, config.border.radius),
+            (config.title.x, config.title.y, config.title.font_size),
+            (config.message.x, config.message.y, config.message.font_size),
+            colors,
             (
-                config.title.x,
-                config.title.y,
-                config.title.font_size,
+                args.title.unwrap_or("Title".to_string()),
+                args.message
+                    .unwrap_or("you didn't set the title or message".to_string()),
+                args.delay.unwrap_or(config.screen.delay),
             ),
-            (
-                config.message.x,
-                config.message.y,
-                config.message.font_size,
-            ),colors
         );
     }
 }
-
-
diff --git a/src/screen.rs b/src/screen.rs
index 524e512..c47efcb 100644
--- a/src/screen.rs
+++ b/src/screen.rs
@@ -17,7 +17,7 @@ pub fn get_size(

     if h.to_lowercase() == "left" {
         if v.to_lowercase() == "top" {
-            return (screens[monitor].0 + ax, ay + screens[monitor].1 , aw , ah);
+            return (screens[monitor].0 + ax, ay + screens[monitor].1, aw, ah);
         } else if v.to_lowercase() == "bottom" {
             return (
                 screens[monitor].0 + ax,
diff --git a/src/ui.rs b/src/ui.rs
index d11c590..025ab1d 100644
--- a/src/ui.rs
+++ b/src/ui.rs
@@ -1,5 +1,5 @@
 use fltk::{
-    app::{self,get_font_names},
+    app::{self, get_font_names},
     draw,
     enums::{Align, Color, Event, Font},
     frame::Frame,
@@ -7,10 +7,14 @@ use fltk::{
     window::Window,
 };
 use std::{
-    fs::{self, metadata}, sync::mpsc, thread, time::{Duration, Instant}
+    io::Read,
+    os::unix::net::UnixListener,
+    path::Path,
+    sync::mpsc,
+    thread,
+    time::{Duration, Instant},
 };

-
 pub fn print_fonts() {
     for font in get_font_names() {
         println!("{font}");
@@ -21,9 +25,10 @@ pub fn ui(
     screen_info: (i32, i32, i32, i32),
     font_family: String,
     border: (i32, i32),
-    title: (i32,i32,i32),
-    message: (i32,i32,i32),
+    title: (i32, i32, i32),
+    message: (i32, i32, i32),
     colors: (String, String, String, String),
+    data: (String, String, u64),
 ) {
     let app = app::App::default().load_system_fonts();

@@ -34,7 +39,6 @@ pub fn ui(
         screen_info.3,
         "Pino",
     );
-
     wind1.set_color(Color::from_hex_str(colors.1.as_str()).unwrap());

     let mut wind2 = Window::new(
@@ -46,25 +50,40 @@ pub fn ui(
     );
     wind2.set_color(Color::from_hex_str(colors.0.as_str()).unwrap());

-    let mut title_frame =   Frame::new(5 + title.0, 20 + title.1, wind2.w() - 10, wind2.h()-10, "");
-    let mut message_frame = Frame::new(5 + message.0, 20 + message.1, wind2.w() - 10, wind2.h()-10, "");
+    let mut title_frame = Frame::new(
+        5 + title.0,
+        20 + title.1,
+        wind2.w() - 10,
+        wind2.h() - 10,
+        "",
+    );
+    let mut message_frame = Frame::new(
+        5 + message.0,
+        20 + message.1,
+        wind2.w() - 10,
+        wind2.h() - 10,
+        "",
+    );

     title_frame.set_align(Align::Top | Align::Left);
     title_frame.set_label_color(Color::from_hex_str(colors.2.as_str()).unwrap());
     title_frame.set_label_font(Font::by_name(font_family.as_str()));
     title_frame.set_label_size(title.2);

-
     message_frame.set_align(Align::Top | Align::Left);
     message_frame.set_label_color(Color::from_hex_str(colors.3.as_str()).unwrap());
     message_frame.set_label_font(Font::by_name(font_family.as_str()));
     message_frame.set_label_size(message.2);

-
-
     wind1.draw(move |f| {
         draw::set_draw_color(Color::from_hex_str(colors.0.as_str()).unwrap());
-        draw::draw_rounded_rectf(border.0 - 1, border.0 -1 , f.w() - border.0 *2 +2, f.h() - border.0 * 2 +2, border.1);
+        draw::draw_rounded_rectf(
+            border.0 - 1,
+            border.0 - 1,
+            f.w() - border.0 * 2 + 2,
+            f.h() - border.0 * 2 + 2,
+            border.1,
+        );
     });

     wind1.handle(|_, event| {
@@ -76,40 +95,53 @@ pub fn ui(
         }
     });

-    let (tx, rx) = mpsc::channel::<(String, String, Option<u64>)>();
+    let (tx, rx) = mpsc::channel::<(String, String, u64)>();
+    tx.send((data.0, data.1, data.2))
+        .unwrap();
+    let socket_path = "/tmp/pino-check.sock";
+
+    if Path::new(socket_path).exists() {
+        std::fs::remove_file(socket_path).unwrap();
+    }
+
     thread::spawn(move || {
-        let filename = "/tmp/pino-check";
-
-        let (mut current_title,mut current_message,mut current_delay) = (String::new(),String::new(),None);
-        let mut modified = metadata(filename).unwrap().modified().unwrap();
-        let mut batata = true; 
-
-        loop {
-            if modified != metadata(filename).unwrap().modified().unwrap() || batata{
-                batata = false;
-                modified = metadata(filename).unwrap().modified().unwrap(); 
-
-                let content = fs::read_to_string(filename).unwrap();
-                let lines: Vec<&str> = content.lines().collect();
-                let new_title = lines.first().unwrap_or(&"").to_string();
-                let new_message = lines.get(1).unwrap_or(&"").to_string();
-                let new_delay = lines.get(2).and_then(|s| s.parse::<u64>().ok());
-                if new_title != current_title || new_message != current_message || new_delay != current_delay {
-                    tx.send((new_title.clone(), new_message.clone(), new_delay)).unwrap();
-                    current_title = new_title;
-                    current_message = new_message;
-                    current_delay = new_delay;
+        let listener = UnixListener::bind(socket_path).unwrap();
+
+        for stream in listener.incoming() {
+            match stream {
+                Ok(mut stream) => {
+                    let mut buffer = [0; 1024];
+                    let n = stream.read(&mut buffer).unwrap();
+                    let received = String::from_utf8_lossy(&buffer[..n]).to_string();
+
+                    let parts: Vec<String> = received
+                        .split("|+|")
+                        .map(|item| item.replace('\n', ""))
+                        .collect();
+                    if parts.len() >= 3 {
+                        let title = parts[0].to_string();
+                        let message = parts[1].to_string();
+                        let delay = parts[2].parse::<u64>().unwrap_or_else(|_| {
+                            eprintln!("Invalid delay value, using 3s");
+                            3
+                        });
+                        tx.send((title, message, delay)).unwrap();
+                    }
+                }
+                Err(e) => {
+                    eprintln!("Error accepting connection: {}", e);
+                    break;
                 }
-            };
-            thread::sleep(Duration::from_millis(600));
+            }
         }
     });

-    let mut close_timer: Option<Instant> = None;
-    let mut current_delay_secs: Option<u64> = None;
-
     wind1.set_override();
     wind1.show();
+    wind2.show();
+
+    let mut close_timer: Option<Instant> = None;
+    let mut current_delay_secs: Option<u64> = None;

     while app.wait() {
         if let Ok((title, message, delay)) = rx.try_recv() {
@@ -121,9 +153,9 @@ pub fn ui(
             }
             wind2.redraw();

-            if delay != current_delay_secs {
-                current_delay_secs = delay;
-                close_timer = delay.map(|secs| Instant::now() + Duration::from_secs(secs));
+            if current_delay_secs.map(|d| d != delay).unwrap_or(true) {
+                current_delay_secs = Some(delay);
+                close_timer = Some(Instant::now() + Duration::from_secs(delay));
             }
         }

@@ -134,6 +166,7 @@ pub fn ui(
             }
         }
     }
+    if Path::new(socket_path).exists() {
+        std::fs::remove_file(socket_path).unwrap();
+    }
 }
-
-
diff --git a/src/utils.rs b/src/utils.rs
deleted file mode 100644
index 85796b4..0000000
--- a/src/utils.rs
+++ /dev/null
@@ -1,18 +0,0 @@
-use std::process::{Command, Stdio};
-
-pub fn is_running(name: &str) -> bool {
-    let output = Command::new("pgrep")
-        .arg("-x")
-        .arg(name)
-        .stdout(Stdio::piped())  
-        .stderr(Stdio::null())   
-        .output();
-
-    match output {
-        Ok(output) if output.status.success() => {
-            let pids = String::from_utf8_lossy(&output.stdout);
-            pids.lines().count() == 2
-        },
-        _ => false 
-    }
-}