Skip to content

Commit

Permalink
feat(ui): add raw output mode
Browse files Browse the repository at this point in the history
Only show errors, requests and raw messages
  • Loading branch information
tagoro9 committed Dec 23, 2022
1 parent 86ab900 commit d2a3f53
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/io/messenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface Message {
detail?: string;
emoji?: Emoji;
message: string;
showInRawMode?: boolean;
showSpinner: boolean;
type: MessageType;
}
Expand Down Expand Up @@ -69,8 +70,8 @@ export class Messenger {
}

@boundMethod
public emit(message: string, emoji?: Emoji): void {
this.subject.next({ emoji, message, type: MessageType.INFO, showSpinner: true });
public emit(message: string, emoji?: Emoji, showInRawMode?: boolean): void {
this.subject.next({ emoji, message, type: MessageType.INFO, showInRawMode, showSpinner: true });
}

@boundMethod
Expand Down
6 changes: 5 additions & 1 deletion src/ui/Fotingo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function Fotingo({
messenger,
programStartTime,
showFooter = true,
useRawOutput = false,
}: FotingoProps): JSX.Element {
const { executionTime, isDone, isInThread, messages, request, sendRequestData } = useCmd(
messenger,
Expand All @@ -27,9 +28,12 @@ export function Fotingo({
isRequesting={request !== undefined}
isInThread={isInThread}
messages={messages}
useRawOutput={useRawOutput}
/>
{request && <InputRequest request={request} onSubmit={sendRequestData} />}
{!request && executionTime && showFooter && <Footer executionTime={executionTime} />}
{!request && executionTime && showFooter && !useRawOutput && (
<Footer executionTime={executionTime} />
)}
</>
);
}
26 changes: 21 additions & 5 deletions src/ui/Message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ const MESSAGE_TYPE_TO_EMOJI: Record<MessageProps['message']['type'], string | un
status: undefined,
};

export function Message({ isDone = false, isLast = false, message }: MessageProps): JSX.Element {
return (
<Box>
export function Message({
isDone = false,
isLast = false,
message,
useRawOutput = false,
}: MessageProps): JSX.Element {
const spinnerOrEmoji =
useRawOutput && message.type !== 'error' ? undefined : (
<Box>
{!isDone && message.showSpinner && isLast ? (
<Box marginRight={1}>
Expand All @@ -29,11 +34,22 @@ export function Message({ isDone = false, isLast = false, message }: MessageProp
</Text>
)}
</Box>
);
const messageColor = useRawOutput || message.type !== 'error' ? undefined : 'red';
const messageDetailColor = useRawOutput || message.type !== 'request' ? undefined : 'gray';
const wrap = useRawOutput ? 'end' : undefined;
return (
<Box>
{spinnerOrEmoji}
<Box>
<Text color={message.type === 'error' ? 'red' : undefined}>{message.message}</Text>
<Text color={messageColor} wrap={wrap}>
{message.message}
</Text>
{message.detail && (
<Box marginLeft={1}>
<Text color={message.type === 'request' ? 'gray' : undefined}>{message.detail}</Text>
<Text color={messageDetailColor} wrap={wrap}>
{message.detail}
</Text>
</Box>
)}
</Box>
Expand Down
13 changes: 9 additions & 4 deletions src/ui/Messages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ export function Messages({
isInThread,
isRequesting,
messages,
useRawOutput = false,
}: MessagesProps): JSX.Element {
const pastMessages = init(messages);
const lastMessage = last(messages);
const actualMessages = messages.filter(
(message) =>
message.showInRawMode || ['request', 'error'].includes(message.type) || !useRawOutput,
);
const pastMessages = init(actualMessages);
const lastMessage = last(actualMessages);
const staticMessages =
isDebugging && lastMessage !== undefined ? [...pastMessages, lastMessage] : pastMessages;
return (
<Box flexDirection="column">
<Static items={staticMessages}>
{(message, id) => <Message key={id} message={message} />}
{(message, id) => <Message key={id} message={message} useRawOutput={useRawOutput} />}
</Static>
{lastMessage && !isDebugging && !isRequesting && !isInThread && (
<Message isDone={isDone} isLast={true} message={lastMessage} />
<Message isDone={isDone} isLast={true} message={lastMessage} useRawOutput={useRawOutput} />
)}
</Box>
);
Expand Down
3 changes: 3 additions & 0 deletions src/ui/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface MessageProps {
isDone?: boolean;
isLast?: boolean;
message: Omit<Message, 'message'> & { message: string | Element };
useRawOutput?: boolean;
}

export interface MessagesProps {
Expand All @@ -23,6 +24,7 @@ export interface MessagesProps {
isInThread: boolean;
isRequesting: boolean;
messages: Array<Omit<Message, 'message'> & { message: string | Element }>;
useRawOutput?: boolean;
}

export interface FotingoProps {
Expand All @@ -31,4 +33,5 @@ export interface FotingoProps {
messenger: Messenger;
programStartTime: number;
showFooter?: boolean;
useRawOutput?: boolean;
}

0 comments on commit d2a3f53

Please sign in to comment.