From 81167837b081d830c23d0c4255a9d52c70a87337 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Wed, 18 Sep 2024 12:01:48 +0900 Subject: [PATCH 1/9] Organize progress bar code --- flutter_package/bin/src/common.dart | 2 +- flutter_package/bin/src/progress.dart | 51 +++++++++------------------ 2 files changed, 18 insertions(+), 35 deletions(-) diff --git a/flutter_package/bin/src/common.dart b/flutter_package/bin/src/common.dart index c3c96775..27aed458 100644 --- a/flutter_package/bin/src/common.dart +++ b/flutter_package/bin/src/common.dart @@ -20,6 +20,6 @@ void removeCliLines(int lines) { for (var i = 0; i < lines; i++) { stdout.write('\x1B[1A'); // Move the cursor up one line stdout.write('\x1B[2K'); // Clear the line - stdout.write('\r'); // Return the cursor to the front + stdout.write('\r'); // Move the cursor to the front } } diff --git a/flutter_package/bin/src/progress.dart b/flutter_package/bin/src/progress.dart index 0c8432cf..c982b743 100644 --- a/flutter_package/bin/src/progress.dart +++ b/flutter_package/bin/src/progress.dart @@ -1,8 +1,7 @@ -// CLI progress bar copied from +// CLI progress bar copied and modified from // https://github.com/RohitEdathil/ConsoleBars import 'dart:async'; -import 'dart:io'; import 'common.dart'; class ProgressBar { @@ -13,7 +12,7 @@ class ProgressBar { int _progress = 0; late int max; - // Time + /// Tracks time final _clock = Stopwatch(); /// Whether a timer should be present @@ -22,8 +21,6 @@ class ProgressBar { /// Percentage should be displayed or not bool percentage; - // Decorations - /// The description of the bar String _desc; @@ -33,11 +30,8 @@ class ProgressBar { /// The character to used as fill String fill; - /// Scale of the bar relative to the terminal width - double scale; - /// Width of the bar - int? width; + int width; /// Whether the instance should print nothing bool silent; @@ -56,14 +50,13 @@ class ProgressBar { } /// Arguments: - /// - total : Total number of steps - /// - desc : Simple text shown before the bar (optional) - /// - space : Character denoting empty space (default : '.') - /// - fill : Character denoting filled space (default : '█') - /// - time : Toggle timing mode (default : false) - /// - percentage : Toggle percentage display (default : false) - /// - scale : Scale of the bar relative to width (between: 0 and 1, default: 0.5, Irrelavant if width is specified) - /// - width : Width of the bar drawn in the CLI + /// - `total` : Total number of steps + /// - `desc` : Simple text shown after the bar (optional) + /// - `space` : Character denoting empty space (default : '.') + /// - `fill` : Character denoting filled space (default : '█') + /// - `time` : Toggle timing mode (default : false) + /// - `percentage` : Toggle percentage display (default : false) + /// - `width` : Width of the bar drawn in the CLI (default: 40) ProgressBar({ required int total, String desc = '', @@ -71,18 +64,11 @@ class ProgressBar { this.fill = '█', this.time = false, this.percentage = false, - this.scale = 0.5, this.width = 40, this.silent = false, }) : _desc = desc, _total = total { - // Handles width of the bar, throws an error if it's not specified and the terminal width is not available - try { - max = width ?? ((stdout.terminalColumns - _desc.length) * scale).toInt(); - } on StdoutException { - throw StdoutException( - 'Could not get terminal width, try specifying a width manually'); - } + max = width; if (time) { _clock.start(); scheduleMicrotask(autoRender); @@ -128,24 +114,21 @@ class ProgressBar { _clock.stop(); } } - String timeStr = ''; + String? timeStr = null; if (time) { final rate = _clock.elapsedMicroseconds / (_current == 0 ? 1 : _current); final eta = Duration(microseconds: ((_total - _current) * rate).toInt()); - timeStr = '[ ' + - _clock.elapsed.toString().substring(0, 10) + - ' / ' + - eta.toString().substring(0, 10) + - ' ]'; + final elpsedStr = _clock.elapsed.toString().substring(0, 10); + final etaStr = eta.toString().substring(0, 10); + timeStr = '[ $elpsedStr / $etaStr ]'; } - String perc = ''; + String? perc = null; if (percentage) { perc = '${(_current * 100 / _total).toStringAsFixed(1)}%'; } final bar = '${fill * _progress}${space * (max - _progress)}'; final frameParts = [bar, '$_current/$_total', perc, timeStr, ':', _desc]; - final filteredParts = frameParts.where((v) => v.isNotEmpty).toList(); - final frame = filteredParts.join(' '); + final frame = frameParts.where((v) => v != null).toList().join(' '); removeCliLines(1); print(frame); } From 55363a1530c80a66b10dd27f4b1745912a6ffcd4 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Wed, 18 Sep 2024 12:18:05 +0900 Subject: [PATCH 2/9] Separate command `rinf server` --- flutter_package/bin/rinf.dart | 37 ++++++++++++++++++++-------- flutter_package/bin/src/helpers.dart | 28 +-------------------- 2 files changed, 28 insertions(+), 37 deletions(-) diff --git a/flutter_package/bin/rinf.dart b/flutter_package/bin/rinf.dart index 5381fc36..9660bee2 100644 --- a/flutter_package/bin/rinf.dart +++ b/flutter_package/bin/rinf.dart @@ -8,7 +8,7 @@ import 'src/internet.dart'; import 'src/common.dart'; Future main(List args) async { - // After running `dart run rinf`, + // When running `dart run rinf`, // Unnecessary two lines of //`Building package executable...\nBuilt rinf:rinf.` appear. // Remove those before proceeding. @@ -20,13 +20,14 @@ Future main(List args) async { // Parse CLI arguments and run the corresponding function. final runner = CommandRunner( 'rinf', - 'Helper commands for building apps with Rust in Flutter.', + 'Helper commands for building apps with Rust in Flutter', usageLineLength: 80, ) ..addCommand(ConfigCommand()) ..addCommand(TemplateCommand()) ..addCommand(MessageCommand()) - ..addCommand(WasmCommand()); + ..addCommand(WasmCommand()) + ..addCommand(ServerCommand()); try { await runner.run(args); @@ -38,8 +39,7 @@ Future main(List args) async { class ConfigCommand extends Command { final name = 'config'; - final description = 'Shows current Rinf configuration' + - ' resolved from `pubspec.yaml` with defaults applied.'; + final description = 'Shows Rinf configuration resolved from `pubspec.yaml`'; ConfigCommand() {} @@ -51,7 +51,7 @@ class ConfigCommand extends Command { class TemplateCommand extends Command { final name = 'template'; - final description = 'Applies Rust template to the current Flutter project.'; + final description = 'Applies Rust template to the current Flutter project'; TemplateCommand() {} @@ -63,13 +63,13 @@ class TemplateCommand extends Command { class MessageCommand extends Command { final name = 'message'; - final description = 'Generates message code from `.proto` files.'; + final description = 'Generates message code from `.proto` files'; MessageCommand() { argParser.addFlag( 'watch', abbr: 'w', - help: 'Continuously watches `.proto` files.', + help: 'Continuously watches `.proto` files', ); } @@ -90,13 +90,14 @@ class MessageCommand extends Command { class WasmCommand extends Command { final name = 'wasm'; - final description = 'Builds the webassembly module for the web.'; + final description = 'Builds the webassembly module for the web' + ' with `wasm-pack`'; WasmCommand() { argParser.addFlag( 'release', abbr: 'r', - help: 'Builds in release mode.', + help: 'Builds in release mode', ); } @@ -109,3 +110,19 @@ class WasmCommand extends Command { await buildWebassembly(release); } } + +class ServerCommand extends Command { + final name = 'server'; + final description = 'Shows how to run Flutter web server with web headers'; + + ServerCommand() {} + + Future run() async { + final commandLines = [ + 'flutter run', + '--web-header=Cross-Origin-Opener-Policy=same-origin', + '--web-header=Cross-Origin-Embedder-Policy=require-corp' + ]; + print(commandLines.join(' ').dim); + } +} diff --git a/flutter_package/bin/src/helpers.dart b/flutter_package/bin/src/helpers.dart index 8a014b18..17605321 100644 --- a/flutter_package/bin/src/helpers.dart +++ b/flutter_package/bin/src/helpers.dart @@ -2,7 +2,6 @@ import 'dart:io'; import 'package:package_config/package_config.dart'; import 'package:yaml/yaml.dart'; -import 'package:chalkdart/chalkstrings.dart'; import 'config.dart'; import 'message.dart'; @@ -286,30 +285,5 @@ Future buildWebassembly(bool isReleaseMode) async { fillingBar.increment(); // Guide the developer how to run Flutter web server with web headers. - print('To run the Flutter web server, use:'); - final commandLineDivider = await getCommandLineDivider(); - final commandLines = [ - 'flutter run', - '--web-header=Cross-Origin-Opener-Policy=same-origin', - '--web-header=Cross-Origin-Embedder-Policy=require-corp' - ]; - print(commandLines.join(' ${commandLineDivider}\n').dim); -} - -Future getCommandLineDivider() async { - if (Platform.isWindows) { - // Windows environment, check further for PowerShell or CMD - if (Platform.environment['SHELL'] == null) { - // Likely PowerShell environment - return '`'; - // // Likely Command Prompt (cmd.exe) - // return "^"; - } else { - // Bash or some other shell - return '\\'; - } - } else { - // Bash or some other shell - return '\\'; - } + print('To get the Flutter web server command, run `rinf server`'); } From 8d4aa3f925c5b446404471fcb1a8022eb7cd94c5 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Wed, 18 Sep 2024 12:19:45 +0900 Subject: [PATCH 3/9] Organize `rinf server` code --- flutter_package/bin/rinf.dart | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flutter_package/bin/rinf.dart b/flutter_package/bin/rinf.dart index 9660bee2..fe2fcc43 100644 --- a/flutter_package/bin/rinf.dart +++ b/flutter_package/bin/rinf.dart @@ -119,7 +119,8 @@ class ServerCommand extends Command { Future run() async { final commandLines = [ - 'flutter run', + 'flutter', + 'run', '--web-header=Cross-Origin-Opener-Policy=same-origin', '--web-header=Cross-Origin-Embedder-Policy=require-corp' ]; From aeb934f60f93b7a0aaf3d0f6914fa769eb4dcdc2 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Wed, 18 Sep 2024 12:20:00 +0900 Subject: [PATCH 4/9] Organize `rinf server` code again --- flutter_package/bin/rinf.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/flutter_package/bin/rinf.dart b/flutter_package/bin/rinf.dart index fe2fcc43..b4806679 100644 --- a/flutter_package/bin/rinf.dart +++ b/flutter_package/bin/rinf.dart @@ -118,12 +118,12 @@ class ServerCommand extends Command { ServerCommand() {} Future run() async { - final commandLines = [ + final commandParts = [ 'flutter', 'run', '--web-header=Cross-Origin-Opener-Policy=same-origin', '--web-header=Cross-Origin-Embedder-Policy=require-corp' ]; - print(commandLines.join(' ').dim); + print(commandParts.join(' ').dim); } } From a6ddd72137d4a0388f3ba346e55e11e873af6b2d Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Wed, 18 Sep 2024 12:34:20 +0900 Subject: [PATCH 5/9] Remove `internet_connection_checker` --- flutter_package/bin/src/internet.dart | 11 +++++++++-- flutter_package/pubspec.yaml | 1 - 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/flutter_package/bin/src/internet.dart b/flutter_package/bin/src/internet.dart index 02d8eef6..421841e4 100644 --- a/flutter_package/bin/src/internet.dart +++ b/flutter_package/bin/src/internet.dart @@ -1,7 +1,14 @@ -import 'package:internet_connection_checker/internet_connection_checker.dart'; +import 'dart:io'; var isInternetConnected = false; Future checkConnectivity() async { - isInternetConnected = await InternetConnectionChecker().hasConnection; + try { + final result = await InternetAddress.lookup('pub.dev'); + if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) { + isInternetConnected = true; + } + } on SocketException catch (_) { + isInternetConnected = false; + } } diff --git a/flutter_package/pubspec.yaml b/flutter_package/pubspec.yaml index 3a32446c..26e664cd 100644 --- a/flutter_package/pubspec.yaml +++ b/flutter_package/pubspec.yaml @@ -15,7 +15,6 @@ dependencies: watcher: ^1.1.0 ffi: ^2.1.0 yaml: ^3.1.2 - internet_connection_checker: ^1.0.0 args: ^2.5.0 chalkdart: ^2.2.1 From 38880a61fc7511771e56f25bf8c679d4d7213cf5 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Wed, 18 Sep 2024 12:35:25 +0900 Subject: [PATCH 6/9] Upgrade `lints` --- flutter_package/pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flutter_package/pubspec.yaml b/flutter_package/pubspec.yaml index 26e664cd..d41ca3ed 100644 --- a/flutter_package/pubspec.yaml +++ b/flutter_package/pubspec.yaml @@ -19,7 +19,7 @@ dependencies: chalkdart: ^2.2.1 dev_dependencies: - lints: ">=3.0.0 <5.0.0" + lints: ">=4.0.0 <5.0.0" # For information on the generic Dart part of this file, see the # following page: https://dart.dev/tools/pub/pubspec From 480fa8773ea63a9da913ecb11d8a6c9ac7f9a236 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Wed, 18 Sep 2024 12:36:35 +0900 Subject: [PATCH 7/9] Improve readability of the command runner code --- flutter_package/bin/rinf.dart | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/flutter_package/bin/rinf.dart b/flutter_package/bin/rinf.dart index b4806679..673a8d58 100644 --- a/flutter_package/bin/rinf.dart +++ b/flutter_package/bin/rinf.dart @@ -18,19 +18,19 @@ Future main(List args) async { await checkConnectivity(); // Parse CLI arguments and run the corresponding function. - final runner = CommandRunner( + final commandRunner = CommandRunner( 'rinf', 'Helper commands for building apps with Rust in Flutter', usageLineLength: 80, - ) - ..addCommand(ConfigCommand()) - ..addCommand(TemplateCommand()) - ..addCommand(MessageCommand()) - ..addCommand(WasmCommand()) - ..addCommand(ServerCommand()); + ); + commandRunner.addCommand(ConfigCommand()); + commandRunner.addCommand(TemplateCommand()); + commandRunner.addCommand(MessageCommand()); + commandRunner.addCommand(WasmCommand()); + commandRunner.addCommand(ServerCommand()); try { - await runner.run(args); + await commandRunner.run(args); } catch (error) { // Print the error gracefully without backtrace. print(error.toString().trim().red); From 4c17220429082d0512c22796300f8ec5cf500529 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Wed, 18 Sep 2024 12:39:11 +0900 Subject: [PATCH 8/9] Format command help sentences --- flutter_package/bin/rinf.dart | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/flutter_package/bin/rinf.dart b/flutter_package/bin/rinf.dart index 673a8d58..674e84f8 100644 --- a/flutter_package/bin/rinf.dart +++ b/flutter_package/bin/rinf.dart @@ -39,7 +39,7 @@ Future main(List args) async { class ConfigCommand extends Command { final name = 'config'; - final description = 'Shows Rinf configuration resolved from `pubspec.yaml`'; + final description = 'Show Rinf configuration resolved from `pubspec.yaml`.'; ConfigCommand() {} @@ -51,7 +51,7 @@ class ConfigCommand extends Command { class TemplateCommand extends Command { final name = 'template'; - final description = 'Applies Rust template to the current Flutter project'; + final description = 'Apply Rust template to the current Flutter project.'; TemplateCommand() {} @@ -63,7 +63,7 @@ class TemplateCommand extends Command { class MessageCommand extends Command { final name = 'message'; - final description = 'Generates message code from `.proto` files'; + final description = 'Generate message code from `.proto` files.'; MessageCommand() { argParser.addFlag( @@ -90,8 +90,7 @@ class MessageCommand extends Command { class WasmCommand extends Command { final name = 'wasm'; - final description = 'Builds the webassembly module for the web' - ' with `wasm-pack`'; + final description = 'Build the webassembly module for the web.'; WasmCommand() { argParser.addFlag( @@ -113,7 +112,7 @@ class WasmCommand extends Command { class ServerCommand extends Command { final name = 'server'; - final description = 'Shows how to run Flutter web server with web headers'; + final description = 'Show how to run Flutter web server with web headers.'; ServerCommand() {} From 71f7c15a259fce4250cfd7d7508e5bf51356f7e8 Mon Sep 17 00:00:00 2001 From: Donghyun Kim Date: Wed, 18 Sep 2024 12:43:45 +0900 Subject: [PATCH 9/9] Format command description sentence --- flutter_package/bin/rinf.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flutter_package/bin/rinf.dart b/flutter_package/bin/rinf.dart index 674e84f8..7c76c3ce 100644 --- a/flutter_package/bin/rinf.dart +++ b/flutter_package/bin/rinf.dart @@ -20,7 +20,7 @@ Future main(List args) async { // Parse CLI arguments and run the corresponding function. final commandRunner = CommandRunner( 'rinf', - 'Helper commands for building apps with Rust in Flutter', + 'Helper commands for building apps using Rust in Flutter.', usageLineLength: 80, ); commandRunner.addCommand(ConfigCommand());