From 44acee2f7a2d29d6c82b5f6527b87a6d3580882d Mon Sep 17 00:00:00 2001 From: Udhay-Adithya Date: Thu, 11 Sep 2025 01:27:43 +0530 Subject: [PATCH] refactor: move serialization methods for terminal entries from terminal page to terminal provider --- lib/providers/terminal_providers.dart | 45 +++++++++++++++++++++ lib/screens/terminal/terminal_page.dart | 52 +++---------------------- 2 files changed, 51 insertions(+), 46 deletions(-) diff --git a/lib/providers/terminal_providers.dart b/lib/providers/terminal_providers.dart index a224d23f..5e6050d2 100644 --- a/lib/providers/terminal_providers.dart +++ b/lib/providers/terminal_providers.dart @@ -174,4 +174,49 @@ class TerminalController extends StateNotifier { system: SystemLogData(category: category, message: message, stack: stack), )); } + + // Serialization + String serializeAll({List? entries}) { + final list = entries ?? state.entries; + final buf = StringBuffer(); + for (final e in list) { + final time = e.ts.toIso8601String(); + final title = titleFor(e); + final sub = subtitleFor(e); + buf.writeln('[$time] ${e.level.name.toUpperCase()} - $title'); + if (sub != null && sub.isNotEmpty) { + buf.writeln(' $sub'); + } + } + return buf.toString(); + } + + String titleFor(TerminalEntry e) { + switch (e.source) { + case TerminalSource.network: + final n = e.network!; + final status = n.responseStatus != null ? ' — ${n.responseStatus}' : ''; + return '${n.method.name.toUpperCase()} ${n.url}$status'; + case TerminalSource.js: + final j = e.js!; + return 'JS ${j.level}'; + case TerminalSource.system: + return 'System'; + } + } + + String? subtitleFor(TerminalEntry e) { + switch (e.source) { + case TerminalSource.network: + final n = e.network!; + if (n.errorMessage != null) return n.errorMessage; + return n.responseBodyPreview ?? n.requestBodyPreview; + case TerminalSource.js: + final j = e.js!; + return j.args.join(' '); + case TerminalSource.system: + final s = e.system!; + return s.message; + } + } } diff --git a/lib/screens/terminal/terminal_page.dart b/lib/screens/terminal/terminal_page.dart index 57373778..838fd4cc 100644 --- a/lib/screens/terminal/terminal_page.dart +++ b/lib/screens/terminal/terminal_page.dart @@ -111,7 +111,9 @@ class _TerminalPageState extends ConsumerState { // Copy all button CopyButton( showLabel: false, - toCopy: _serializeAllLogs(allEntries), + toCopy: ref + .read(terminalStateProvider.notifier) + .serializeAll(entries: allEntries), ), ], ), @@ -123,56 +125,14 @@ class _TerminalPageState extends ConsumerState { bool matches(TerminalEntry e) { if (!_selectedLevels.contains(e.level)) return false; if (q.isEmpty) return true; - final title = _titleFor(e).toLowerCase(); - final sub = (_subtitleFor(e) ?? '').toLowerCase(); + final controller = ref.read(terminalStateProvider.notifier); + final title = controller.titleFor(e).toLowerCase(); + final sub = (controller.subtitleFor(e) ?? '').toLowerCase(); return title.contains(q) || sub.contains(q); } return entries.where(matches).toList(growable: false); } - - String _serializeAllLogs(List entries) { - final buf = StringBuffer(); - for (final e in entries) { - final time = e.ts.toIso8601String(); - final title = _titleFor(e); - final sub = _subtitleFor(e); - buf.writeln('[$time] ${e.level.name.toUpperCase()} - $title'); - if (sub != null && sub.isNotEmpty) { - buf.writeln(' $sub'); - } - } - return buf.toString(); - } - - String _titleFor(TerminalEntry e) { - switch (e.source) { - case TerminalSource.network: - final n = e.network!; - final status = n.responseStatus != null ? ' — ${n.responseStatus}' : ''; - return '${n.method.name.toUpperCase()} ${n.url}$status'; - case TerminalSource.js: - final j = e.js!; - return 'JS ${j.level}'; - case TerminalSource.system: - return 'System'; - } - } - - String? _subtitleFor(TerminalEntry e) { - switch (e.source) { - case TerminalSource.network: - final n = e.network!; - if (n.errorMessage != null) return n.errorMessage; - return n.responseBodyPreview ?? n.requestBodyPreview; - case TerminalSource.js: - final j = e.js!; - return j.args.join(' '); - case TerminalSource.system: - final s = e.system!; - return s.message; - } - } } class _FilterMenu extends StatelessWidget {