Skip to content

Commit

Permalink
feat: use watch mode when executed without console
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Noah committed Jul 10, 2024
1 parent 915d5f4 commit b09939b
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 47 deletions.
117 changes: 91 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ reqwest = { version = "0.12.5", features = ["blocking"] }
serde = { version = "1.0.203", features = ["derive"] }
serde-xml-rs = "0.6.0"

[target.'cfg(windows)'.dependencies.windows]
version = "0.58.0"
features = [
"Win32_Foundation",
"Win32_Security",
"Win32_Storage_FileSystem",
"Win32_System_Console",
"Win32_UI_WindowsAndMessaging",
]

[profile.release]
lto = true
codegen-units = 1
Expand Down
85 changes: 64 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{path::PathBuf, sync::mpsc};
// #![windows_subsystem = "windows"]

use std::{fs, path::PathBuf, sync::mpsc};

use notify::{Config, RecommendedWatcher, Watcher};
use serde::Deserialize;
Expand Down Expand Up @@ -27,8 +29,13 @@ fn main() {
let args: Vec<String> = std::env::args().collect();
let args = &args[1..];

if args.len() == 0 || args[0] == "run" {
run();
if args.len() == 0 {
if has_console_window() {
run();
} else {
hide_console_window();
watch();
}
} else {
match args[0].as_str() {
"help" | "--help" | "-h" => {
Expand Down Expand Up @@ -67,24 +74,8 @@ fn main() {
}
);
}
"watch" => {
run();

let (tx, rx) = mpsc::channel();

let mut watcher = RecommendedWatcher::new(tx, Config::default()).unwrap();

watcher.watch(&get_screenshots_directory(), notify::RecursiveMode::NonRecursive).unwrap();

for event in rx {
match event {
Ok(_) => {
run();
}
Err(e) => eprintln!("Watch error: {:?}", e),
}
}
}
"run" => run(),
"watch" => watch(),
_ => println!("Invalid command."),
}
}
Expand Down Expand Up @@ -125,6 +116,25 @@ fn run() {
}
}

fn watch() {
run();

let (tx, rx) = mpsc::channel();

let mut watcher = RecommendedWatcher::new(tx, Config::default()).unwrap();

watcher.watch(&get_screenshots_directory(), notify::RecursiveMode::NonRecursive).unwrap();

for event in rx {
match event {
Ok(_) => {
run();
}
Err(e) => eprintln!("Watch error: {:?}", e),
}
}
}

fn get_steam_id() -> Option<u64> {
let directories = std::fs::read_dir(r#"C:\Program Files (x86)\Steam\userdata"#).unwrap().collect::<Vec<_>>();

Expand Down Expand Up @@ -189,3 +199,36 @@ fn get_screenshots_directory() -> PathBuf {
.join("Pictures")
.join("Steam Screenshots")
}

#[cfg(target_os = "windows")]
fn has_console_window() -> bool {
use windows::Win32::{System::Console::GetConsoleWindow, UI::WindowsAndMessaging::GetWindowThreadProcessId};

let console = unsafe { GetConsoleWindow() };

if console.is_invalid() {
return false;
}

let mut console_pid = 0;
unsafe { GetWindowThreadProcessId(console, Some(&mut console_pid)) };

console_pid != std::process::id()
}

#[cfg(target_os = "linux")]
fn has_console_window() -> bool {
todo!("has_console_window");
}

#[cfg(target_os = "windows")]
fn hide_console_window() {
use windows::Win32::System::Console::FreeConsole;

unsafe { FreeConsole().unwrap() };
}

#[cfg(target_os = "linux")]
fn hide_console_window() {
todo!("hide_console_window");
}

0 comments on commit b09939b

Please sign in to comment.