1- using System . Collections . Concurrent ;
1+ using System . Buffers ;
2+ using System . Collections . Concurrent ;
23using System . Diagnostics ;
34using System . Reflection ;
5+ using System . Runtime . InteropServices ;
46using System . Text ;
57using Kantan . Text ;
68using Novus . Streams ;
79using SixLabors . ImageSharp ;
810using SixLabors . ImageSharp . Formats ;
11+ using SmartImage . Lib ;
912using Spectre . Console ;
1013using Spectre . Console . Cli ;
1114using SmartImage . Rdx . Shell ;
@@ -27,57 +30,63 @@ namespace SmartImage.Rdx;
2730public static class Program
2831{
2932
33+ private static readonly byte [ ] Utf8_Bom_Sig = new [ ]
34+ {
35+ ( byte ) 0xEF , ( byte ) 0xBB , ( byte ) 0xBF
36+ } ;
37+
38+ public static string ReadInputStream ( out bool isFile )
39+ {
40+
41+ var path = Path . GetTempFileName ( ) ;
42+
43+ using var fs = File . Open ( path , FileMode . Truncate , FileAccess . Write ) ;
44+
45+ using Stream stdin = Console . OpenStandardInput ( ) ;
46+
47+ byte [ ] buffer = new byte [ 4096 ] ; // Buffer to hold byte input
48+ int bytesRead ;
49+ int iter = 0 ;
50+
51+ while ( ( bytesRead = stdin . Read ( buffer , 0 , buffer . Length ) ) > 0 ) {
52+ if ( iter == 0 ) {
53+
54+ if ( buffer [ 0 ] == Utf8_Bom_Sig [ 0 ]
55+ && buffer [ 1 ] == Utf8_Bom_Sig [ 1 ]
56+ && buffer [ 2 ] == Utf8_Bom_Sig [ 2 ] ) {
57+
58+ buffer = buffer [ 3 ..] ;
59+ bytesRead -= Utf8_Bom_Sig . Length ;
60+ }
61+ }
62+
63+ fs . Write ( buffer , 0 , bytesRead ) ;
64+
65+ iter ++ ;
66+ }
67+
68+ isFile = File . Exists ( path ) ;
69+ fs . Flush ( ) ;
70+ fs . Dispose ( ) ;
71+
72+ return path ;
73+ }
74+
3075 public static async Task < int > Main ( string [ ] args )
3176 {
3277 Debug . WriteLine ( AConsole . Profile . Height ) ;
3378 Debug . WriteLine ( Console . BufferHeight ) ;
3479
3580 if ( Console . IsInputRedirected ) {
36- // using var str = Console.OpenStandardInput(500);
37- Stream stdin = Console . OpenStandardInput ( 1000 ) ;
38- /*byte[] inBuffer = new byte[1_000_000];
39-
40- int outLen = stdin.Read(inBuffer, 0, inBuffer.Length);
41- char[] chars = Encoding.ASCII.GetChars(inBuffer, 0, outLen);
42- var cmd = new string(chars);
43- if ((cmd[cmd.Length - 2] == '\r') && (cmd[cmd.Length - 1] == '\n'))
44- {
45- cmd = cmd.Substring(0, cmd.Length - 2);
46- }*/
47- var sr = new StreamReader ( stdin ) ;
48- int n1 = 0 , n2 = 0 ;
49- var sb = new char [ 8_000_000 ] ;
50-
51- while ( ! sr . EndOfStream ) {
52- /*if (sb[^2] == '\r' && sb[^1] == '\n') {
53- break;
54- }*/
55- n1 += ( n2 = sr . Read ( sb , n1 , sb . Length - n1 ) ) ;
56-
57- if ( n2 == 0 ) {
58- break ;
59- }
81+ var pipeInput = ReadInputStream ( out var isf ) ;
6082
61- }
62-
63- sb = sb [ ..n1 ] ;
64-
65- if ( sb [ ^ 2 ] == '\r ' && sb [ ^ 1 ] == '\n ' ) {
66- sb = sb [ 0 ..^ 2 ] ;
67- }
83+ // AConsole.WriteLine($"[{pipeInput}] {isf}");
6884
69- IImageFormat fmt ;
70- AConsole . WriteLine ( $ "{ sb . Length } ") ;
85+ var newargs = new string [ args . Length + 1 ] ;
86+ newargs [ 0 ] = pipeInput ;
87+ args . CopyTo ( newargs , 1 ) ;
7188
72- /*try {
73- fmt = await Image.DetectFormatAsync();
74- }
75- catch (Exception e) {
76- AConsole.WriteLine($"{e.Message}");
77- }
78- finally {
79- str.TrySeek();
80- }*/
89+ args = newargs ;
8190 }
8291
8392 var ff = CliFormat . LoadFigletFontFromResource ( nameof ( R2 . Fg_larry3d ) , out var ms ) ;
0 commit comments