Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.

Commit 3cadb80

Browse files
committed
Add hide command and use indoc
Should resolve #18
1 parent 6c44aee commit 3cadb80

File tree

4 files changed

+73
-14
lines changed

4 files changed

+73
-14
lines changed

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@ log = { version = "0.4.14", optional = true }
2828
simplelog = { version = "0.11.0", optional = true }
2929

3030
regex = "1.5.4"
31-
winapi = { version = "0.3.9", features = ["wincon", "consoleapi"] }
31+
winapi = { version = "0.3.9", features = ["wincon", "consoleapi", "shellapi"] }
32+
systrayx = "0.4.1"
33+
34+
indoc = "1.0.3"
35+
36+
# When winsafe matures, maybe winapi can be removed
3237

3338
[features]
3439
default = ["runner", "logging"]

assets/run.ico

4.19 KB
Binary file not shown.

src/input.rs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::sys::{runlua::runLua, statics::*};
2-
use std::path::Path;
2+
use std::{path::Path, io::Read};
33

44
pub(crate) fn try_process_input() -> std::io::Result<()> {
55
// Loop forever in this thread, since it is separate from Gmod, and take in user input.
@@ -40,15 +40,50 @@ pub(crate) fn try_process_input() -> std::io::Result<()> {
4040
}
4141
},
4242

43+
"hide" => unsafe {
44+
use std::sync::atomic::{AtomicPtr, Ordering};
45+
use winapi::um::{
46+
wincon::GetConsoleWindow,
47+
winuser::{ShowWindow, SW_HIDE, SW_SHOW},
48+
};
49+
50+
let wind = GetConsoleWindow();
51+
ShowWindow(wind, SW_HIDE);
52+
53+
let mut tray = systrayx::Application::new().unwrap();
54+
tray.set_icon_from_buffer(
55+
&include_bytes!("../assets/run.ico")[..],
56+
32,
57+
32
58+
).expect("Failed to set icon");
59+
60+
let ptr = AtomicPtr::new(wind);
61+
62+
tray.add_menu_item("Open", move |x| {
63+
let a = ptr.load(Ordering::Relaxed);
64+
ShowWindow(a, SW_SHOW);
65+
66+
x.quit();
67+
Ok::<_, systrayx::Error>(())
68+
})
69+
.unwrap();
70+
71+
tray.wait_for_message().unwrap();
72+
},
73+
4374
"help" => {
44-
println!("Commands list:");
45-
println!("lua_run_cl <code> | Runs lua code on the currently loaded lua state. Will print if any errors occur.");
46-
println!("lua_openscript_cl <file_dir> | Runs a lua script located at file_dir, this dir being an absolute directory on your pc. (Not relative)");
75+
indoc::printdoc! {"
76+
[Commands]
77+
78+
lua_run_cl <code> | Runs lua code on the currently loaded lua state. Will print if any errors occur.
79+
lua_openscript_cl <file_dir> | Runs a lua script located at file_dir, this dir being an absolute directory on your pc. (Not relative)
4780
48-
println!("lua_run_menu <code> | Runs lua code in the menu state. Will print if any errors occur.");
49-
println!("lua_openscript_menu <code> | Runs lua code in the menu state. Will print if any errors occur.");
81+
lua_run_menu <code> | Runs lua code in the menu state. Will print if any errors occur.
82+
lua_openscript_menu <code> | Runs lua code in the menu state. Will print if any errors occur.
5083
51-
println!("help | Prints this out.");
84+
help | Prints this out.
85+
hide | Hides the console, but remains active.
86+
"};
5287
}
5388
_ => (),
5489
}

src/lib.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ mod sys; // Configs
2222
static SENDER: OnceCell<mpsc::SyncSender<()>> = OnceCell::new();
2323

2424
use winapi::{
25-
shared::minwindef::TRUE,
25+
shared::minwindef::{DWORD, FALSE, TRUE},
2626
um::{
27-
consoleapi::AllocConsole,
27+
consoleapi::{AllocConsole, SetConsoleCtrlHandler},
2828
errhandlingapi::GetLastError,
29-
wincon::{FreeConsole, GetConsoleWindow},
29+
wincon::{
30+
FreeConsole, GetConsoleWindow, CTRL_CLOSE_EVENT, CTRL_C_EVENT, CTRL_SHUTDOWN_EVENT,
31+
},
32+
winuser::{
33+
CreateMenu, DeleteMenu, GetSystemMenu, ShowWindow, MF_BYCOMMAND, SC_CLOSE, SW_HIDE,
34+
SW_SHOW,
35+
},
3036
},
3137
};
3238

@@ -54,12 +60,25 @@ fn init() -> Result<(), InitializeError> {
5460
detours::init()?;
5561
}
5662

57-
debug!("Initialized.");
58-
println!("<---> Autorun-rs <--->");
59-
println!("Type [help] for the list of commands");
63+
indoc::printdoc! {"
64+
[Autorun-rs] v{version}
65+
Type 'help' for the list of commands
66+
", version = env!("CARGO_PKG_VERSION")};
6067

6168
let (sender, receiver) = mpsc::sync_channel(1);
6269

70+
// Disable close button and shortcuts
71+
// If you want to hide the console use the ``hide`` command.
72+
// Currently no way to halt Autorun w/o closing gmod
73+
unsafe {
74+
let window = GetConsoleWindow();
75+
let menu = GetSystemMenu(window, FALSE);
76+
77+
DeleteMenu(menu, SC_CLOSE as u32, MF_BYCOMMAND);
78+
79+
SetConsoleCtrlHandler(None, TRUE);
80+
}
81+
6382
thread::spawn(move || loop {
6483
use mpsc::TryRecvError::*;
6584
if input::try_process_input().is_ok() {

0 commit comments

Comments
 (0)