Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/dashmon.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
library dashmon;

export './src/dashmon.dart';

export './src/device_selection.dart';
46 changes: 32 additions & 14 deletions lib/src/dashmon.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:io';
import 'dart:convert';

import '../src/device_selection.dart';

class Dashmon {
late Process _process;
final List<String> args;
Expand Down Expand Up @@ -44,29 +46,33 @@ class Dashmon {
}

void _processLine(String line) {
Comment thread
hsd2747 marked this conversation as resolved.
if (line.contains('More than one device connected')) {
_print(
"Dashmon found multiple devices, device choosing menu isn't supported yet, please use the -d argument");
_process.kill();
exit(1);
} else {
_print(line);
}
_print(line);
}

void _processError(String line) {
_print(line);
}

Future<void> start() async {
Device? selectedDevice;
final devices = await getDevices();

if (devices.length > 1) {
selectedDevice = await selectDevice(devices);

if (selectedDevice == null) {
print('No device selected.');
Comment thread
hsd2747 marked this conversation as resolved.
exit(1);
}

_proxiedArgs.add('--device-id=${selectedDevice.id}');
}

_process = await (_isFvm
? Process.start(
'fvm', ['flutter', _isAttach ? 'attach' : 'run', ..._proxiedArgs])
: Process.start(
'flutter', [_isAttach ? 'attach' : 'run', ..._proxiedArgs]));
? Process.start('fvm', ['flutter', _isAttach ? 'attach' : 'run', ..._proxiedArgs])
: Process.start('flutter', [_isAttach ? 'attach' : 'run', ..._proxiedArgs]));

_process.stdout.transform(utf8.decoder).forEach(_processLine);

_process.stderr.transform(utf8.decoder).forEach(_processError);

final currentDir = File('.');
Expand All @@ -85,7 +91,19 @@ class Dashmon {

stdin.lineMode = false;
stdin.echoMode = false;
stdin.transform(utf8.decoder).forEach(_process.stdin.write);

stdin.transform(utf8.decoder).listen((input) {
switch (input.trim()) {
case 'c':
for (int i = 0; i < stdout.terminalLines; i++) {
stdout.writeln();
}
break;
default:
_process.stdin.write(input);
}
});

final exitCode = await _process.exitCode;
exit(exitCode);
}
Expand Down
51 changes: 51 additions & 0 deletions lib/src/device_selection.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import 'dart:io';

class Device {
Device({
required this.id,
this.name,
});

final String id;
final String? name;
}

Future<List<Device>> getDevices() async {
final result = await Process.run('flutter', ['devices']);
final devices = result.stdout.toString().split('\n');

return devices.where((device) => device.contains('•')).map((device) {
final parts = device.split('•');

return Device(
id: parts[1].trim(),
name: parts[0].trim(),
);
}).toList();
}

Future<Device?> selectDevice(List<Device> devices) async {
if (devices.isEmpty) {
print('No devices found.');
return null;
}

print('Available devices:');
for (int i = 0; i < devices.length; i++) {
print('${i + 1}. ${devices[i].name} (${devices[i].id})');
}

stdout.write('Select a device (enter the number): ');
final input = stdin.readLineSync();
if (input == null) {
return null;
}

final index = int.tryParse(input);
if (index == null || index < 1 || index > devices.length) {
print('Invalid selection.');
return null;
}

return devices[index - 1];
}