1+ using JetBrains . Annotations ;
2+ using SER . Code . ArgumentSystem . Arguments ;
3+ using SER . Code . ArgumentSystem . BaseArguments ;
4+ using SER . Code . Helpers ;
5+ using SER . Code . MethodSystem . BaseMethods . Synchronous ;
6+ using SER . Code . MethodSystem . MethodDescriptors ;
7+ using SER . Code . ValueSystem ;
8+
9+ namespace SER . Code . MethodSystem . Methods . TimeMethods ;
10+
11+ [ UsedImplicitly ]
12+ public class FormatDurationMethod : ReturningMethod < TextValue > , IAdditionalDescription
13+ {
14+ public override string Description => "Formats a duration into a special format." ;
15+
16+ public string AdditionalDescription =>
17+ """
18+ Here are some examples:
19+
20+ #1) "HH:SS"
21+ Shows only minutes and seconds, perfect for round timers or cooldowns.
22+ Result: 01:30
23+
24+ #2) "DD days, HH hours and MM minutes"
25+ Creates a readable sentence. The system automatically handles the spaces and commas.
26+ Result: 2 days, 04 hours and 15 minutes
27+
28+ #3) "MMm SSs FFFms"
29+ Displays minutes, seconds, and milliseconds for pinpoint accuracy.
30+ Result: 01m 22s 450ms
31+ """ ;
32+
33+ public override Argument [ ] ExpectedArguments { get ; } =
34+ [
35+ new DurationArgument ( "duration to format" ) ,
36+ new TextArgument ( "format" )
37+ {
38+ Description =
39+ """
40+ Use these tags to format your time: DD for days, HH for hours, MM for minutes, and SS for seconds.
41+ You can separate them with colons (:) or dashes (-).
42+ """
43+ }
44+ ] ;
45+
46+ public override void Execute ( )
47+ {
48+ var durationToFormat = Args . GetDuration ( "duration to format" ) ;
49+ var format = Args . GetText ( "format" ) ;
50+ ReturnValue = new DynamicTextValue ( durationToFormat . ToString ( CreateNativeFormat ( format ) ) , Script ) ;
51+ }
52+
53+ private static readonly Dictionary < string , string > TokenMap = new ( )
54+ {
55+ { "DD" , @"d\ " } ,
56+ { "HH" , "hh" } ,
57+ { "MM" , "mm" } ,
58+ { "SS" , "ss" } ,
59+ { "FFF" , "fff" }
60+ } ;
61+
62+ public static string CreateNativeFormat ( string userFormat )
63+ {
64+ if ( string . IsNullOrEmpty ( userFormat ) ) return "c" ;
65+
66+ // 1. Put a quote at the start and end of the string
67+ // 2. "Break" the quotes only when we find a token
68+ // Example: HH:MM -> 'hh':'mm'
69+
70+ string result = "'" + userFormat + "'" ;
71+
72+ // We replace the User Token with: ' + Native Token + '
73+ // This effectively closes the literal string, puts the command, and reopens the literal.
74+ result = result . Replace ( "DD" , "'d'" )
75+ . Replace ( "HH" , "'hh'" )
76+ . Replace ( "MM" , "'mm'" )
77+ . Replace ( "SS" , "'ss'" )
78+ . Replace ( "FFF" , "'fff'" ) ;
79+
80+ // Clean up empty quotes '' created if tokens are next to each other
81+ return result . Replace ( "''" , "" ) ;
82+ }
83+ }
0 commit comments