utils.dart 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import 'dart:math' as math;
  2. import 'dart:io';
  3. import 'package:flutter/services.dart';
  4. String bytesToSize(int bytes) {
  5. if (bytes == 0) return '0 B';
  6. const k = 1024;
  7. const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
  8. final i = (math.log(bytes) / math.log(k)).floor();
  9. return '${(bytes / math.pow(k, i)).toStringAsFixed(2)} ${sizes[i]}';
  10. }
  11. const Map<String, Map<String, String>> _copyCommandLineProxyTypes = {
  12. 'cmd': {'prefix': 'set ', 'quot': '', 'join': '&&'},
  13. 'bash': {'prefix': 'export ', 'quot': '"', 'join': ' && '},
  14. 'powershell': {'prefix': '\$env:', 'quot': '"', 'join': ';'},
  15. };
  16. Future<void> copyCommandLineProxy(String type, {String? http, String? https}) async {
  17. final types = _copyCommandLineProxyTypes[type];
  18. if (types == null) return;
  19. final prefix = types['prefix']!;
  20. final quot = types['quot']!;
  21. final join = types['join']!;
  22. List<String> commands = [];
  23. if (http != null) commands.add('${prefix}http_proxy=${quot}http://$http$quot');
  24. if (https != null) commands.add('${prefix}https_proxy=${quot}http://$https$quot');
  25. if (commands.isNotEmpty) await Clipboard.setData(ClipboardData(text: commands.join(join)));
  26. }
  27. extension OrNull<T> on T {
  28. T? orNull(bool a) {
  29. return a ? this : null;
  30. }
  31. }
  32. extension BindOne<T, R> on R Function(T a) {
  33. R Function() bindOne(T a) {
  34. return () => this(a);
  35. }
  36. }
  37. extension BindFirst<T, T2, R> on R Function(T a, T2 b) {
  38. R Function(T2 b) bindFirst(T a) {
  39. return (T2 b) => this(a, b);
  40. }
  41. }
  42. extension MapIndex<T> on List<T> {
  43. List<R> mapIndex<R>(R Function(int index, T item) fn) {
  44. final List<R> list = [];
  45. for (int idx = 0; idx < length; idx++) {
  46. list.add(fn(idx, this[idx]));
  47. }
  48. return list;
  49. }
  50. }
  51. enum RunningState {
  52. starting,
  53. running,
  54. stopping,
  55. stoped,
  56. error,
  57. uninstall,
  58. }
  59. Future<bool> isPortAvailable(int port) async {
  60. try {
  61. final socket = await ServerSocket.bind(InternetAddress.loopbackIPv4, port, shared: true);
  62. await socket.close();
  63. return true;
  64. } catch (e) {
  65. return false;
  66. }
  67. }
  68. Future<int> findAvailablePort(int startPort, {int maxAttempts = 100}) async {
  69. for (int i = 1; i < maxAttempts; i++) {
  70. int port = startPort + i;
  71. return port;
  72. }
  73. throw Exception('无法找到可用端口');
  74. }