Skip to content

Commit

Permalink
refactor: add type guard for osd size
Browse files Browse the repository at this point in the history
  • Loading branch information
4ndrs committed Jan 4, 2023
1 parent 82c7b0f commit 84049f3
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
19 changes: 8 additions & 11 deletions src/cropbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { MouseProperties, VideoProperties } from "./properties";
import { printMessage } from "./utils";
import PureBox from "./purebox";
import { Box, MousePos, SetBox } from "./types";
import { Box, MousePos, OSDSize, SetBox } from "./types";

class CropBox {
pureBox!: PureBox;
Expand Down Expand Up @@ -75,11 +75,7 @@ class CropBox {
throw new Error("Unable to get the video's properties");
}

if (
osdSize === undefined ||
osdSize.width === undefined ||
osdSize.height === undefined
) {
if (!isOSDSize(osdSize)) {
throw new Error("Unable to get the OSD sizes");
}

Expand Down Expand Up @@ -171,11 +167,7 @@ const drawBox = () => {

const osdSize = mp.get_osd_size();

if (
osdSize !== undefined &&
osdSize.width !== undefined &&
osdSize.height !== undefined
) {
if (isOSDSize(osdSize)) {
({ width: overlay.res_x, height: overlay.res_y } = osdSize);
} else {
mp.msg.error(
Expand Down Expand Up @@ -233,6 +225,11 @@ const calculateBox = (mousePos: MousePos) => {
const isMousePos = (value: unknown): value is MousePos =>
(value as MousePos)?.x !== undefined && (value as MousePos)?.y !== undefined;

const isOSDSize = (value: mp.OSDSize | undefined): value is OSDSize =>
typeof value !== "undefined" &&
typeof value?.width === "number" &&
typeof value?.height === "number";

const boxIsSet = (box: Box): box is SetBox =>
typeof box.w === "number" &&
typeof box.h === "number" &&
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export interface SetBox {
y: number;
}

export interface OSDSize {
width: number;
height: number;
}

export type Selection = "primary" | "clipboard";
export type CopyUtility = "detect" | "xclip" | "wl-copy";
export type CopyMode = "ffmpeg" | "purewebm";

0 comments on commit 84049f3

Please sign in to comment.