Skip to content
Open
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
58 changes: 55 additions & 3 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ chrono = {features = ["serde"], version = "0.4"}
thiserror = "2.0"
clap_complete = "4.5.66"

[target.'cfg(target_os = "windows")'.dependencies]
windows = { version = "0.62.2", features = ["Win32", "Win32_System", "Win32_System_Console", "Win32_UI_WindowsAndMessaging"] }

[profile.release]
lto = "thin"
codegen-units = 1
Expand Down
12 changes: 12 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,18 @@ pub struct Cli {

#[arg(long, value_name = "SHELL", value_enum)]
pub completions: Option<Shell>,

#[cfg(target_os = "windows")]
#[arg(long, help = "Spawn as a borderless fullscreen window")]
pub fullscreen: bool,

#[cfg(target_os = "windows")]
#[arg(
long,
help = "Don't respawn into conhost when --fullscreen is enabled",
hide = true
)]
pub forked: bool,
}

pub fn extract_simulate_missing_value(err: clap::Error) -> clap::Error {
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl Config {
toml::Value::try_into(value).map_err(ConfigError::ParseError)
}

fn get_config_path() -> Result<PathBuf, ConfigError> {
pub fn get_config_path() -> Result<PathBuf, ConfigError> {
let config_dir = dirs::config_dir()
.or_else(|| dirs::home_dir().map(|h| h.join(".config")))
.ok_or(ConfigError::NoConfigDir)?;
Expand Down
32 changes: 31 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ mod render;
mod scene;
mod weather;

#[cfg(target_os = "windows")]
mod screensaver;

use clap::{CommandFactory, Parser};
use clap_complete::generate;
use cli::Cli;
Expand Down Expand Up @@ -38,7 +41,20 @@ async fn main() -> io::Result<()> {
default_hook(info);
}));

let cli = match Cli::try_parse() {
#[cfg(target_os = "windows")]
let cli = {
if screensaver::windows::is_screensaver() {
screensaver::windows::init_screensaver()?; // fysa: this function prepares a terminal
Cli::try_parse_from(screensaver::windows::normalize_args())
} else {
Cli::try_parse()
}
};

#[cfg(not(target_os = "windows"))]
let cli = Cli::try_parse();

let cli = match cli {
Ok(cli) => cli,
Err(err) => {
let err = cli::extract_simulate_missing_value(err);
Expand All @@ -56,6 +72,15 @@ async fn main() -> io::Result<()> {
return Ok(());
}

#[cfg(target_os = "windows")]
{
if cli.fullscreen && cli.forked {
screensaver::windows::make_full_screen()?;
} else if cli.fullscreen {
screensaver::windows::relaunch_in_conhost();
}
}

let mut config = match Config::load() {
Ok(config) => config,
Err(e) => {
Expand Down Expand Up @@ -201,6 +226,11 @@ async fn main() -> io::Result<()> {
}
};

#[cfg(target_os = "windows")]
if cli.fullscreen {
println!("Press alt+enter twice to return the terminal to normal.");
}

renderer.cleanup()?;

if let Err(e) = result {
Expand Down
142 changes: 142 additions & 0 deletions src/screensaver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
//! Screensaver support

#[cfg(target_os = "windows")]
pub mod windows {
use std::{env, process::Command};

use windows::Win32::Foundation::HWND;
use windows::Win32::System::Console::{
CONSOLE_SCREEN_BUFFER_INFO, COORD, GetConsoleScreenBufferInfo, GetConsoleWindow,
GetStdHandle, STD_OUTPUT_HANDLE, SetConsoleScreenBufferSize,
};
use windows::Win32::UI::WindowsAndMessaging::{
GWL_STYLE, GetWindowLongW, SW_MAXIMIZE, SetWindowLongW, ShowWindow, WS_CAPTION,
WS_MAXIMIZEBOX, WS_MINIMIZEBOX, WS_SYSMENU, WS_THICKFRAME,
};

use crate::config::Config;

pub fn is_screensaver() -> bool {
let binding = env::args().collect::<Vec<String>>();
let args = binding.first().unwrap();
args.contains(&".scr".to_string())
}

pub fn normalize_args() -> Vec<String> {
env::args()
.map(|arg| {
if let Some(rest) = arg.strip_prefix('/') {
if rest.len() == 1 && rest.chars().next().unwrap().is_ascii_alphabetic() {
match rest {
"S" | "s" | "c" => "--fullscreen".to_string(),

_ => {
format!("-{rest}")
}
}
} else {
arg
}
} else {
arg
}
})
.collect()
}

/// Only useful in Windows 11, cannot run inside Windows Terminal
/// This handles all the logic for handling default CLI arguments for `.scr` files
/// Also includes logic for relaunching in conhost
pub fn init_screensaver() -> windows::core::Result<()> {
let args = normalize_args();

let cfg_path = Config::get_config_path().unwrap();

if args.len() == 1 {
println!(
"Opening config file in system text editor: {}",
cfg_path.display()
);
Command::new("notepad.exe")
.arg(cfg_path)
.spawn()
.expect("Failed to open config file in system text editor");
std::process::exit(1);
}

if let Some(config) = args.get(1)
&& config.contains("/c:")
{
println!("Waiting for notepad to close...");
Command::new("notepad.exe")
.arg(cfg_path)
.output()
.expect("Failed to open config file in system text editor");
}

if !args.contains(&"--forked".to_string()) {
relaunch_in_conhost();
} // Always relaunch in conhost - Best way to avoid Win Terminal in Win11
// make_full_screen()

Ok(())
}

/// Makes any console window fullscreen
/// In Windows 11 this creates some rather weird artifacts due to `Windows Terminal`
/// But it works fine in Windows 10, as a result ensure conhost.exe is the owner of the console
pub fn make_full_screen() -> windows::core::Result<()> {
unsafe {
let hwnd: HWND = GetConsoleWindow();

let style = GetWindowLongW(hwnd, GWL_STYLE);

let new_style = style
& !(WS_CAPTION.0 as i32
| WS_THICKFRAME.0 as i32
| WS_MINIMIZEBOX.0 as i32
| WS_MAXIMIZEBOX.0 as i32
| WS_SYSMENU.0 as i32);

SetWindowLongW(hwnd, GWL_STYLE, new_style);

if !hwnd.0.is_null() {
ShowWindow(hwnd, SW_MAXIMIZE).unwrap();
} else {
println!("No console window found.");
}

let handle = GetStdHandle(STD_OUTPUT_HANDLE)?;

let mut info = CONSOLE_SCREEN_BUFFER_INFO::default();
GetConsoleScreenBufferInfo(handle, &mut info)?;

let window_width = info.srWindow.Right - info.srWindow.Left + 1;
let window_height = info.srWindow.Bottom - info.srWindow.Top + 1;

SetConsoleScreenBufferSize(
handle,
COORD {
X: window_width,
Y: window_height,
},
)?;
}

Ok(())
}

/// Only useful in Windows 11, cannot run inside Windows Terminal
pub fn relaunch_in_conhost() -> ! {
let mut args = normalize_args();

args.push("--forked".to_string());

Command::new("conhost")
.args(args)
.spawn()
.expect("Failed to relaunch in conhost");

std::process::exit(0);
}
}
2 changes: 1 addition & 1 deletion src/weather/provider/supplementary/aad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ struct SunData {

impl SunData {
fn get_time(&self) -> String {
self.time.clone().replace(" ST", "") // Unsure what ST stands for, but its not needed
self.time.clone().replace(" ST", "").replace(" DT", "") // Figured out what ST and DT mean (Standard Time & Daylight Time)
}

fn to_chrono_time(&self) -> Result<NaiveTime, WeatherError> {
Expand Down