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
-0
lines changed
Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 11using System ;
2+ using System . Linq ;
23using System . Text ;
4+ using System . Text . RegularExpressions ;
35using Discord ;
46
57namespace TabletBot . Discord
@@ -22,10 +24,31 @@ public static string CodeBlock(string text, string? lang = null) =>
2224
2325 public static void AppendCodeBlock ( this StringBuilder stringBuilder , string [ ] lines , string ? lang = null )
2426 {
27+ TrimBaseIndentation ( lines ) ;
28+
2529 stringBuilder . AppendLine ( CODE_BLOCK + lang ) ;
2630 foreach ( var line in lines )
2731 stringBuilder . AppendLine ( line ) ;
2832 stringBuilder . AppendLine ( CODE_BLOCK ) ;
2933 }
34+
35+ public static void TrimBaseIndentation ( string [ ] lines )
36+ {
37+ var baseIndentationLength = lines . Min ( line => {
38+ var indentation = CountIndentation ( line ) ;
39+ return line . Length > indentation ? indentation : int . MaxValue ;
40+ } ) ;
41+
42+ for ( int i = 0 ; i != lines . Length ; i ++ )
43+ lines [ i ] = lines [ i ] . Substring ( Math . Min ( baseIndentationLength , lines [ i ] . Length ) ) . TrimEnd ( ) ;
44+ }
45+
46+ public static int CountIndentation ( string line )
47+ {
48+ for ( var i = 0 ; i < line . Length ; i ++ )
49+ if ( ! char . IsWhiteSpace ( line [ i ] ) )
50+ return i ;
51+ return int . MaxValue ;
52+ }
3053 }
3154}
You can’t perform that action at this time.
0 commit comments