-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
541 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
.DS_Store | ||
node_modules | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.