From 89e8a839b4ba96fc6a73907e976f219fd29a6357 Mon Sep 17 00:00:00 2001 From: Aamr El Kazdadi Date: Wed, 18 Mar 2020 04:06:41 +0100 Subject: [PATCH] Allow user provided geometry --- src/bridge/mod.rs | 4 ++-- src/editor/mod.rs | 4 ++-- src/main.rs | 6 ++++++ src/settings.rs | 2 ++ src/window.rs | 44 +++++++++++++++++++++++++++++++++++++++++++- 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/bridge/mod.rs b/src/bridge/mod.rs index dbd09ec..e3e438c 100644 --- a/src/bridge/mod.rs +++ b/src/bridge/mod.rs @@ -17,7 +17,7 @@ use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; use crate::error_handling::ResultPanicExplanation; use crate::settings::*; -use crate::INITIAL_DIMENSIONS; +use crate::window::window_geometry_or_default; pub use events::*; use handler::NeovimHandler; pub use layouts::*; @@ -58,7 +58,7 @@ async fn drain(receiver: &mut UnboundedReceiver) -> Option) { - let (width, height) = INITIAL_DIMENSIONS; + let (width, height) = window_geometry_or_default(); let (mut nvim, io_handler, _) = create::new_child_cmd(&mut create_nvim_command(), NeovimHandler()) .await diff --git a/src/editor/mod.rs b/src/editor/mod.rs index c35f909..9218c5f 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -12,7 +12,7 @@ use unicode_segmentation::UnicodeSegmentation; use crate::bridge::{GridLineCell, GuiOption, RedrawEvent}; use crate::redraw_scheduler::REDRAW_SCHEDULER; -use crate::INITIAL_DIMENSIONS; +use crate::window::window_geometry_or_default; pub use cursor::{Cursor, CursorMode, CursorShape}; pub use grid::CharacterGrid; pub use style::{Colors, Style}; @@ -43,7 +43,7 @@ pub struct Editor { impl Editor { pub fn new() -> Editor { let mut editor = Editor { - grid: CharacterGrid::new(INITIAL_DIMENSIONS), + grid: CharacterGrid::new(window_geometry_or_default()), title: "Neovide".to_string(), font_name: None, font_size: None, diff --git a/src/main.rs b/src/main.rs index 3de08c4..0ef3d68 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,11 +20,17 @@ extern crate lazy_static; use lazy_static::initialize; use bridge::BRIDGE; +use std::process; use window::ui_loop; +use window::window_geometry; pub const INITIAL_DIMENSIONS: (u64, u64) = (100, 50); fn main() { + if let Err(err) = window_geometry() { + eprintln!("{}", err); + process::exit(1); + }; window::initialize_settings(); redraw_scheduler::initialize_settings(); renderer::cursor_renderer::initialize_settings(); diff --git a/src/settings.rs b/src/settings.rs index 56a776e..552cb75 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -138,6 +138,8 @@ impl Settings { if arg == "--log" { log_to_file = true; false + } else if arg.starts_with("--geometry=") { + false } else { true } diff --git a/src/window.rs b/src/window.rs index 34c19a8..c0a1685 100644 --- a/src/window.rs +++ b/src/window.rs @@ -56,6 +56,48 @@ struct WindowWrapper { fullscreen: bool, } +pub fn window_geometry() -> Result<(u64, u64), String> { + let prefix = "--geometry="; + + std::env::args() + .filter(|arg| arg.starts_with(prefix)) + .next() + .map_or(Ok(INITIAL_DIMENSIONS), |arg| { + let input = &arg[prefix.len()..]; + let invalid_parse_err = format!( + "Invalid geometry: {}\nValid format: x", + input + ); + + input + .split('x') + .map(|dimension| { + dimension + .parse::() + .or(Err(invalid_parse_err.as_str())) + .and_then(|dimension| { + if dimension > 0 { + Ok(dimension) + } else { + Err("Invalid geometry: Window dimensions should be greater than 0.") + } + }) + }) + .collect::, &str>>() + .and_then(|dimensions| { + if let [width, height] = dimensions[..] { + Ok((width, height)) + } else { + Err(invalid_parse_err.as_str()) + } + }) + .map_err(|msg| msg.to_owned()) + }) +} +pub fn window_geometry_or_default() -> (u64, u64) { + window_geometry().unwrap_or(INITIAL_DIMENSIONS) +} + impl WindowWrapper { pub fn new() -> WindowWrapper { let context = sdl2::init().expect("Failed to initialize sdl2"); @@ -64,7 +106,7 @@ impl WindowWrapper { .expect("Failed to create sdl video subsystem"); video_subsystem.text_input().start(); - let (width, height) = INITIAL_DIMENSIONS; + let (width, height) = window_geometry_or_default(); let renderer = Renderer::new(); let logical_size = LogicalSize {