Skip to content

Commit 3e6970c

Browse files
committed
refactor: commands
1 parent 09765b1 commit 3e6970c

File tree

3 files changed

+66
-63
lines changed

3 files changed

+66
-63
lines changed

src/commands/mod.rs

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use crate::delayed::Delayed;
2+
use serde::Deserialize;
3+
use std::{process::Output, sync::Mutex};
4+
5+
#[derive(Debug, Deserialize, Clone)]
6+
#[serde(rename_all = "lowercase")]
7+
pub enum Command {
8+
Unlock,
9+
Lock,
10+
String(String),
11+
}
12+
13+
// fixme: don't use sudo, use proper permissions
14+
// fixme: use dbus to lock/unlock
15+
static LOCKED: Mutex<bool> = Mutex::new(false);
16+
static DELAYED_LOCK: Mutex<Option<Delayed>> = Mutex::new(None);
17+
18+
impl Command {
19+
pub fn run(&self) -> anyhow::Result<()> {
20+
let locked = *LOCKED.lock().unwrap();
21+
match self {
22+
Command::Unlock => {
23+
// cancel the delayed lock
24+
if let Some(delayed) = &mut *DELAYED_LOCK.lock().unwrap() {
25+
delayed.cancel();
26+
}
27+
if locked {
28+
println!("Unlocking desktop...");
29+
run("sudo loginctl unlock-sessions")?;
30+
*LOCKED.lock().unwrap() = false;
31+
};
32+
}
33+
34+
// fixme: unlocking might not be good idea if it wasn't locked automatically
35+
Command::Lock => {
36+
let duration = std::time::Duration::from_secs(15);
37+
if !locked {
38+
println!("Locking desktop in {:?}", duration);
39+
if let Some(delayed) = &mut *DELAYED_LOCK.lock().unwrap() {
40+
delayed.cancel();
41+
}
42+
// wait before actually locking the desktop
43+
let delayed = Delayed::new(duration, || async {
44+
run("sudo loginctl lock-sessions").expect("error running lock command");
45+
*LOCKED.lock().unwrap() = true;
46+
});
47+
*DELAYED_LOCK.lock().unwrap() = Some(delayed);
48+
}
49+
}
50+
Command::String(cmd) => {
51+
run(cmd)?;
52+
}
53+
};
54+
Ok(())
55+
}
56+
}
57+
58+
pub fn run(cmd: &str) -> anyhow::Result<Output> {
59+
let output = std::process::Command::new("sh")
60+
.arg("-c")
61+
.arg(cmd)
62+
.output()?;
63+
Ok(output)
64+
}

src/config.rs

+1-63
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
use crate::delayed::Delayed;
1+
use crate::commands::Command;
22
use figment::{
33
providers::{Env, Format, Toml},
44
Figment,
55
};
66
use serde::Deserialize;
7-
use std::{process::Output, sync::Mutex};
87

98
#[derive(Deserialize, Debug)]
109
pub struct Config {
@@ -74,14 +73,6 @@ impl BLEConnection {
7473
}
7574
}
7675

77-
pub fn run(cmd: &str) -> anyhow::Result<Output> {
78-
let output = std::process::Command::new("sh")
79-
.arg("-c")
80-
.arg(cmd)
81-
.output()?;
82-
Ok(output)
83-
}
84-
8576
#[derive(Deserialize, Debug, Clone)]
8677
#[serde(tag = "type")]
8778
#[serde(rename_all = "lowercase")]
@@ -97,59 +88,6 @@ pub struct ProximityAction {
9788
pub command: Command,
9889
}
9990

100-
#[derive(Debug, Deserialize, Clone)]
101-
#[serde(rename_all = "lowercase")]
102-
pub enum Command {
103-
Unlock,
104-
Lock,
105-
String(String),
106-
}
107-
108-
// fixme: don't use sudo, use proper permissions
109-
// fixme: use dbus to lock/unlock
110-
static LOCKED: Mutex<bool> = Mutex::new(false);
111-
static DELAYED_LOCK: Mutex<Option<Delayed>> = Mutex::new(None);
112-
113-
impl Command {
114-
pub fn run(&self) -> anyhow::Result<()> {
115-
let locked = *LOCKED.lock().unwrap();
116-
match self {
117-
Command::Unlock => {
118-
// cancel the delayed lock
119-
if let Some(delayed) = &mut *DELAYED_LOCK.lock().unwrap() {
120-
delayed.cancel();
121-
}
122-
if locked {
123-
println!("Unlocking desktop...");
124-
run("sudo loginctl unlock-sessions")?;
125-
*LOCKED.lock().unwrap() = false;
126-
};
127-
}
128-
129-
// fixme: unlocking might not be good idea if it wasn't locked automatically
130-
Command::Lock => {
131-
let duration = std::time::Duration::from_secs(15);
132-
if !locked {
133-
println!("Locking desktop in {:?}", duration);
134-
if let Some(delayed) = &mut *DELAYED_LOCK.lock().unwrap() {
135-
delayed.cancel();
136-
}
137-
// wait before actually locking the desktop
138-
let delayed = Delayed::new(duration, || async {
139-
run("sudo loginctl lock-sessions").expect("error running lock command");
140-
*LOCKED.lock().unwrap() = true;
141-
});
142-
*DELAYED_LOCK.lock().unwrap() = Some(delayed);
143-
}
144-
}
145-
Command::String(cmd) => {
146-
run(cmd)?;
147-
}
148-
};
149-
Ok(())
150-
}
151-
}
152-
15391
const APP_NAME: &str = "nearby";
15492
pub fn get_config() -> anyhow::Result<Config> {
15593
let config_dir = dirs::config_dir().expect("Could not find config directory");

src/main.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use bluer::{
44
use config::{get_config, Connection};
55
use futures::{pin_mut, stream::SelectAll, StreamExt};
66
use std::collections::HashSet;
7+
mod commands;
78
mod config;
89
mod delayed;
910

0 commit comments

Comments
 (0)