#[cfg(not(windows))]
compile_error!("this crate is designed for Windows platforms only");
#[macro_use]
extern crate lazy_static;
struct NLog;
static NLOG: NLog = NLog;
impl log::Log for NLog {
fn enabled(&self, metadata: &log::Metadata) -> bool {
metadata.level() <= log::max_level()
}
fn log(&self, record: &log::Record) {
if !self.enabled(record.metadata()) {
return
}
let message = format!("[{:5}] {}\n", record.level(), record.args());
nlog(&message);
}
fn flush(&self) { }
}
pub fn init(level: log::LevelFilter) -> Result<(), log::SetLoggerError> {
log::set_logger(&NLOG)
.map(|()| log::set_max_level(level))
}
fn nlog(message: &str) {
use std::sync::Mutex;
use std::ptr::null_mut as NULL;
use winapi::um::winuser::{FindWindowA, FindWindowExA, SendMessageW, GetWindowTextLengthW, EM_GETSEL, EM_SETSEL, EM_REPLACESEL};
use winapi::shared::minwindef::DWORD;
lazy_static! {
static ref NLOG_BUFFER: Mutex<Vec<u16>> = Mutex::default();
}
let notepad = unsafe {
FindWindowA(NULL(), b"Untitled - Notepad\0".as_ptr() as _)
};
if notepad.is_null() {
return
}
let edit = unsafe {
FindWindowExA(notepad, NULL(), b"EDIT\0".as_ptr() as _, NULL())
};
if edit.is_null() {
return
}
use std::ffi::OsStr;
use std::os::windows::ffi::OsStrExt;
let mut buf = NLOG_BUFFER.lock().unwrap();
buf.clear();
buf.extend(OsStr::new(message).encode_wide().filter(|&c| c != 0));
buf.push(0);
unsafe {
let mut sel_a: DWORD = 0;
let mut sel_z: DWORD = 0;
let len = GetWindowTextLengthW(edit);
SendMessageW(edit, EM_GETSEL as _, &mut sel_a as *mut _ as _, &mut sel_z as *mut _ as _);
SendMessageW(edit, EM_SETSEL as _, len as _, len as _);
SendMessageW(edit, EM_REPLACESEL as _, 1 as _, buf.as_ptr() as _);
SendMessageW(edit, EM_SETSEL as _, sel_a as _, sel_z as _);
}
}