-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnotification.rs
More file actions
76 lines (72 loc) · 2.68 KB
/
Copy pathnotification.rs
File metadata and controls
76 lines (72 loc) · 2.68 KB
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
use crate::{StdFunction, StdlibModule, StdlibRegistry};
use std::collections::HashMap;
use std::rc::Rc;
use techscript_runtime::{error::RuntimeError, value::RuntimeValue};
impl StdlibRegistry {
pub fn register_notification(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
HashMap::new();
exports.insert(
"show".to_string(),
Rc::new(StdFunction {
name: "show".to_string(),
arity: 2,
callback: |_ctx, args| {
let title = args[0].to_string();
let body = args[1].to_string();
#[cfg(target_os = "windows")]
{
use std::process::Command;
let _ = Command::new("powershell")
.args([
"-Command",
&format!(
"[System.Windows.MessageBox]::Show('{}','{}')",
body.replace("'", "''"),
title.replace("'", "''")
),
])
.spawn();
}
#[cfg(not(target_os = "windows"))]
{
let _ = std::process::Command::new("notify-send")
.args([&title, &body])
.spawn();
}
Ok(RuntimeValue::Null)
},
}),
);
exports.insert(
"alert".to_string(),
Rc::new(StdFunction {
name: "alert".to_string(),
arity: 1,
callback: |_ctx, args| {
let msg = args[0].to_string();
#[cfg(target_os = "windows")]
{
let _ = std::process::Command::new("msg").args(["*", &msg]).spawn();
}
#[cfg(not(target_os = "windows"))]
{
let _ = std::process::Command::new("notify-send")
.args(["Alert", &msg])
.spawn();
}
Ok(RuntimeValue::Null)
},
}),
);
self.register_module(
"std.notification",
StdlibModule {
name: "std.notification".to_string(),
version: "1.0.0".to_string(),
exports,
required_capabilities: Vec::new(),
},
);
}
}