-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added support for commands and going back in history
- Loading branch information
1 parent
a7ce9bb
commit 390aee8
Showing
15 changed files
with
525 additions
and
26 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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; | ||
import { tomorrow } from 'react-syntax-highlighter/dist/esm/styles/prism'; | ||
|
||
export type PrismProps = { | ||
className?: string; | ||
children: string | string[]; | ||
}; | ||
|
||
export function Prism({ className, children }: PrismProps) { | ||
return ( | ||
<div className={className}> | ||
<SyntaxHighlighter | ||
language="shell" | ||
style={tomorrow} | ||
customStyle={{ backgroundColor: 'transparent', padding: 0, margin: 0, font: 'inherit', fontSize: 'inherit' }} | ||
codeTagProps={{ | ||
style: { | ||
font: 'inherit', | ||
}, | ||
}} | ||
lineProps={{ | ||
style: { | ||
font: 'inherit', | ||
}, | ||
}} | ||
> | ||
{children} | ||
</SyntaxHighlighter> | ||
</div> | ||
); | ||
} |
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
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,2 +1,3 @@ | ||
.shell { | ||
font-family: 'Roboto Mono', monospace; | ||
} |
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,9 +1,14 @@ | ||
import { Window } from '../web-osx/Window'; | ||
import { WSHInput } from './WSHInput'; | ||
|
||
export type WSHProps = { | ||
demo?: boolean; | ||
}; | ||
|
||
export function WSH({ demo }: WSHProps) { | ||
return <Window preventEvents={demo} />; | ||
return ( | ||
<Window preventEvents={demo}> | ||
<WSHInput /> | ||
</Window> | ||
); | ||
} |
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,37 @@ | ||
.container { | ||
position: relative; | ||
font-family: 'Roboto Mono', monospace; | ||
font-size: 16px; | ||
margin-left: 20px; | ||
line-height: 1.5; | ||
|
||
.prism { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
right: 0; | ||
bottom: 0; | ||
pointer-events: none; | ||
} | ||
|
||
&:before { | ||
content: '$'; | ||
user-select: none; | ||
position: absolute; | ||
top: 0; | ||
left: -20px; | ||
} | ||
|
||
.input { | ||
color: transparent; | ||
caret-color: white; | ||
background: transparent; | ||
padding: 0; | ||
border: none; | ||
width: 100%; | ||
height: 100%; | ||
outline: none; | ||
font: inherit; | ||
resize: none; | ||
} | ||
} |
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,65 @@ | ||
import { KeyboardEvent, useCallback, useState } from 'react'; | ||
import * as styles from './WSHInput.module.scss'; | ||
import { History } from '../../services/history'; | ||
import { Prism } from '../common/Prism'; | ||
|
||
export type WSHInputProps = { | ||
onSubmit?: (value: string) => void; | ||
}; | ||
|
||
export function WSHInput({ onSubmit }: WSHInputProps) { | ||
const [value, setValue] = useState(''); | ||
|
||
const onSubmitInternal = useCallback( | ||
(e: KeyboardEvent<HTMLTextAreaElement>, value: string, ignore: boolean = false) => { | ||
e.preventDefault(); | ||
|
||
if (!ignore) { | ||
History.add(value); | ||
} | ||
|
||
onSubmit?.(value); | ||
setValue(''); | ||
}, | ||
[onSubmit] | ||
); | ||
|
||
return ( | ||
<div className={styles.container}> | ||
<Prism className={styles.prism}>{value}</Prism> | ||
<textarea | ||
className={styles.input} | ||
spellCheck={false} | ||
value={value} | ||
onChange={({ target }) => setValue(target.value)} | ||
onKeyDown={(e) => { | ||
const { metaKey, ctrlKey, key } = e; | ||
|
||
if (metaKey && key === 'k') { | ||
// CMD + K - Clears out the terminal window | ||
onSubmitInternal(e, 'clear', true); | ||
} else if (ctrlKey && key === 'u') { | ||
// Ctrl + U - Clears out the current line | ||
e.preventDefault(); | ||
|
||
setValue(''); | ||
} else if (key === 'ArrowUp') { | ||
// Up - Navigates to the prior history | ||
e.preventDefault(); | ||
|
||
setValue(History.next()); | ||
} else if (key === 'ArrowDown') { | ||
// Up - Navigates to more current history | ||
e.preventDefault(); | ||
|
||
setValue(History.previous() ?? ''); | ||
} else if (key === 'Enter') { | ||
// Enter - Submits the current input | ||
|
||
onSubmitInternal(e, value); | ||
} | ||
}} | ||
/> | ||
</div> | ||
); | ||
} |
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
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
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
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,71 @@ | ||
import { ScopedStorage } from './storage'; | ||
// import { Bubbles } from '../dynamic/utils-bubbles.js'; | ||
|
||
export class ScopedHistory<T extends string> { | ||
private storage: ScopedStorage<T>; | ||
index: number | null = null; | ||
private _list: string[]; | ||
|
||
constructor(scope: T) { | ||
this.storage = new ScopedStorage(scope); | ||
this._list = []; | ||
} | ||
|
||
add(command: string): void { | ||
this.list.unshift(command); | ||
this.store(this.list); | ||
this.reset(); | ||
} | ||
|
||
next(): string { | ||
if (this.index === null) { | ||
this.index = 0; | ||
} else if (this.index >= this.list.length - 1) { | ||
this.index = this.list.length - 1; | ||
// Bubbles.Instance.add({ | ||
// message: `We've hit the edge of the universe, nothing to see here...`, | ||
// }); | ||
} else { | ||
this.index = this.index + 1; | ||
} | ||
|
||
return this.list[this.index]; | ||
} | ||
|
||
previous(): string | undefined { | ||
if (this.index === null || this.index <= -1) { | ||
this.index = -1; | ||
// Bubbles.Instance.add({ | ||
// message: `We'll you've got to start somewhere!`, | ||
// }); | ||
} else { | ||
this.index = this.index - 1; | ||
} | ||
|
||
return this.list[this.index]; | ||
} | ||
|
||
reset(): void { | ||
this.index = null; | ||
} | ||
|
||
clear(): void { | ||
this._list = []; | ||
this.store(this._list); | ||
} | ||
|
||
store(history: string[]): void { | ||
this.storage.set('history', JSON.stringify(history)); | ||
} | ||
|
||
get list(): string[] { | ||
if (!this._list) { | ||
this._list = JSON.parse(this.storage.get('history') || '[]'); | ||
this.store(this._list); | ||
} | ||
|
||
return this._list; | ||
} | ||
} | ||
|
||
export const History = new ScopedHistory('utils.rains.cafe'); |
Oops, something went wrong.