Skip to content

Commit

Permalink
moves projects to tslua
Browse files Browse the repository at this point in the history
  • Loading branch information
jakenvac committed Nov 7, 2022
1 parent 25b4711 commit 2eb0fcf
Show file tree
Hide file tree
Showing 11 changed files with 541 additions and 126 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
.DS_Store
node_modules
dist/
14 changes: 14 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "wezmode-ts",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts":{
"build": "tstl --luaBundle wezmode.lua --luaBundleEntry ./src/wm.ts -lt JIT --outDir ./dist/"
},
"devDependencies": {
"lua-types": "^2.13.0",
"typescript": "^4.8.4",
"typescript-to-lua": "^1.10.1"
}
}
21 changes: 21 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
type mergable = { [key: string]: mergable | string | number | boolean }

const isObject = (o: any): o is mergable => {
return typeof o === "object"
}

export const mergeObjects = <T extends mergable, P extends mergable>(
o1: T,
o2: P
): T => {
Object.keys(o2).forEach((k) => {
const v1 = o1[k];
const v2 = o2[k];
if (isObject(v1) && isObject(v2)) {
o1 = Object.assign(o1, {[k]:mergeObjects(v1, v2)});
} else {
o1 = Object.assign(o1, {[k]: o2[k]});
}
});
return o1;
};
159 changes: 159 additions & 0 deletions src/wm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
import type { Modifier, Key, KeyBind } from "wezterm";
import * as wezterm from "wezterm";

import { mergeObjects } from "./helpers";

type describedKeyBind = KeyBind & {
desc: string;
};

type mode = {
name: string;
key: Key;
modeColor: string;
keyTable: describedKeyBind[];
one_shot?: boolean;
until_unknown?: boolean;
prevent_fallback?: boolean;
};

type wezmodeOpts = {
modifier: Modifier;
hintSeparator: string;
theme: {
normalModeColor: string;
hintColor: string;
modeTextColor: string;
textColor: string;
};
};

type wezmodeState = {
keys: KeyBind[];
modeTexts: Record<string, string>;
keyTables: Record<string, describedKeyBind[]>;
};

const state: wezmodeState = {
keys: [],
modeTexts: {},
keyTables: {},
};

const defaultOpts: wezmodeOpts = {
modifier: "CTRL",
hintSeparator: "/",
theme: {
normalModeColor: "red",
hintColor: "green",
modeTextColor: "black",
textColor: "white",
},
};

const createPrefixText = (prefix: string, textColor: string) => {
return wezterm.format([
{ Attribute: { Intensity: "Bold" } },
{ Foreground: { Color: textColor } },
{ Text: prefix },
]);
};

const createHintText = (
key: Key,
desc: string,
hintColor: string,
textColor: string
) => {
return wezterm.format([
{ Attribute: { Intensity: "Bold" } },
{ Foreground: { Color: textColor } },
{ Text: "<" },
{ Foreground: { Color: hintColor } },
{ Text: key },
{ Foreground: { Color: textColor } },
{ Text: `> ${desc}` },
]);
};

const createModeText = (name: string, textColor: string, modeColor: string) => {
return wezterm.format([
{ Attribute: { Intensity: "Bold" } },
{ Foreground: { Color: textColor } },
{ Background: { Color: modeColor } },
{ Text: ` ${name.toUpperCase()} MODE ` },
]);
};

const setup = (modes: mode[], opts?: Partial<wezmodeOpts>) => {
const options = mergeObjects(defaultOpts, opts ?? {});

const modeHints = modes
.map((m) =>
createHintText(
m.key,
m.name,
options.theme.hintColor,
options.theme.textColor
)
)
.join(` ${options.hintSeparator} `);

state.modeTexts["normal"] = `${createPrefixText(
options.modifier,
options.theme.normalModeColor
)} + ${modeHints} ${createModeText("normal", options.theme.modeTextColor, options.theme.normalModeColor)}`;

modes.forEach((m) => {
state.keys.push({
key: m.key,
mods: options.modifier,
action: wezterm.action.ActivateKeyTable({
name: m.name,
one_shot: !!m.one_shot,
until_unknown: !!m.until_unknown,
prevent_fallback: !!m.prevent_fallback,
}),
});

state.keyTables[m.name] = m.keyTable;

const actionHints = m.keyTable
.map((k) =>
createHintText(
k.key,
k.desc,
options.theme.hintColor,
options.theme.textColor
)
)
.join(` ${options.hintSeparator} `);

state.modeTexts[m.name] = `${actionHints} ${createModeText(
m.name,
options.theme.modeTextColor,
m.modeColor
)}`;
});
};

const getModeText = (modeName: string) => state.modeTexts[modeName];
const getKeys = () => state.keys;
const getKeyTables = () => state.keyTables;
const handleRightStatusUpdate = () => {
wezterm.on("update-right-status", (window) => {
window.set_right_status(getModeText(window.active_key_table() ?? "normal"));
});
};
const mergeTables = mergeObjects;
const extendTable = (t1: any[], t2: any[]) => t1.concat(t2);

export {
setup,
getModeText,
getKeys,
getKeyTables,
handleRightStatusUpdate,
mergeTables,
extendTable,
};
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"include": ["./src"],
"compilerOptions": {
"target": "esnext",
"lib": ["esnext"],
"moduleResolution": "node",
"types": [],
"strict": true,
"types": ["lua-types/jit", "./types/wezterm"],
},
"tstl": {
"luaTarget": "JIT",
"noImplicitSelf": true
}
}
9 changes: 9 additions & 0 deletions types/wezterm/actions.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
declare module "wezterm/coreTypes/actionFuncs" {
export type Action = () => void;
export type ActivateKeyTable = (opts: {
name: string,
one_shot: boolean,
until_unknown: boolean,
prevent_fallback: boolean
}) => Action;
}
Loading

0 comments on commit 2eb0fcf

Please sign in to comment.