Skip to content

Commit

Permalink
Open map on correct port, from output log
Browse files Browse the repository at this point in the history
Closes #10
  • Loading branch information
TechnicJelle committed Sep 4, 2024
1 parent 53ec7e7 commit 5186f0f
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions lib/control_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import "package:url_launcher/url_launcher.dart";
import "console.dart";
import "main.dart";

final portExtractionRegex = RegExp(r"(?:port\s*|:)(\d{4,5})$");

final _processProvider = Provider<RunningProcess?>((ref) {
final Directory? projectDirectory = ref.watch(projectDirectoryProvider);
if (projectDirectory == null) return null;
Expand Down Expand Up @@ -45,6 +47,9 @@ class RunningProcess {

Process? _process;

int _port = 8100;
int get port => _port;

Stream<String> get consoleOutput => _consoleOutputController.stream;
final _consoleOutputController = StreamController<String>();

Expand Down Expand Up @@ -110,8 +115,10 @@ class RunningProcess {
_consoleOutputController.add(event);
}

if (event.contains("WebServer started")) {
if (event.contains("WebServer bound to")) {
_stateController.add(RunningProcessState.running);
final String? portText = portExtractionRegex.firstMatch(event)?.group(1);
_port = int.tryParse(portText ?? "") ?? 8100;
}
});

Expand Down Expand Up @@ -180,7 +187,8 @@ class ControlPanel extends ConsumerWidget {
ElevatedButton.icon(
onPressed: processState == RunningProcessState.running
? () async {
if (!await launchUrl(Uri.parse("http://localhost:8100"))) {
final int port = ref.read(_processProvider)?.port ?? 8100;
if (!await launchUrl(Uri.parse("http://localhost:$port"))) {
throw Exception("Could not launch url!");
}
}
Expand Down

0 comments on commit 5186f0f

Please sign in to comment.