Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

general: checks for usage outside of a terminal on windows #850

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
54 changes: 54 additions & 0 deletions bin/src/windows_message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
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 {
if GetConsoleWindow().is_null() {
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 = "HEMTT is a command-line tool intended to be used in a terminal. To use it, open a terminal (like in Visual Studio Code) and run it there.\n\nWould you like to open The HEMTT Book for more information?";
let title = "HEMTT";

// 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();

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
Loading