Skip to content

Commit

Permalink
feat: cmd line argument alias --ls for --ls-win (see #220) (#222)
Browse files Browse the repository at this point in the history
* refactor: move margin and PlatformApi into their own modules
* feat: implement cmd line alias for `--ls` same as `--ls-win` (see #220)
  • Loading branch information
sassman authored Jan 14, 2025
1 parent c594839 commit 4c666be
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 78 deletions.
1 change: 1 addition & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub fn launch() -> ArgMatches {
.required(false)
.short('l')
.long("ls-win")
.long("ls")
.help("If you want to see a list of windows available for recording by their id, you can set env var 'WINDOWID' or `--win-id` to record this specific window only"),
)
.arg(
Expand Down
66 changes: 66 additions & 0 deletions src/common/margin.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#[derive(Debug, PartialEq, Eq)]
pub struct Margin {
pub top: u16,
pub right: u16,
pub bottom: u16,
pub left: u16,
}

impl Margin {
pub fn new(top: u16, right: u16, bottom: u16, left: u16) -> Self {
Self {
top,
right,
bottom,
left,
}
}

pub fn new_equal(margin: u16) -> Self {
Self::new(margin, margin, margin, margin)
}

pub fn zero() -> Self {
Self::new_equal(0)
}

pub fn is_zero(&self) -> bool {
self.top == 0
&& self.right == self.left
&& self.left == self.bottom
&& self.bottom == self.top
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn margin_new() {
let m = Margin::new(1, 2, 3, 4);
assert_eq!(m.top, 1);
assert_eq!(m.right, 2);
assert_eq!(m.bottom, 3);
assert_eq!(m.left, 4);
}

#[test]
fn margin_new_equal() {
let m = Margin::new_equal(1);
assert_eq!(m.top, 1);
assert_eq!(m.right, 1);
assert_eq!(m.bottom, 1);
assert_eq!(m.left, 1);
}

#[test]
fn margin_zero() {
let m = Margin::zero();
assert_eq!(m.top, 0);
assert_eq!(m.right, 0);
assert_eq!(m.bottom, 0);
assert_eq!(m.left, 0);
assert!(m.is_zero());
}
}
82 changes: 4 additions & 78 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,82 +1,8 @@
pub mod identify_transparency;
pub mod image;
mod margin;
mod platform_api;
pub mod utils;

use crate::{ImageOnHeap, Result, WindowId, WindowList};

pub trait PlatformApi: Send {
/// 1. it does check for the screenshot
/// 2. it checks for transparent margins and configures the api
/// to cut them away in further screenshots
fn calibrate(&mut self, window_id: WindowId) -> Result<()>;
fn window_list(&self) -> Result<WindowList>;
fn capture_window_screenshot(&self, window_id: WindowId) -> Result<ImageOnHeap>;
fn get_active_window(&self) -> Result<WindowId>;
}

#[derive(Debug, PartialEq, Eq)]
pub struct Margin {
pub top: u16,
pub right: u16,
pub bottom: u16,
pub left: u16,
}

impl Margin {
pub fn new(top: u16, right: u16, bottom: u16, left: u16) -> Self {
Self {
top,
right,
bottom,
left,
}
}

pub fn new_equal(margin: u16) -> Self {
Self::new(margin, margin, margin, margin)
}

pub fn zero() -> Self {
Self::new_equal(0)
}

pub fn is_zero(&self) -> bool {
self.top == 0
&& self.right == self.left
&& self.left == self.bottom
&& self.bottom == self.top
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn margin_new() {
let m = Margin::new(1, 2, 3, 4);
assert_eq!(m.top, 1);
assert_eq!(m.right, 2);
assert_eq!(m.bottom, 3);
assert_eq!(m.left, 4);
}

#[test]
fn margin_new_equal() {
let m = Margin::new_equal(1);
assert_eq!(m.top, 1);
assert_eq!(m.right, 1);
assert_eq!(m.bottom, 1);
assert_eq!(m.left, 1);
}

#[test]
fn margin_zero() {
let m = Margin::zero();
assert_eq!(m.top, 0);
assert_eq!(m.right, 0);
assert_eq!(m.bottom, 0);
assert_eq!(m.left, 0);
assert!(m.is_zero());
}
}
pub use margin::*;
pub use platform_api::*;
11 changes: 11 additions & 0 deletions src/common/platform_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use crate::{ImageOnHeap, Result, WindowId, WindowList};

pub trait PlatformApi: Send {
/// 1. it does check for the screenshot
/// 2. it checks for transparent margins and configures the api
/// to cut them away in further screenshots
fn calibrate(&mut self, window_id: WindowId) -> Result<()>;
fn window_list(&self) -> Result<WindowList>;
fn capture_window_screenshot(&self, window_id: WindowId) -> Result<ImageOnHeap>;
fn get_active_window(&self) -> Result<WindowId>;
}

0 comments on commit 4c666be

Please sign in to comment.