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 11using System ;
2+ using System . Linq ;
23using System . Text ;
34using Discord ;
45
@@ -22,10 +23,31 @@ public static string CodeBlock(string text, string? lang = null) =>
2223
2324 public static void AppendCodeBlock ( this StringBuilder stringBuilder , string [ ] lines , string ? lang = null )
2425 {
26+ TrimBaseIndentation ( lines ) ;
27+
2528 stringBuilder . AppendLine ( CODE_BLOCK + lang ) ;
2629 foreach ( var line in lines )
2730 stringBuilder . AppendLine ( line ) ;
2831 stringBuilder . AppendLine ( CODE_BLOCK ) ;
2932 }
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+ }
3052 }
31- }
53+ }
You can’t perform that action at this time.
0 commit comments