tools.dart 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import 'dart:convert';
  2. import 'dart:math';
  3. // DateFormat('dd.MM.yy HH:mm:ss').format(dateCreated); // ??
  4. String formatTimeStamp({int? timeStamp, DateTime? date, format='yyyy-MM-dd HH:mm:ss'}) {
  5. try {
  6. var time = (date != null) ? date
  7. : ((timeStamp != null) ? DateTime.fromMillisecondsSinceEpoch(timeStamp) : DateTime.now());
  8. var dateObj = {
  9. 'M+': time.month, //月份
  10. 'd+': time.day, //日
  11. 'H+': time.hour, //小时
  12. 'm+': time.minute, //分
  13. 's+': time.second, //秒
  14. };
  15. RegExp yearReg = RegExp(r"(y+)");
  16. if (yearReg.hasMatch(format)) {
  17. var matches = yearReg.allMatches(format);
  18. String match = "${matches.elementAt(0).group(1)}";
  19. format = format.replaceAll(match, "${time.year}".substring(4 - match.length));
  20. }
  21. dateObj.forEach((key, value) {
  22. RegExp replaceReg = RegExp(r'(' + key + ')');
  23. if (replaceReg.hasMatch(format)) {
  24. var matches = replaceReg.allMatches(format);
  25. String match = "${matches.elementAt(0).group(1)}";
  26. format = format.replaceAll(match, (match.length == 1) ? "$value" : "00$value".substring("$value".length));
  27. }
  28. });
  29. return format;
  30. } catch (error) {
  31. return '';
  32. }
  33. }
  34. void printJsonStr(String input) {
  35. const JsonDecoder decoder = JsonDecoder();
  36. const JsonEncoder encoder = JsonEncoder.withIndent(' ');
  37. final dynamic object = decoder.convert(input);
  38. final dynamic prettyString = encoder.convert(object);
  39. prettyString.split('\n').forEach((dynamic element) => print(element));
  40. }
  41. void printJson(Map input) {
  42. const JsonEncoder encoder = JsonEncoder.withIndent(' ');
  43. final dynamic prettyString = encoder.convert(input);
  44. prettyString.split('\n').forEach((dynamic element) => print(element));
  45. }
  46. // move to fileUtils.dart
  47. // String formatFileSize(int size) {
  48. // final fileSizeInKB = size / 1000;
  49. // if (fileSizeInKB < 1) {
  50. // return '$size bytes';
  51. // }
  52. // final fileSizeInMB = fileSizeInKB / 1000;
  53. // if (fileSizeInMB < 1) {
  54. // return '${fileSizeInKB.toStringAsFixed(2)} KB';
  55. // }
  56. // final fileSizeInGB = fileSizeInMB / 1000;
  57. // if (fileSizeInGB < 1) {
  58. // return '${fileSizeInMB.toStringAsFixed(2)} MB';
  59. // }
  60. // final fileSizeInTB = fileSizeInGB / 1000;
  61. // if (fileSizeInTB < 1) {
  62. // return '${fileSizeInGB.toStringAsFixed(2)} GB';
  63. // }
  64. // return '${fileSizeInTB.toStringAsFixed(2)} TB';
  65. // }
  66. // 均匀分组
  67. List<int> uniformGrouping(int totalSize, int defaultGroupSize) {
  68. if (totalSize < 0) {
  69. return [];
  70. } else if (totalSize == 0) {
  71. return [0];
  72. }
  73. final int theUpperLimit = (defaultGroupSize * 1.5).toInt(); // 折行上限
  74. if (totalSize <= theUpperLimit) {
  75. return [totalSize];
  76. } else if ((totalSize > theUpperLimit) && (totalSize <= (defaultGroupSize * 2))) {
  77. int half = totalSize ~/ 2;
  78. return [half, totalSize - half];
  79. }
  80. final int groupNumber = totalSize ~/ defaultGroupSize;
  81. final int remainder = totalSize % defaultGroupSize;
  82. List<int> group = [];
  83. if (remainder == 0) {
  84. for (int i = 0; i < groupNumber; i++) {
  85. group.add(defaultGroupSize);
  86. }
  87. } else {
  88. final splitRemainder = remainder ~/ groupNumber;
  89. final groupRemainder = remainder % groupNumber;
  90. for (int i = 0; i < groupRemainder; i++) {
  91. group.add(defaultGroupSize + splitRemainder + 1);
  92. }
  93. for (int i = groupRemainder; i < groupNumber; i++) {
  94. group.add((defaultGroupSize + splitRemainder));
  95. }
  96. }
  97. return group;
  98. }
  99. String ellipsizeStrMiddle(String str, {
  100. int headerLen = 4,
  101. int tailLen = 4,
  102. String splitter = '...',
  103. }) {
  104. if (str.length > 2) {
  105. int liveHeaderLen = headerLen;
  106. int liveTailLen = tailLen;
  107. int loopN = min(liveHeaderLen, liveTailLen);
  108. for (int i = 0; i < loopN; i++) {
  109. if (str.length > (liveHeaderLen + liveTailLen)) {
  110. String prefix = str.substring(0, liveHeaderLen);
  111. String suffix = str.substring(str.length - liveTailLen);
  112. return '$prefix$splitter$suffix';
  113. }
  114. liveHeaderLen--;
  115. liveTailLen--;
  116. }
  117. String prefix = str.substring(0, liveHeaderLen);
  118. String suffix = str.substring(str.length - liveTailLen);
  119. return '$prefix$splitter$suffix';
  120. } else {
  121. return str;
  122. }
  123. }