MCPcopy Create free account
hub / github.com/anus-dev/ANUS / useShellHistory

Function useShellHistory

packages/cli/src/ui/hooks/useShellHistory.ts:71–134  ·  view source on GitHub ↗
(projectRoot: string)

Source from the content-addressed store, hash-verified

69}
70
71export function useShellHistory(projectRoot: string): UseShellHistoryReturn {
72 const [history, setHistory] = useState<string[]>([]);
73 const [historyIndex, setHistoryIndex] = useState(-1);
74 const [historyFilePath, setHistoryFilePath] = useState<string | null>(null);
75
76 useEffect(() => {
77 async function loadHistory() {
78 const filePath = await getHistoryFilePath(projectRoot);
79 setHistoryFilePath(filePath);
80 const loadedHistory = await readHistoryFile(filePath);
81 setHistory(loadedHistory.reverse()); // Newest first
82 }
83 loadHistory();
84 }, [projectRoot]);
85
86 const addCommandToHistory = useCallback(
87 (command: string) => {
88 if (!command.trim() || !historyFilePath) {
89 return;
90 }
91 const newHistory = [command, ...history.filter((c) => c !== command)]
92 .slice(0, MAX_HISTORY_LENGTH)
93 .filter(Boolean);
94 setHistory(newHistory);
95 // Write to file in reverse order (oldest first)
96 writeHistoryFile(historyFilePath, [...newHistory].reverse());
97 setHistoryIndex(-1);
98 },
99 [history, historyFilePath],
100 );
101
102 const getPreviousCommand = useCallback(() => {
103 if (history.length === 0) {
104 return null;
105 }
106 const newIndex = Math.min(historyIndex + 1, history.length - 1);
107 setHistoryIndex(newIndex);
108 return history[newIndex] ?? null;
109 }, [history, historyIndex]);
110
111 const getNextCommand = useCallback(() => {
112 if (historyIndex < 0) {
113 return null;
114 }
115 const newIndex = historyIndex - 1;
116 setHistoryIndex(newIndex);
117 if (newIndex < 0) {
118 return '';
119 }
120 return history[newIndex] ?? null;
121 }, [history, historyIndex]);
122
123 const resetHistoryPosition = useCallback(() => {
124 setHistoryIndex(-1);
125 }, []);
126
127 return {
128 history,

Callers 2

InputPromptFunction · 0.85

Calls 2

loadHistoryFunction · 0.85
writeHistoryFileFunction · 0.85

Tested by

no test coverage detected