diff --git a/src/components/Shell/Shell.tsx b/src/components/Shell/Shell.tsx index 96768ee..ea7a115 100644 --- a/src/components/Shell/Shell.tsx +++ b/src/components/Shell/Shell.tsx @@ -1,13 +1,20 @@ // src/components/Shell/Shell.tsx 'use client'; +import React from 'react'; // hooks import { useShell } from './hooks/useShell'; +import { SyntaxPart } from '@/data/commandSyntaxMap'; interface ShellProps { decreaseCommandsLeft: () => void; } +const InlineHint = ({ part }: { part: SyntaxPart }) => ( + + {" " + part.syntax} + +); export default function Shell({ decreaseCommandsLeft }: ShellProps) { const { handleInputChange, @@ -16,7 +23,9 @@ export default function Shell({ decreaseCommandsLeft }: ShellProps) { inputRef, output, command, + remainingSyntax } = useShell(decreaseCommandsLeft); + return (
))}
-

dice ~$

-
+ dice ~$ +
+
+ {remainingSyntax.map((part, index) => ( + + {index > 0 && ' '} + + + ))} +
-
+
); } + + + + diff --git a/src/components/Shell/hooks/useShell.tsx b/src/components/Shell/hooks/useShell.tsx index 153e51a..e4a53f0 100644 --- a/src/components/Shell/hooks/useShell.tsx +++ b/src/components/Shell/hooks/useShell.tsx @@ -3,7 +3,8 @@ import { useState, useEffect, useRef, KeyboardEvent, ChangeEvent } from 'react'; // utils import { handleCommand } from '@/shared/utils/shellUtils'; -import blacklistedCommands from '@/shared/utils/blacklist'; +import blacklistedCommands from '@/shared/utils/blacklist'; // Assuming you added blacklist here +import { syntaxMap, SyntaxPart } from '@/data/commandSyntaxMap'; export const useShell = (decreaseCommandsLeft: () => void) => { // states @@ -13,6 +14,7 @@ export const useShell = (decreaseCommandsLeft: () => void) => { // Initialise the command history with sessionStorage const [commandHistory, setCommandHistory] = useState([]); const [historyIndex, setHistoryIndex] = useState(-1); + const [remainingSyntax, setRemainingSyntax] = useState([]); // useRefs const terminalRef = useRef(null); @@ -34,6 +36,25 @@ export const useShell = (decreaseCommandsLeft: () => void) => { decreaseCommandsLeft(); // Call to update remaining commands }; + + const updateSyntax = (value: string) => { + const inputParts = value.trim().split(' '); + const command = inputParts[0].toUpperCase(); + if (syntaxMap[command]) { + const parts = syntaxMap[command].parts; + if (inputParts.length === 1) { + // Only command typed, show all parts + setRemainingSyntax(parts); + } else { + // Show remaining parts based on what's already typed + const remainingParts = parts.slice(inputParts.length - 1); + setRemainingSyntax(remainingParts); + } + } else { + setRemainingSyntax([]); + } + }; + useEffect(() => { if (terminalRef.current) { terminalRef.current.scrollTop = terminalRef.current.scrollHeight; @@ -59,9 +80,10 @@ export const useShell = (decreaseCommandsLeft: () => void) => { }, []); const handleInputChange = (e: ChangeEvent) => { - setCommand(e.target.value); - // Save current input when starting to navigate history - setTempCommand(e.target.value); + + const value = e.target.value; + setCommand(value); + updateSyntax(value); }; const handleKeyDown = (e: KeyboardEvent) => { @@ -71,6 +93,7 @@ export const useShell = (decreaseCommandsLeft: () => void) => { setCommandHistory((prev) => [...prev, command]); setHistoryIndex(-1); } + setRemainingSyntax([]) return; } @@ -102,7 +125,7 @@ export const useShell = (decreaseCommandsLeft: () => void) => { } else { setCommand( filteredCommandHistory[ - filteredCommandHistory.length - 1 - newIndex + filteredCommandHistory.length - 1 - newIndex ], ); } @@ -120,5 +143,6 @@ export const useShell = (decreaseCommandsLeft: () => void) => { command, tempCommand, setTempCommand, + remainingSyntax }; }; diff --git a/src/data/commandSyntaxMap.ts b/src/data/commandSyntaxMap.ts new file mode 100644 index 0000000..4bf1a0b --- /dev/null +++ b/src/data/commandSyntaxMap.ts @@ -0,0 +1,56 @@ +export type SyntaxPart = { + syntax: string; + doc: string; + } + + export type CommandSyntax = { + parts: SyntaxPart[]; + } + + export type SyntaxMap = { + [command: string]: CommandSyntax; + } + + export const syntaxMap: SyntaxMap = { + SET: { + parts: [ + { + syntax: 'key', + doc: 'The key under which to store the value' + }, + { + syntax: 'value', + doc: 'The value to be stored' + }, + { + syntax: '[NX | XX]', + doc: 'NX - Only set if key does not exist. XX - Only set if key exists' + }, + { + syntax: '[EX seconds | PX milliseconds | EXAT unix-time-seconds | PXAT unix-time-milliseconds | KEEPTTL]', + doc: 'Options to set the key expiration: EX (seconds), PX (milliseconds), EXAT/PXAT (unix timestamp), or KEEPTTL to retain existing TTL' + } + ] + }, + GET:{ + parts: [ + { + syntax:"Key", + doc:"Key of the value you want to retrive" + } + ] + }, + DEL:{ + parts: [ + { + syntax:"Key", + doc:"Key that you want to delete" + },{ + syntax:"[Key ...]", + doc:"Multiple keys you want to delete" + } + ] + }, + + }; +