-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
249 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,162 @@ | ||
import 'dart:convert'; | ||
import 'dart:io'; | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:wslconfigurer/windows/sh.dart'; | ||
|
||
class ProcessText extends StatelessWidget { | ||
class ProcessText extends StatefulWidget { | ||
final Process process; | ||
final bool latest; | ||
final Encoding codec; | ||
const ProcessText({ | ||
super.key, | ||
required this.process, | ||
this.latest = false, | ||
this.codec = systemEncoding, | ||
}); | ||
|
||
const ProcessText({super.key, required this.process}); | ||
@override | ||
State<StatefulWidget> createState() => _ProcessTextState(); | ||
} | ||
|
||
class _ProcessTextState extends State<ProcessText> { | ||
List<(bool, String)> span = []; | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
|
||
var process = widget.process; | ||
|
||
process.stderr.listen( | ||
(data) => setState(() { | ||
span.add((true, widget.codec.decode(data).trim())); | ||
}), | ||
); | ||
process.stdout.listen( | ||
(data) => setState(() { | ||
span.add((false, widget.codec.decode(data).trim())); | ||
}), | ||
); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
if (span.isEmpty) { | ||
return const SizedBox.shrink(); | ||
} | ||
|
||
if (widget.latest) { | ||
return span.lastOrNull == null | ||
? const Text("") | ||
: SelectableText( | ||
span.last.$2, | ||
style: span.last.$1 ? const TextStyle(color: Colors.red) : null, | ||
); | ||
} | ||
|
||
return Column( | ||
children: span | ||
.map((data) => SelectableText(data.$2, | ||
style: data.$1 ? const TextStyle(color: Colors.red) : null)) | ||
.toList(), | ||
); | ||
} | ||
} | ||
|
||
class ProcessCommandRunWidget extends StatefulWidget { | ||
final String executable; | ||
final Iterable<String> arguments; | ||
final bool su; | ||
final bool runInShell; | ||
final Encoding codec; | ||
|
||
const ProcessCommandRunWidget({ | ||
super.key, | ||
required this.executable, | ||
this.arguments = const [], | ||
this.su = false, | ||
this.runInShell = false, | ||
this.codec = systemEncoding, | ||
}); | ||
|
||
@override | ||
State<StatefulWidget> createState() => _ProcessCommandRunWidgetState(); | ||
} | ||
|
||
class _ProcessCommandRunWidgetState extends State<ProcessCommandRunWidget> { | ||
bool confirm = false; | ||
Process? process; | ||
|
||
@override | ||
void dispose() { | ||
super.dispose(); | ||
process?.kill(); | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return StreamBuilder( | ||
stream: process.stdout, | ||
builder: (context, snapshot) { | ||
var data = snapshot.data; | ||
if (data == null) { | ||
return const Text(""); | ||
} | ||
|
||
return SelectableText(const SystemEncoding().decode(data)); | ||
}, | ||
if (confirm) { | ||
return Column( | ||
mainAxisSize: MainAxisSize.min, | ||
children: [ | ||
Row( | ||
mainAxisSize: MainAxisSize.min, | ||
children: [ | ||
Text( | ||
[widget.executable, ...widget.arguments].join(" "), | ||
), | ||
const IconButton( | ||
onPressed: null, | ||
icon: Icon(Icons.keyboard_arrow_right_rounded), | ||
), | ||
], | ||
), | ||
FutureBuilder( | ||
future: Process.start( | ||
widget.executable, | ||
widget.arguments.toList(), | ||
runInShell: widget.runInShell, | ||
), | ||
builder: (context, snapshot) { | ||
var proc = snapshot.data; | ||
if (proc == null) { | ||
return const CircularProgressIndicator(); | ||
} | ||
process = proc; | ||
|
||
return ProcessText( | ||
process: proc, | ||
codec: widget.codec, | ||
); | ||
}, | ||
) | ||
], | ||
); | ||
} | ||
return Row( | ||
mainAxisSize: MainAxisSize.min, | ||
children: [ | ||
Text( | ||
[widget.executable, ...widget.arguments].join(" "), | ||
), | ||
IconButton( | ||
onPressed: () { | ||
if (widget.su) { | ||
su( | ||
context, | ||
() => setState(() { | ||
confirm = true; | ||
})); | ||
} else { | ||
setState(() { | ||
confirm = true; | ||
}); | ||
} | ||
}, | ||
icon: const Icon(Icons.keyboard_arrow_right_rounded), | ||
), | ||
], | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,4 +30,3 @@ Future<Process> enableFeature(String featurename) async { | |
"/norestart" | ||
]); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import 'dart:convert'; | ||
import 'dart:typed_data'; | ||
|
||
import 'package:flutter/services.dart'; | ||
|
||
const utf16 = Utf16Codec(); | ||
|
||
final class Utf16Codec extends Encoding { | ||
const Utf16Codec(); | ||
@override | ||
Converter<List<int>, String> get decoder => Utf16Decoder(); | ||
|
||
@override | ||
Converter<String, List<int>> get encoder => Utf16Encoder(); | ||
|
||
@override | ||
String get name => "utf-16"; | ||
} | ||
|
||
class Utf16Decoder extends Converter<List<int>, String> { | ||
@override | ||
String convert(List<int> input) { | ||
return String.fromCharCodes( | ||
Uint16List.sublistView(Uint8List.fromList(input))); | ||
} | ||
} | ||
|
||
class Utf16Encoder extends Converter<String, List<int>> { | ||
@override | ||
List<int> convert(String input) { | ||
return input.codeUnits; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters