Skip to content

Commit

Permalink
general: checks for usage outside of a terminal on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
BrettMayson committed Dec 19, 2024
1 parent f823a0c commit 053b38b
Show file tree
Hide file tree
Showing 8 changed files with 165 additions and 95 deletions.
179 changes: 91 additions & 88 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions bin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ zip = { workspace = true }

[target.'cfg(windows)'.dependencies]
enable-ansi-support = "0.2.1"
winapi = { version = "0.3.9", features = ["winuser"] }
winreg = "0.52.0"

[dev-dependencies]
Expand Down
8 changes: 8 additions & 0 deletions bin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ use clap::Parser;
use hemtt_workspace::reporting::WorkspaceFiles;
use tracing::error;

#[cfg(windows)]
mod windows_message;

fn main() {
#[cfg(windows)]
if !hemtt::is_ci() && std::env::args().count() == 1 {
windows_message::check_no_terminal();
}

std::panic::set_hook(Box::new(|panic| {
error!("{panic}");
eprintln!(
Expand Down
57 changes: 57 additions & 0 deletions bin/src/windows_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use winapi::{
shared::minwindef::DWORD,
um::{
wincon::{GetConsoleProcessList, GetConsoleWindow},
winuser::{MessageBoxW, IDYES, MB_ICONQUESTION, MB_YESNO},
},
};

pub fn check_no_terminal() {
if is_console_created_for_this_program() {
message();
std::process::exit(1);
}
}

fn is_console_created_for_this_program() -> bool {
unsafe {
// Check if there is a console window
if GetConsoleWindow().is_null() {
// No console window is attached
return false;
}

// Get the number of processes attached to the console
let mut process_list: [DWORD; 2] = [0; 2];
let process_count =
GetConsoleProcessList(process_list.as_mut_ptr(), process_list.len() as DWORD);

// If only one process is attached, the console was likely created for this program
process_count == 1
}
}

fn message() {
let message = "This is a command-line tool. To use it, open a terminal and run it there.\n\nWould you like to open the documentation?";
let title = "CLI Tool - Information";

// Convert strings to wide strings for the Windows API
let message_wide: Vec<u16> = message.encode_utf16().chain(Some(0)).collect();
let title_wide: Vec<u16> = title.encode_utf16().chain(Some(0)).collect();

// Show the message box with Yes/No buttons
let response = unsafe {
MessageBoxW(
std::ptr::null_mut(),
message_wide.as_ptr(),
title_wide.as_ptr(),
MB_YESNO | MB_ICONQUESTION,
)
};

if response == IDYES {
if let Err(e) = webbrowser::open("https://hemtt.dev/") {
eprintln!("Failed to open the HEMTT book: {e}");
}
}
}
4 changes: 2 additions & 2 deletions hls/package-lock.json

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

4 changes: 3 additions & 1 deletion hls/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@
{
"language": "sqf",
"scopes": {
"property.function": ["entity.name.function.sqf"]
"property.function": [
"entity.name.function.sqf"
]
}
}
],
Expand Down
4 changes: 2 additions & 2 deletions libs/preprocessor/src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ pub fn file(path: &WorkspacePath) -> Result<Vec<Arc<Token>>, Error> {
}

/// Parse a string into tokens
///
///
/// # Errors
/// If the string is invalid
///
///
/// # Panics
/// If the string is invalid
pub fn str(source: &str, path: &WorkspacePath) -> Result<Vec<Arc<Token>>, Error> {
Expand Down
3 changes: 1 addition & 2 deletions libs/preprocessor/src/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ impl Processor {

processor.file_stack.push(path.clone());

let tokens =
crate::parse::file(path).map_err(|e| (processor.included_files.clone(), e))?;
let tokens = crate::parse::file(path).map_err(|e| (processor.included_files.clone(), e))?;
let mut pragma = Pragma::root();
let mut buffer = Vec::with_capacity(tokens.len());
let mut stream = tokens.into_iter().peekmore();
Expand Down

0 comments on commit 053b38b

Please sign in to comment.