Formatter.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. using System.Text;
  2. using Microsoft.AspNetCore.Components;
  3. using Moonlight.App.Services;
  4. namespace Moonlight.App.Helpers;
  5. public static class Formatter
  6. {
  7. public static string ReplaceEnd(string input, string substringToReplace, string newSubstring)
  8. {
  9. int lastIndexOfSubstring = input.LastIndexOf(substringToReplace);
  10. if (lastIndexOfSubstring >= 0)
  11. {
  12. input = input.Remove(lastIndexOfSubstring, substringToReplace.Length).Insert(lastIndexOfSubstring, newSubstring);
  13. }
  14. return input;
  15. }
  16. public static string ConvertCamelCaseToSpaces(string input)
  17. {
  18. StringBuilder output = new StringBuilder();
  19. foreach (char c in input)
  20. {
  21. if (char.IsUpper(c))
  22. {
  23. output.Append(' ');
  24. }
  25. output.Append(c);
  26. }
  27. return output.ToString().Trim();
  28. }
  29. public static string FormatUptime(double uptime)
  30. {
  31. TimeSpan t = TimeSpan.FromMilliseconds(uptime);
  32. if (t.Days > 0)
  33. {
  34. return $"{t.Days}d {t.Hours}h {t.Minutes}m {t.Seconds}s";
  35. }
  36. else
  37. {
  38. return $"{t.Hours}h {t.Minutes}m {t.Seconds}s";
  39. }
  40. }
  41. public static string FormatUptime(TimeSpan t)
  42. {
  43. if (t.Days > 0)
  44. {
  45. return $"{t.Days}d {t.Hours}h {t.Minutes}m {t.Seconds}s";
  46. }
  47. else
  48. {
  49. return $"{t.Hours}h {t.Minutes}m {t.Seconds}s";
  50. }
  51. }
  52. private static double Round(this double d, int decimals)
  53. {
  54. return Math.Round(d, decimals);
  55. }
  56. public static string FormatSize(long bytes)
  57. {
  58. var i = Math.Abs(bytes) / 1024D;
  59. if (i < 1)
  60. {
  61. return bytes + " B";
  62. }
  63. else if (i / 1024D < 1)
  64. {
  65. return i.Round(2) + " KB";
  66. }
  67. else if (i / (1024D * 1024D) < 1)
  68. {
  69. return (i / 1024D).Round(2) + " MB";
  70. }
  71. else
  72. {
  73. return (i / (1024D * 1024D)).Round(2) + " GB";
  74. }
  75. }
  76. public static string FormatAgoFromDateTime(DateTime dt, SmartTranslateService translateService = null)
  77. {
  78. TimeSpan timeSince = DateTime.UtcNow.Subtract(dt);
  79. if (timeSince.TotalMilliseconds < 1)
  80. return translateService == null ? "just now" : translateService.Translate("just now");
  81. if (timeSince.TotalMinutes < 1)
  82. return translateService == null ? "less than a minute ago" : translateService.Translate("less than a minute ago");
  83. if (timeSince.TotalMinutes < 2)
  84. return translateService == null ? "1 minute ago" : translateService.Translate("1 minute ago");
  85. if (timeSince.TotalMinutes < 60)
  86. return Math.Round(timeSince.TotalMinutes) + (translateService == null ? " minutes ago" : translateService.Translate(" minutes ago"));
  87. if (timeSince.TotalHours < 2)
  88. return translateService == null ? "1 hour ago" : translateService.Translate("1 hour ago");
  89. if (timeSince.TotalHours < 24)
  90. return Math.Round(timeSince.TotalHours) + (translateService == null ? " hours ago" : translateService.Translate(" hours ago"));
  91. if (timeSince.TotalDays < 2)
  92. return translateService == null ? "1 day ago" : translateService.Translate("1 day ago");
  93. return Math.Round(timeSince.TotalDays) + (translateService == null ? " days ago" : translateService.Translate(" days ago"));
  94. }
  95. public static string FormatDate(DateTime e)
  96. {
  97. string i2s(int i)
  98. {
  99. if (i.ToString().Length < 2)
  100. return "0" + i;
  101. return i.ToString();
  102. }
  103. return $"{i2s(e.Day)}.{i2s(e.Month)}.{e.Year} {i2s(e.Hour)}:{i2s(e.Minute)}";
  104. }
  105. public static string FormatDateOnly(DateTime e)
  106. {
  107. string i2s(int i)
  108. {
  109. if (i.ToString().Length < 2)
  110. return "0" + i;
  111. return i.ToString();
  112. }
  113. return $"{i2s(e.Day)}.{i2s(e.Month)}.{e.Year}";
  114. }
  115. public static string FormatSize(double bytes)
  116. {
  117. var i = Math.Abs(bytes) / 1024D;
  118. if (i < 1)
  119. {
  120. return bytes + " B";
  121. }
  122. else if (i / 1024D < 1)
  123. {
  124. return i.Round(2) + " KB";
  125. }
  126. else if (i / (1024D * 1024D) < 1)
  127. {
  128. return (i / 1024D).Round(2) + " MB";
  129. }
  130. else
  131. {
  132. return (i / (1024D * 1024D)).Round(2) + " GB";
  133. }
  134. }
  135. public static double CalculateAverage(List<double> values)
  136. {
  137. if (values == null || values.Count == 0)
  138. {
  139. throw new ArgumentException("The list cannot be null or empty.");
  140. }
  141. double sum = 0;
  142. foreach (double value in values)
  143. {
  144. sum += value;
  145. }
  146. return sum / values.Count;
  147. }
  148. public static double CalculatePercentage(double part, double total)
  149. {
  150. if (total == 0)
  151. {
  152. return 0;
  153. }
  154. return (part / total) * 100;
  155. }
  156. public static RenderFragment FormatLineBreaks(string content)
  157. {
  158. return builder =>
  159. {
  160. int i = 0;
  161. var arr = content.Split("\n", StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
  162. foreach (var line in arr)
  163. {
  164. builder.AddContent(i, line);
  165. if (i++ != arr.Length - 1)
  166. {
  167. builder.AddMarkupContent(i, "<br/>");
  168. }
  169. }
  170. };
  171. }
  172. }