Refactor terminal and remove duplicate code.

This commit is contained in:
Ankit Mahato
2025-09-28 13:55:32 +05:30
parent dc7aa246d7
commit 3545c75cd5
21 changed files with 91 additions and 253 deletions

View File

@@ -1,44 +0,0 @@
import 'package:flutter/material.dart';
class EmptyMessage extends StatelessWidget {
const EmptyMessage({
super.key,
this.title = 'No logs yet',
this.subtitle = 'Send a request to view its details in the console.',
this.icon,
});
final String title;
final String subtitle;
final IconData? icon;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (icon != null) ...[
Icon(icon, size: 36, color: theme.colorScheme.outline),
const SizedBox(height: 8),
],
Text(
title,
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 6),
Text(
subtitle,
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.outline,
),
textAlign: TextAlign.center,
),
],
),
);
}
}

View File

@@ -1,121 +0,0 @@
import 'package:flutter/material.dart';
import '../consts.dart';
class TerminalLevelFilterMenu extends StatelessWidget {
const TerminalLevelFilterMenu({
super.key,
required this.selected,
required this.onChanged,
});
final Set<TerminalLevel> selected;
final ValueChanged<Set<TerminalLevel>> onChanged;
void _toggleLevel(TerminalLevel level) {
final next = selected.contains(level)
? (selected.toSet()..remove(level))
: (selected.toSet()..add(level));
onChanged(next);
}
@override
Widget build(BuildContext context) {
final all = TerminalLevel.values.toSet();
return PopupMenuButton<_MenuAction>(
tooltip: 'Filter',
icon: const Icon(Icons.filter_alt),
onSelected: (action) {
switch (action) {
case _MenuAction.toggleDebug:
_toggleLevel(TerminalLevel.debug);
break;
case _MenuAction.toggleInfo:
_toggleLevel(TerminalLevel.info);
break;
case _MenuAction.toggleWarn:
_toggleLevel(TerminalLevel.warn);
break;
case _MenuAction.toggleError:
_toggleLevel(TerminalLevel.error);
break;
case _MenuAction.selectAll:
onChanged(all);
break;
case _MenuAction.clearAll:
onChanged(<TerminalLevel>{});
break;
}
},
itemBuilder: (context) => [
CheckedPopupMenuItem<_MenuAction>(
value: _MenuAction.toggleError,
checked: selected.contains(TerminalLevel.error),
child: const ListTile(
dense: true,
contentPadding: EdgeInsets.zero,
leading: Icon(Icons.error_outline_rounded, color: Colors.red),
title: Text('Errors'),
),
),
CheckedPopupMenuItem<_MenuAction>(
value: _MenuAction.toggleWarn,
checked: selected.contains(TerminalLevel.warn),
child: const ListTile(
dense: true,
contentPadding: EdgeInsets.zero,
leading: Icon(Icons.warning_amber_outlined, color: Colors.amber),
title: Text('Warnings'),
),
),
CheckedPopupMenuItem<_MenuAction>(
value: _MenuAction.toggleInfo,
checked: selected.contains(TerminalLevel.info),
child: const ListTile(
dense: true,
contentPadding: EdgeInsets.zero,
leading: Icon(Icons.info_outline, color: Colors.blue),
title: Text('Info'),
),
),
CheckedPopupMenuItem<_MenuAction>(
value: _MenuAction.toggleDebug,
checked: selected.contains(TerminalLevel.debug),
child: const ListTile(
dense: true,
contentPadding: EdgeInsets.zero,
leading: Icon(Icons.bug_report_outlined),
title: Text('Debug'),
),
),
const PopupMenuDivider(),
PopupMenuItem<_MenuAction>(
value: _MenuAction.selectAll,
child: const ListTile(
dense: true,
contentPadding: EdgeInsets.zero,
leading: Icon(Icons.select_all),
title: Text('Select all'),
),
),
PopupMenuItem<_MenuAction>(
value: _MenuAction.clearAll,
child: const ListTile(
dense: true,
contentPadding: EdgeInsets.zero,
leading: Icon(Icons.clear_all),
title: Text('Clear selection'),
),
),
],
);
}
}
enum _MenuAction {
toggleDebug,
toggleInfo,
toggleWarn,
toggleError,
selectAll,
clearAll,
}

View File

@@ -1,406 +0,0 @@
import 'package:flutter/material.dart';
import 'package:apidash_design_system/apidash_design_system.dart';
import 'package:apidash_core/apidash_core.dart';
import '../consts.dart';
import '../models/terminal/models.dart';
import '../utils/ui_utils.dart';
import 'expandable_section.dart';
import 'highlight_text.dart';
class SystemLogTile extends StatelessWidget {
const SystemLogTile({
super.key,
required this.entry,
this.showTimestamp = false,
this.searchQuery,
});
final TerminalEntry entry;
final bool showTimestamp;
final String? searchQuery;
@override
Widget build(BuildContext context) {
assert(entry.system != null, 'System tile requires SystemLogData');
final s = entry.system!;
final cs = Theme.of(context).colorScheme;
IconData icon;
Color? iconColor;
switch (entry.level) {
case TerminalLevel.error:
icon = Icons.error_outline_rounded;
iconColor = cs.error;
break;
case TerminalLevel.warn:
icon = Icons.warning_amber_rounded;
iconColor = Colors.amber[800];
break;
case TerminalLevel.info:
case TerminalLevel.debug:
icon = Icons.info_outline;
iconColor = cs.primary;
break;
}
final titleStyle = Theme.of(context).textTheme.bodyMedium;
final subStyle = Theme.of(context).textTheme.bodySmall;
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (showTimestamp) ...[
Padding(
padding: const EdgeInsets.only(top: 2, right: 8),
child: Text(_formatTs(entry.ts), style: subStyle),
),
] else ...[
Padding(
padding: const EdgeInsets.only(top: 2, right: 8),
child: Icon(icon, size: 18, color: iconColor),
),
],
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
HighlightedSelectableText(
text: '[${s.category}] ${s.message}',
style: titleStyle,
query: searchQuery,
),
if (s.stack != null && s.stack!.isNotEmpty) ...[
const SizedBox(height: 4),
HighlightedSelectableText(
text: s.stack!,
style: subStyle,
query: searchQuery,
),
],
],
),
),
],
),
);
}
}
class JsLogTile extends StatelessWidget {
const JsLogTile({
super.key,
required this.entry,
this.showTimestamp = false,
this.requestName,
this.searchQuery,
});
final TerminalEntry entry;
final bool showTimestamp;
final String? requestName;
final String? searchQuery;
@override
Widget build(BuildContext context) {
assert(entry.js != null, 'JS tile requires JsLogData');
final cs = Theme.of(context).colorScheme;
final j = entry.js!;
IconData? icon;
Color? iconColor;
Color? bg;
switch (entry.level) {
case TerminalLevel.error:
icon = Icons.error_outline_rounded;
iconColor = cs.error;
bg = Colors.redAccent.shade200.withValues(alpha: 0.2);
break;
case TerminalLevel.warn:
icon = Icons.warning_amber_rounded;
iconColor = Colors.amber[800];
bg = Colors.amberAccent.shade200.withValues(alpha: 0.25);
break;
case TerminalLevel.info:
case TerminalLevel.debug:
break;
}
final bodyParts = <String>[];
if (requestName != null && requestName!.isNotEmpty) {
bodyParts.add('[$requestName]');
}
// Add JS level/context prefix to disambiguate
if (j.context != null && j.context!.isNotEmpty) {
bodyParts.add('(${j.context})');
}
bodyParts.addAll(j.args);
final bodyText = bodyParts.join(' ');
return Container(
color: bg,
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (showTimestamp) ...[
Padding(
padding: const EdgeInsets.only(top: 2, right: 8),
child: Text(_formatTs(entry.ts),
style: Theme.of(context).textTheme.bodySmall),
),
] else if (icon != null) ...[
Padding(
padding: const EdgeInsets.only(top: 2, right: 8),
child: Icon(icon, size: 18, color: iconColor),
),
],
Expanded(
child: HighlightedSelectableText(
text: bodyText,
style: Theme.of(context).textTheme.bodyMedium,
query: searchQuery,
),
),
],
),
);
}
}
class NetworkLogTile extends StatefulWidget {
const NetworkLogTile({
super.key,
required this.entry,
this.showTimestamp = false,
this.requestName,
this.searchQuery,
});
final TerminalEntry entry;
final bool showTimestamp;
final String? requestName;
final String? searchQuery;
@override
State<NetworkLogTile> createState() => _NetworkLogTileState();
}
class _NetworkLogTileState extends State<NetworkLogTile> {
bool _expanded = false;
String? _lastQuery;
@override
void didUpdateWidget(covariant NetworkLogTile oldWidget) {
super.didUpdateWidget(oldWidget);
// Auto-expand if a new non-empty search query matches hidden detail content
final q = widget.searchQuery?.trim();
if (q != null && q.isNotEmpty && q != _lastQuery) {
if (!_expanded && _networkDetailsContainQuery(q)) {
setState(() => _expanded = true);
}
}
_lastQuery = q;
}
bool _networkDetailsContainQuery(String q) {
final n = widget.entry.network!;
final lowerQ = q.toLowerCase();
bool inMap(Map<String, String>? m) =>
m != null &&
m.entries.any((e) =>
e.key.toLowerCase().contains(lowerQ) ||
e.value.toLowerCase().contains(lowerQ));
bool inText(String? t) =>
t != null && t.toLowerCase().contains(lowerQ) && t.isNotEmpty;
return inMap(n.requestHeaders) ||
inMap(n.responseHeaders) ||
inText(n.requestBodyPreview) ||
inText(n.responseBodyPreview) ||
inText(n.errorMessage);
}
@override
Widget build(BuildContext context) {
final n = widget.entry.network!;
final status = n.responseStatus != null ? '${n.responseStatus}' : null;
final duration =
n.duration != null ? '${n.duration!.inMilliseconds} ms' : null;
final bodyStyle = Theme.of(context).textTheme.bodyMedium;
final methodStyle = kCodeStyle.copyWith(
fontWeight: FontWeight.bold,
color: getAPIColor(
APIType.rest,
method: n.method,
brightness: Theme.of(context).brightness,
),
);
return Column(
children: [
InkWell(
onTap: () => setState(() => _expanded = !_expanded),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 10),
child: Row(
children: [
if (widget.showTimestamp) ...[
Padding(
padding: const EdgeInsets.only(right: 8),
child: Text(
_formatTs(widget.entry.ts),
style: Theme.of(context).textTheme.bodySmall,
),
),
],
Expanded(
child: RichText(
text: TextSpan(
style: bodyStyle,
children: [
if (widget.requestName != null &&
widget.requestName!.isNotEmpty) ...[
...buildHighlightedSpans(
'[${widget.requestName}]',
context,
widget.searchQuery,
baseStyle: bodyStyle,
),
const TextSpan(text: ' '),
],
...buildHighlightedSpans(
n.method.name.toUpperCase(),
context,
widget.searchQuery,
baseStyle: methodStyle,
),
const TextSpan(text: ' '),
...buildHighlightedSpans(
n.url,
context,
widget.searchQuery,
baseStyle: bodyStyle,
),
],
),
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
const SizedBox(width: 12),
if (status != null) Text(status),
if (status != null && duration != null) const Text(' | '),
if (duration != null) Text(duration),
const SizedBox(width: 6),
Icon(_expanded
? Icons.keyboard_arrow_up
: Icons.keyboard_arrow_down),
],
),
),
),
if (_expanded) NetworkDetails(n: n, searchQuery: widget.searchQuery),
],
);
}
}
class NetworkDetails extends StatelessWidget {
const NetworkDetails({super.key, required this.n, this.searchQuery});
final NetworkLogData n;
final String? searchQuery;
@override
Widget build(BuildContext context) {
final q = searchQuery?.trim();
bool contains(String? text) =>
q != null &&
q.isNotEmpty &&
text != null &&
text.toLowerCase().contains(q.toLowerCase());
bool mapContains(Map<String, String>? m) =>
q != null &&
q.isNotEmpty &&
m != null &&
m.entries.any((e) =>
e.key.toLowerCase().contains(q.toLowerCase()) ||
e.value.toLowerCase().contains(q.toLowerCase()));
final networkBodyMap = {
'API Type': n.apiType.name,
'Phase': n.phase.name,
if (n.isStreaming) 'Streaming': 'true',
if (n.sentAt != null) 'Sent': n.sentAt!.toIso8601String(),
if (n.completedAt != null) 'Completed': n.completedAt!.toIso8601String(),
if (n.duration != null) 'Duration': '${n.duration!.inMilliseconds} ms',
'URL': n.url,
'Method': n.method.name.toUpperCase(),
if (n.responseStatus != null) 'Status': '${n.responseStatus}',
if (n.errorMessage != null) 'Error': n.errorMessage!,
};
final networkSectionHasMatch = q != null &&
q.isNotEmpty &&
networkBodyMap.entries.any((e) =>
e.key.toLowerCase().contains(q.toLowerCase()) ||
e.value.toLowerCase().contains(q.toLowerCase()));
final tiles = <Widget>[
ExpandableSection(
title: 'Network',
forceOpen: networkSectionHasMatch ? true : null,
highlightQuery: searchQuery,
child: _kvBody(networkBodyMap, query: searchQuery),
),
ExpandableSection(
title: 'Request Headers',
forceOpen: mapContains(n.requestHeaders) ? true : null,
highlightQuery: searchQuery,
child: _mapBody(n.requestHeaders, query: searchQuery),
),
ExpandableSection(
title: 'Request Body',
forceOpen: contains(n.requestBodyPreview) ? true : null,
highlightQuery: searchQuery,
child: _textBody(n.requestBodyPreview, query: searchQuery),
),
ExpandableSection(
title: 'Response Headers',
forceOpen: mapContains(n.responseHeaders) ? true : null,
highlightQuery: searchQuery,
child: _mapBody(n.responseHeaders, query: searchQuery),
),
ExpandableSection(
title: 'Response Body',
forceOpen: contains(n.responseBodyPreview) ? true : null,
highlightQuery: searchQuery,
child: _textBody(n.responseBodyPreview, query: searchQuery),
),
];
return Container(
width: double.infinity,
padding: const EdgeInsets.fromLTRB(12, 0, 12, 12),
child: Column(
children: [
for (int i = 0; i < tiles.length; i++) ...[
tiles[i],
if (i != tiles.length - 1) const SizedBox(height: 8),
],
],
),
);
}
Widget _textBody(String? text, {String? query}) {
final value = text == null || text.isEmpty ? '(empty)' : text;
return HighlightedSelectableText(text: value, query: query);
}
Widget _mapBody(Map<String, String>? map, {String? query}) {
if (map == null || map.isEmpty) {
return const HighlightedSelectableText(text: '(none)');
}
final lines = map.entries.map((e) => '${e.key}: ${e.value}').join('\n');
return HighlightedSelectableText(text: lines, query: query);
}
Widget _kvBody(Map<String, String> map, {String? query}) {
final lines = map.entries.map((e) => '${e.key}: ${e.value}').join('\n');
return HighlightedSelectableText(text: lines, query: query);
}
}
String _formatTs(DateTime ts) {
// Show only time (HH:mm:ss) for compactness
final h = ts.hour.toString().padLeft(2, '0');
final m = ts.minute.toString().padLeft(2, '0');
final s = ts.second.toString().padLeft(2, '0');
final ms = ts.millisecond.toString().padLeft(2, '0');
return '$h:$m:$s.$ms';
}

View File

@@ -0,0 +1,47 @@
import 'package:flutter/material.dart';
class SimpleText extends StatelessWidget {
const SimpleText({
super.key,
this.title,
this.subtitle,
this.icon,
});
final String? title;
final String? subtitle;
final IconData? icon;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
if (icon != null) ...[
Icon(icon, size: 36, color: theme.colorScheme.outline),
const SizedBox(height: 8),
],
if (title != null) ...[
Text(
title!,
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
const SizedBox(height: 6),
],
if (subtitle != null)
Text(
subtitle!,
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.outline,
),
textAlign: TextAlign.center,
),
],
),
);
}
}

View File

@@ -35,8 +35,6 @@ export 'field_read_only.dart';
export 'field_text_bounded.dart';
export 'field_search.dart';
export 'expandable_section.dart';
export 'empty_message.dart';
export 'terminal_tiles.dart';
export 'field_url.dart';
export 'intro_message.dart';
export 'markdown.dart';
@@ -71,6 +69,8 @@ export 'table_map.dart';
export 'table_request_form.dart';
export 'table_request.dart';
export 'tab_label.dart';
export 'text_highlighted_selectable.dart';
export 'text_simple.dart';
export 'texts.dart';
export 'uint8_audio_player.dart';
export 'widget_not_sent.dart';