This repository was archived by the owner on Nov 24, 2022. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Expand file tree Collapse file tree 1 file changed +23
-1
lines changed Original file line number Diff line number Diff line change 1
1
using System ;
2
+ using System . Linq ;
2
3
using System . Text ;
3
4
using Discord ;
4
5
@@ -22,10 +23,31 @@ public static string CodeBlock(string text, string? lang = null) =>
22
23
23
24
public static void AppendCodeBlock ( this StringBuilder stringBuilder , string [ ] lines , string ? lang = null )
24
25
{
26
+ TrimBaseIndentation ( lines ) ;
27
+
25
28
stringBuilder . AppendLine ( CODE_BLOCK + lang ) ;
26
29
foreach ( var line in lines )
27
30
stringBuilder . AppendLine ( line ) ;
28
31
stringBuilder . AppendLine ( CODE_BLOCK ) ;
29
32
}
33
+
34
+ public static void TrimBaseIndentation ( string [ ] lines )
35
+ {
36
+ var baseIndentationLength = lines . Min ( line => {
37
+ var indentation = CountIndentation ( line ) ;
38
+ return line . Length > indentation ? indentation : int . MaxValue ;
39
+ } ) ;
40
+
41
+ for ( int i = 0 ; i != lines . Length ; i ++ )
42
+ lines [ i ] = lines [ i ] . Substring ( Math . Min ( baseIndentationLength , lines [ i ] . Length ) ) . TrimEnd ( ) ;
43
+ }
44
+
45
+ public static int CountIndentation ( string line )
46
+ {
47
+ for ( var i = 0 ; i < line . Length ; i ++ )
48
+ if ( ! char . IsWhiteSpace ( line [ i ] ) )
49
+ return i ;
50
+ return int . MaxValue ;
51
+ }
30
52
}
31
- }
53
+ }
You can’t perform that action at this time.
0 commit comments