Skip to content

Commit 8dac197

Browse files
window center method
1 parent 48d2fed commit 8dac197

6 files changed

Lines changed: 56 additions & 5 deletions

File tree

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ollieos",
3-
"version": "1.1.0",
3+
"version": "1.1.1",
44
"description": "",
55
"private": true,
66
"main": "server.js",

src/programs/window/center.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import {ANSI} from "../../term_ctl";
2+
import { ProgramMainData } from "../../types"
3+
4+
// extract from ANSI to make code less verbose
5+
const { STYLE, FG, PREFABS } = ANSI;
6+
export const center_subcommand = async (data: ProgramMainData) => {
7+
// extract from data to make code less verbose
8+
const { args, term } = data;
9+
10+
// remove subcommand name
11+
args.shift();
12+
13+
// get the window id to center
14+
if (args.length === 0) {
15+
term.writeln(`${PREFABS.error}Missing window ID.`)
16+
term.writeln(`Try 'window -h' for more information.${STYLE.reset_all}`);
17+
return 1;
18+
}
19+
20+
const window_id = parseInt(args[0], 10);
21+
if (isNaN(window_id)) {
22+
term.writeln(`${PREFABS.error}Invalid window ID '${args[0]}'. Window ID must be an integer.`)
23+
term.writeln(`Try 'window list' to see all open windows.${STYLE.reset_all}`);
24+
return 1;
25+
}
26+
27+
const wm = term.get_window_manager();
28+
const wind = wm!.get_window_by_id(window_id);
29+
30+
if (!wind) {
31+
term.writeln(`${PREFABS.error}No window found with ID '${window_id}'.`)
32+
term.writeln(`Try 'window list' to see all open windows.${STYLE.reset_all}`);
33+
return 1;
34+
}
35+
36+
term.writeln(`Centering window with ID ${FG.cyan}${window_id}${STYLE.reset_all}.`);
37+
wind.center();
38+
39+
return 0;
40+
}

src/programs/window/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {list_subcommand} from "./list";
66
import {show_subcommand} from "./show";
77
import {hide_subcommand} from "./hide";
88
import {close_subcommand} from "./close";
9+
import {center_subcommand} from "./center";
910

1011
// extract from ANSI to make code less verbose
1112
const {STYLE, PREFABS} = ANSI;
@@ -23,15 +24,16 @@ export default {
2324
"show": `Shows a window by its ID: ${PREFABS.program_name}window${STYLE.reset_all + STYLE.italic} show <window_id>${STYLE.reset_all}`,
2425
"hide": `Hides a window by its ID: ${PREFABS.program_name}window${STYLE.reset_all + STYLE.italic} hide <window_id>${STYLE.reset_all}`,
2526
"close": `Closes a window by its ID: ${PREFABS.program_name}window${STYLE.reset_all + STYLE.italic} close <window_id>${STYLE.reset_all}. Note that this does not terminate the process that opened the window.`,
27+
"center": `Centers a window by its ID: ${PREFABS.program_name}window${STYLE.reset_all + STYLE.italic} show <window_id>${STYLE.reset_all}`,
2628
},
2729
"Arguments:": {
2830
"-h": "Displays this help message.",
2931
"For list:": {
3032
"-v": "List only visible windows.",
3133
"-i": "List only invisible (minimised/hidden) windows.",
3234
},
33-
"For show, hide, and close:": {
34-
"<window_id>": "The ID of the window to show, hide, or close.",
35+
"For show, hide, close, and center:": {
36+
"<window_id>": "The ID of the window.",
3537
}
3638
}
3739
},
@@ -66,6 +68,8 @@ export default {
6668
return await hide_subcommand(data);
6769
case "close":
6870
return await close_subcommand(data);
71+
case "center":
72+
return await center_subcommand(data);
6973
default:
7074
term.writeln(`${PREFABS.error}Invalid subcommand.`);
7175
term.writeln(`Try 'window -h' for more information.${STYLE.reset_all}`);

src/window_impl/dom.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ export class DOMWindowManager extends AbstractWindowManager {
323323
this._window_root.style.top = css_pos;
324324
}
325325

326+
center() {
327+
this.x = `calc(calc(100vw - ${this.width}) / 2)`;
328+
this.y = `calc(calc(100vh - ${this.height}) / 2)`;
329+
}
330+
326331
get dom() {
327332
return this._shadow_dom;
328333
}

src/windowing.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export abstract class AbstractWindow {
3232
abstract get y(): string | number;
3333
abstract set y(css_pos: string | number);
3434

35+
abstract center(): void;
36+
3537
abstract get visible(): boolean;
3638
abstract set visible(is_visible: boolean);
3739

0 commit comments

Comments
 (0)