Skip to content

Commit

Permalink
Add theme switcher command
Browse files Browse the repository at this point in the history
  • Loading branch information
pindab0ter committed Apr 12, 2024
1 parent 4ba9835 commit 605ef20
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
2 changes: 2 additions & 0 deletions assets/ts/terminal/Terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Help } from "./commands/Help";
import { Kitties } from "./commands/Kitties";
import { List } from "./commands/List";
import { AutocompletingCommand } from "./AutocompletingCommand";
import { Theme } from "./commands/Theme";

export class Terminal {
private inputElement = document.getElementById("prompt-input") as HTMLSpanElement;
Expand All @@ -20,6 +21,7 @@ export class Terminal {
new Help(),
new Kitties(),
new List(),
new Theme(),
];

public initialise(): void {
Expand Down
37 changes: 37 additions & 0 deletions assets/ts/terminal/commands/Theme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Command } from "../Command";
import { Terminal } from "../Terminal";
import { setDarkMode } from "../../darkMode";
import { Theme as ThemeType } from "../../types/main";
import { AutocompletingCommand } from "../AutocompletingCommand";

export class Theme extends AutocompletingCommand {
public readonly name: string = "theme";
public readonly description: string =
"Set the theme of the website. Available themes: light, dark, system";
private readonly validThemes = ["light", "dark", "system"];

public execute(terminal: Terminal, args: string[]): void {
if (args.length < 1) {
terminal.print("theme: missing arguments");
return;
}

if (args.length > 1) {
terminal.print("theme: too many arguments");
return;
}

const theme = args[0];

if (!this.validThemes.includes(theme)) {
terminal.print("theme: unknown theme");
return;
}

setDarkMode(theme as ThemeType);
}

suggestAutocompletions(arg: string): string[] {
return this.validThemes.filter((name: string) => name.startsWith(arg));
}
}

0 comments on commit 605ef20

Please sign in to comment.