Skip to content

Commit

Permalink
feat(pager-keybinds): added support for keybinds and smooth transitio…
Browse files Browse the repository at this point in the history
…ning between terminal and pager
  • Loading branch information
231tr0n committed Mar 3, 2024
1 parent a7791de commit 930050d
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ module.exports = {
{
ignoreWarnings: true
}
]
],
'svelte/no-at-html-tags': ['warn']
}
}
]
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"svelte-check": "^3.6.6",
"tslib": "^2.6.2",
"typescript": "^5.3.3",
"unlighthouse": "^0.11.3",
"vite": "^5.1.4",
"vitest": "^1.3.1"
},
Expand Down
81 changes: 44 additions & 37 deletions src/lib/components/Terminal.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
let fitTermAddon: FitAddon;
let resizeObserver: ResizeObserver;
let mutationObserver: MutationObserver;
let markDownHtml = 'Loading';
let termThemeDark: ITheme = {
brightBlack: '#383a42',
black: '#383a42',
Expand Down Expand Up @@ -159,33 +160,6 @@
' '
);
};
// TODO: Get the key listeners working on pager.
let pagerKeyListener = (e: KeyboardEvent) => {
console.log(e.key);
if (e.key == 'q' || e.key == 'Q') {
pagerElem.style.display = 'none';
termElem.style.display = 'block';
termElem.focus();
term.focus();
return;
}
if (e.key == 'j' || e.key == 'J') {
pagerElem.scrollBy({
top: 10,
left: 0,
behavior: 'smooth'
});
return;
}
if (e.key == 'k' || e.key == 'K') {
pagerElem.scrollBy({
top: -10,
left: 0,
behavior: 'smooth'
});
return;
}
};
onMount(async () => {
marked.use(
Expand Down Expand Up @@ -247,8 +221,31 @@
fitTermAddon.fit();
// TODO: Implement all commands and pager element functionality.
term.onData(async (e) => {
if (pagerElem.style.display == 'block') {
if (e == 'q' || e == 'Q') {
pagerElem.style.display = 'none';
markDownHtml = 'Loading';
return;
}
if (e == 'j' || e == 'J') {
pagerElem.scrollBy({
top: 50,
left: 0,
behavior: 'smooth'
});
return;
}
if (e == 'k' || e == 'K') {
pagerElem.scrollBy({
top: -50,
left: 0,
behavior: 'smooth'
});
return;
}
return;
}
if (e == '\r') {
const cmds = typedPrompt.split(' ');
let pwdContents = fileSystem;
Expand Down Expand Up @@ -312,10 +309,10 @@
if (pwdContents[file]) {
if (typeof pwdContents[file] === 'string') {
pagerElem.style.display = 'block';
pagerElem.focus();
termElem.style.display = 'none';
const data: string = await (await fetch(pwdContents[file] as string)).text();
pagerElem.innerHTML = dompurify.sanitize(await marked.parse(data)) as string;
markDownHtml = 'Loading';
markDownHtml = dompurify.sanitize(
await marked.parse(await (await fetch(pwdContents[file] as string)).text())
) as string;
break;
}
throw new Error(file + ' is a directory');
Expand All @@ -330,9 +327,7 @@
'pwd: Prints out the present working directory',
'cd: Used to change or navigate through directories',
'clear: Clear terminal screen',
'less: Pager to view files',
'find: Used to find files',
'grep: Used to search for content'
'less: Pager to view files'
].join('\r\n')
);
break;
Expand Down Expand Up @@ -376,6 +371,7 @@
mutationObserver.observe(document.body, {
attributes: true
});
term.focus();
});
onDestroy(() => {
Expand All @@ -385,8 +381,19 @@
});
</script>

<div class="terminal" bind:this={termElem}></div>
<div class="pager" on:keydown={pagerKeyListener} bind:this={pagerElem}></div>
<div class="terminal" bind:this={termElem} on:blur={() => term.focus()}></div>
<div
class="pager"
bind:this={pagerElem}
on:focus={() => term.focus()}
on:scrollend={() => term.focus()}
on:scroll={() => term.focus()}
on:click={() => term.focus()}
on:dblclick={() => term.focus()}
on:select={() => term.focus()}
>
{@html markDownHtml}
</div>

<style>
.terminal {
Expand Down
5 changes: 5 additions & 0 deletions static/images/clipboard-check-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions static/images/clipboard-check-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions static/images/clipboard-dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions static/images/clipboard-light.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 930050d

Please sign in to comment.