Skip to content

Commit 38647e6

Browse files
author
David Sexton
committed
feat(history): add clearAllBuffers and strip ANSI from messages
- Add clearBuffer() and clearAllBuffers() to channel history - clearAllBuffers resets to initial state (only "all" buffer, empty) - Connect clearAllBuffers to clear log button - Strip ANSI codes from messages before storing in "all" buffer
1 parent ca76e1a commit 38647e6

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

src/App.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function App() {
6969
const [guestCount, setGuestCount] = useState(0);
7070
const [showJoinDialog, setShowJoinDialog] = useState(false);
7171
const connectToRoomRef = useRef<((targetRoomId: string) => void) | null>(null);
72-
useChannelHistory(client);
72+
const { clearAllBuffers } = useChannelHistory(client);
7373
const players = useClientEvent<"userlist">(client, "userlist", []) || [];
7474
const outRef = React.useRef<OutputWindow | null>(null);
7575
const inRef = React.useRef<HTMLTextAreaElement | null>(null);
@@ -90,6 +90,7 @@ function App() {
9090
if (outRef.current) {
9191
outRef.current.clearLog();
9292
}
93+
clearAllBuffers();
9394
};
9495

9596
const copyLog = () => {

src/hooks/useChannelHistory.tsx

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useEffect, useRef, useState } from "react";
22
import { announce } from "@react-aria/live-announcer";
3+
import Anser from "anser";
34
import MudClient from "../client";
45
import { preferencesStore, NavigationKeyScheme } from "../PreferencesStore";
56

@@ -132,7 +133,9 @@ export const useChannelHistory = (client: MudClient | null) => {
132133
// Handle regular messages
133134
const handleMessage = (message: string) => {
134135
if (!message.trim()) return;
135-
addMessageToBuffer("all", message);
136+
// Strip ANSI codes before storing
137+
const plainText = Anser.ansiToText(message);
138+
addMessageToBuffer("all", plainText);
136139
};
137140

138141
// Handle channel messages
@@ -372,6 +375,28 @@ export const useChannelHistory = (client: MudClient | null) => {
372375
});
373376
};
374377

378+
const clearBuffer = (bufferName: string) => {
379+
setBuffers(prev => {
380+
const newBuffers = new Map(prev);
381+
const buffer = newBuffers.get(bufferName);
382+
if (buffer) {
383+
newBuffers.set(bufferName, {
384+
...buffer,
385+
messages: [],
386+
currentIndex: 0,
387+
});
388+
}
389+
return newBuffers;
390+
});
391+
};
392+
393+
const clearAllBuffers = () => {
394+
// Delete all buffers except "all", then clear "all"
395+
setBuffers(new Map([["all", { name: "all", messages: [], currentIndex: 0 }]]));
396+
setBufferOrder(["all"]);
397+
setCurrentBufferIndex(0);
398+
};
399+
375400
// Global keyboard handler
376401
useEffect(() => {
377402
const handleKeyDown = (e: KeyboardEvent) => {
@@ -546,5 +571,6 @@ export const useChannelHistory = (client: MudClient | null) => {
546571
currentBufferIndex,
547572
bufferOrder,
548573
getCurrentBuffer,
574+
clearAllBuffers,
549575
};
550576
};

0 commit comments

Comments
 (0)