From 37f24925187cfae7203ca3fac343dd28e1383eaf Mon Sep 17 00:00:00 2001 From: fuwa Date: Thu, 25 Jul 2019 03:57:59 +0800 Subject: [PATCH] limit stdout buffer size to 100 lines --- cyberwow/lib/config.dart | 1 + cyberwow/lib/state.dart | 23 +++++++++++++++-------- cyberwow/lib/widget/resyncing.dart | 2 +- cyberwow/lib/widget/syncing.dart | 2 +- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/cyberwow/lib/config.dart b/cyberwow/lib/config.dart index 412d506..c84427c 100644 --- a/cyberwow/lib/config.dart +++ b/cyberwow/lib/config.dart @@ -34,4 +34,5 @@ const emuHost = '192.168.10.100'; const host = isEmu ? emuHost : '127.0.0.1'; const int hashLength = 12; +const stdoutLineBufferSize = 100; diff --git a/cyberwow/lib/state.dart b/cyberwow/lib/state.dart index 3afac4b..37db11e 100644 --- a/cyberwow/lib/state.dart +++ b/cyberwow/lib/state.dart @@ -24,6 +24,7 @@ import 'dart:io'; import 'dart:developer'; import 'dart:async'; import 'dart:convert'; +import 'dart:collection'; import 'dart:typed_data'; import 'package:flutter/services.dart'; import 'package:path_provider/path_provider.dart'; @@ -136,19 +137,22 @@ class LoadingState extends HookedState { await Future.wait([load(), showBanner()]); } - SyncingState _next = SyncingState(setState, getNotification, status); + SyncingState _next = SyncingState(setState, getNotification); return moveState(_next); } } class SyncingState extends HookedState { - String stdout; + Queue stdout = Queue(); bool synced = false; - SyncingState(f, s, this.stdout) : super (f, s); + SyncingState(f, s) : super (f, s); void append(String msg) { - this.stdout += msg; + stdout.addLast(msg); + while (stdout.length > config.stdoutLineBufferSize) { + stdout.removeFirst(); + } syncState(); } @@ -194,7 +198,7 @@ class SyncingState extends HookedState { } class SyncedState extends HookedState { - String stdout; + Queue stdout; int height; Stream processOutput; bool synced = true; @@ -213,7 +217,7 @@ class SyncedState extends HookedState { await for (final line in processOutput) { if (!synced) break; // print('synced: print stdout loop'); - stdout += line; + stdout.addLast(line); log.info(line); } } @@ -252,14 +256,17 @@ class SyncedState extends HookedState { class ReSyncingState extends HookedState { - String stdout; + Queue stdout; Stream processOutput; bool synced = false; ReSyncingState(f, s, this.stdout, this.processOutput) : super (f, s); void append(String msg) { - this.stdout += msg; + stdout.addLast(msg); + while (stdout.length > config.stdoutLineBufferSize) { + stdout.removeFirst(); + } syncState(); } diff --git a/cyberwow/lib/widget/resyncing.dart b/cyberwow/lib/widget/resyncing.dart index 5d4a57f..ae06ff0 100644 --- a/cyberwow/lib/widget/resyncing.dart +++ b/cyberwow/lib/widget/resyncing.dart @@ -53,7 +53,7 @@ Widget buildReSyncing(BuildContext context, ReSyncingState state) { reverse: true, child: Text ( - state.stdout, + state.stdout.join(), style: TextStyle ( fontFamily: 'RobotoMono', diff --git a/cyberwow/lib/widget/syncing.dart b/cyberwow/lib/widget/syncing.dart index 723027a..3f5978f 100644 --- a/cyberwow/lib/widget/syncing.dart +++ b/cyberwow/lib/widget/syncing.dart @@ -53,7 +53,7 @@ Widget buildSyncing(BuildContext context, SyncingState state) { reverse: true, child: Text ( - state.stdout, + state.stdout.join(), style: TextStyle ( fontFamily: 'RobotoMono',