1- using  System ; 
2- using  System . Linq ; 
3- using  System . Reflection ; 
1+ using  System . Reflection ; 
42using  System . Runtime . InteropServices ; 
5- using  System . Threading . Tasks ; 
63using  WikiClientLibrary . Client ; 
74using  WikiClientLibrary . Scribunto ; 
85using  WikiClientLibrary . Sites ; 
96
10- namespace  WikiClientLibrary . Samples . ScribuntoInteractive 
7+ namespace  WikiClientLibrary . Samples . ScribuntoInteractive ; 
8+ 
9+ internal  static   class  Program 
1110{ 
12-     internal  static   class  Program 
13-     { 
1411
15-         internal  static   async  Task  Main ( string [ ]  args ) 
12+     internal  static   async  Task  Main ( string [ ]  args ) 
13+     { 
14+         var  endPoint  =  args . Length  >  0  ?  args [ 0 ]  :  "https://test2.wikipedia.org/w/api.php" ; 
15+         using  var  client  =  new  WikiClient  {  ClientUserAgent  =  "ScribuntoConsoleTestApplication1/0.1"  } ; 
16+         var  site  =  new  WikiSite ( client ,  endPoint ) ; 
17+         await  site . Initialization ; 
18+         var  sc  =  new  ScribuntoConsole ( site ) ; 
19+         await  ResetSessionAsync ( sc ) ; 
20+         var  eofShortcut  =  RuntimeInformation . IsOSPlatform ( OSPlatform . Windows )  ?  "Ctrl + Z"  :  "Ctrl + D" ; 
21+         Console . WriteLine ( "* Enter any Lua expression to evaluate. EOF ({0}) to exit." ,  eofShortcut ) ; 
22+         Console . WriteLine ( "* Precede a line with '=' to evaluate it as an expression or use \x1b [36mprint()\x1b [0m. Use \x1b [36mmw.logObject()\x1b [0m for tables." ) ; 
23+         Console . WriteLine ( "* Use \x1b [36mmw.log()\x1b [0m and \x1b [36mmw.logObject()\x1b [0m in module code to send messages to this console." ) ; 
24+         Console . WriteLine ( "* Enter .help for a list of local commands." ) ; 
25+         while  ( true ) 
1626        { 
17-             var  endPoint  =  args . Length  >  0  ?  args [ 0 ]  :  "https://test2.wikipedia.org/w/api.php" ; 
18-             using  var  client  =  new  WikiClient  {  ClientUserAgent  =  "ScribuntoConsoleTestApplication1/0.1"  } ; 
19-             var  site  =  new  WikiSite ( client ,  endPoint ) ; 
20-             await  site . Initialization ; 
21-             var  sc  =  new  ScribuntoConsole ( site ) ; 
22-             await  ResetSessionAsync ( sc ) ; 
23-             var  eofShortcut  =  RuntimeInformation . IsOSPlatform ( OSPlatform . Windows )  ?  "Ctrl + Z"  :  "Ctrl + D" ; 
24-             Console . WriteLine ( "* Enter any Lua expression to evaluate. EOF ({0}) to exit." ,  eofShortcut ) ; 
25-             Console . WriteLine ( "* Precede a line with '=' to evaluate it as an expression or use \x1b [36mprint()\x1b [0m. Use \x1b [36mmw.logObject()\x1b [0m for tables." ) ; 
26-             Console . WriteLine ( "* Use \x1b [36mmw.log()\x1b [0m and \x1b [36mmw.logObject()\x1b [0m in module code to send messages to this console." ) ; 
27-             Console . WriteLine ( "* Enter .help for a list of local commands." ) ; 
28-             while  ( true ) 
27+             Console . Write ( "> " ) ; 
28+             var  l  =  Console . ReadLine ( ) ; 
29+             if  ( l  ==  null ) 
30+                 break ; 
31+             if  ( string . IsNullOrWhiteSpace ( l ) ) 
32+                 continue ; 
33+             if  ( l . StartsWith ( "." ) ) 
2934            { 
30-                 Console . Write ( "> " ) ; 
31-                 var  l  =  Console . ReadLine ( ) ; 
32-                 if  ( l  ==  null ) 
35+                 if  ( string . Equals ( l ,  ".exit" ,  StringComparison . OrdinalIgnoreCase ) ) 
3336                    break ; 
34-                 if  ( string . IsNullOrWhiteSpace ( l ) ) 
35-                     continue ; 
36-                 if  ( l . StartsWith ( "." ) ) 
37+                 await  ExecuteCommandAsync ( l [ 1 ..] ,  sc ) ; 
38+                 continue ; 
39+             } 
40+             try 
41+             { 
42+                 var  result  =  await  sc . EvaluateAsync ( l ) ; 
43+                 if  ( result . IsNewSession ) 
3744                { 
38-                     if  ( string . Equals ( l ,  ".exit" ,  StringComparison . OrdinalIgnoreCase ) ) 
39-                         break ; 
40-                     await  ExecuteCommandAsync ( l [ 1 ..] ,  sc ) ; 
41-                     continue ; 
45+                     Console . WriteLine ( "---------- Session Cleared ----------" ) ; 
4246                } 
43-                 try 
47+                 if   ( ! string . IsNullOrEmpty ( result . Output ) ) 
4448                { 
45-                     var  result  =  await  sc . EvaluateAsync ( l ) ; 
46-                     if  ( result . IsNewSession ) 
47-                     { 
48-                         Console . WriteLine ( "---------- Session Cleared ----------" ) ; 
49-                     } 
50-                     if  ( ! string . IsNullOrEmpty ( result . Output ) ) 
51-                     { 
52-                         Console . WriteLine ( result . Output ) ; 
53-                     } 
54-                     if  ( ! string . IsNullOrEmpty ( result . ReturnValue ) ) 
55-                     { 
56-                         Console . ForegroundColor  =  ConsoleColor . White ; 
57-                         Console . WriteLine ( result . ReturnValue ) ; 
58-                         Console . ResetColor ( ) ; 
59-                     } 
49+                     Console . WriteLine ( result . Output ) ; 
6050                } 
61-                 catch   ( ScribuntoConsoleException   ex ) 
51+                 if   ( ! string . IsNullOrEmpty ( result . ReturnValue ) ) 
6252                { 
63-                     if  ( ! string . IsNullOrEmpty ( ex . EvaluationResult ? . Output ) ) 
64-                     { 
65-                         Console . WriteLine ( ex . EvaluationResult . Output ) ; 
66-                     } 
67-                     WriteError ( $ "{ ex . ErrorCode } : { ex . ErrorMessage } ") ; 
53+                     Console . ForegroundColor  =  ConsoleColor . White ; 
54+                     Console . WriteLine ( result . ReturnValue ) ; 
55+                     Console . ResetColor ( ) ; 
6856                } 
6957            } 
70-         } 
71- 
72-         private  static   void  WriteError ( string  message ) 
73-         { 
74-             Console . ForegroundColor  =  ConsoleColor . Red ; 
75-             Console . Error . WriteLine ( message ) ; 
76-             Console . ResetColor ( ) ; 
77-         } 
78- 
79- 
80-         private  static   async  Task  ExecuteCommandAsync ( string  command ,  ScribuntoConsole  sc ) 
81-         { 
82-             var  method  =  typeof ( Program ) . GetMethods ( BindingFlags . Static  |  BindingFlags . NonPublic ) 
83-                 . FirstOrDefault ( m =>  string . Equals ( m . GetCustomAttribute < ConsoleCommandAttribute > ( ) ? . Command ,  command ,  StringComparison . OrdinalIgnoreCase ) ) ; 
84-             if  ( method  ==  null ) 
58+             catch  ( ScribuntoConsoleException  ex ) 
8559            { 
86-                 WriteError ( "Invalid command: "  +  command  +  "." ) ; 
87-                 return ; 
60+                 if  ( ! string . IsNullOrEmpty ( ex . EvaluationResult ? . Output ) ) 
61+                 { 
62+                     Console . WriteLine ( ex . EvaluationResult . Output ) ; 
63+                 } 
64+                 WriteError ( $ "{ ex . ErrorCode } : { ex . ErrorMessage } ") ; 
8865            } 
89-             var  result  =  method . Invoke ( null ,  new  object [ ]  {  sc  } ) ; 
90-             if  ( result  is  Task  t ) 
91-                 await  t ; 
9266        } 
67+     } 
9368
94-          [ ConsoleCommand ( "reset" ,   "Clears the Lua evaluation session." ) ] 
95-          private   static   async   Task   ResetSessionAsync ( ScribuntoConsole   sc ) 
96-         { 
97-              await   sc . ResetAsync ( ) ; 
98-              Console . WriteLine ( "Initialized Scribunto console on {0} with session ID {1}." ,   sc . Site . SiteInfo . SiteName ,   sc . SessionId ) ; 
99-          } 
69+     private   static   void   WriteError ( string   message ) 
70+     { 
71+         Console . ForegroundColor   =   ConsoleColor . Red ; 
72+         Console . Error . WriteLine ( message ) ; 
73+         Console . ResetColor ( ) ; 
74+     } 
10075
101-         [ ConsoleCommand ( "help" ,  "Shows the command list." ) ] 
102-         private  static   void  ShowHelp ( ScribuntoConsole  sc ) 
103-         { 
104-             var  commands  =  typeof ( Program ) . GetMethods ( BindingFlags . Static  |  BindingFlags . NonPublic ) 
105-                 . Select ( m =>  ( method :  m ,  attr :  m . GetCustomAttribute < ConsoleCommandAttribute > ( ) ) ) 
106-                 . Where ( t =>  t . attr  !=  null ) 
107-                 . Select ( t =>  ( command :  t . attr ! . Command ,  desc :  t . attr . Description ,  method :  t . method ) ) 
108-                 . OrderBy ( t =>  t . command ) ; 
109-             foreach  ( ( string  command ,  string  desc ,  _ )  in  commands ) 
110-             { 
111-                 Console . WriteLine ( ".{0,-15} {1}" ,  command ,  desc ) ; 
112-             } 
113-         } 
11476
115-         [ ConsoleCommand ( "memory" ,  "Shows the server-side memory usage." ) ] 
116-         private  static   void  ShowMemory ( ScribuntoConsole  sc ) 
77+     private  static   async  Task  ExecuteCommandAsync ( string  command ,  ScribuntoConsole  sc ) 
78+     { 
79+         var  method  =  typeof ( Program ) . GetMethods ( BindingFlags . Static  |  BindingFlags . NonPublic ) 
80+             . FirstOrDefault ( m =>  string . Equals ( m . GetCustomAttribute < ConsoleCommandAttribute > ( ) ? . Command ,  command ,  StringComparison . OrdinalIgnoreCase ) ) ; 
81+         if  ( method  ==  null ) 
11782        { 
118-             Console . WriteLine ( "Memory used / maximum allowed: {0}/{1}" ,  sc . SessionSize ,  sc . SessionMaxSize ) ; 
83+             WriteError ( "Invalid command: "  +  command  +  "." ) ; 
84+             return ; 
11985        } 
86+         var  result  =  method . Invoke ( null ,  new  object [ ]  {  sc  } ) ; 
87+         if  ( result  is  Task  t ) 
88+             await  t ; 
89+     } 
90+ 
91+     [ ConsoleCommand ( "reset" ,  "Clears the Lua evaluation session." ) ] 
92+     private  static   async  Task  ResetSessionAsync ( ScribuntoConsole  sc ) 
93+     { 
94+         await  sc . ResetAsync ( ) ; 
95+         Console . WriteLine ( "Initialized Scribunto console on {0} with session ID {1}." ,  sc . Site . SiteInfo . SiteName ,  sc . SessionId ) ; 
96+     } 
12097
121-         [ ConsoleCommand ( "exit" ,  "Exits the interactive console." ) ] 
122-         private  static   void  Exit ( ScribuntoConsole  sc ) 
98+     [ ConsoleCommand ( "help" ,  "Shows the command list." ) ] 
99+     private  static   void  ShowHelp ( ScribuntoConsole  sc ) 
100+     { 
101+         var  commands  =  typeof ( Program ) . GetMethods ( BindingFlags . Static  |  BindingFlags . NonPublic ) 
102+             . Select ( m =>  ( method :  m ,  attr :  m . GetCustomAttribute < ConsoleCommandAttribute > ( ) ) ) 
103+             . Where ( t =>  t . attr  !=  null ) 
104+             . Select ( t =>  ( command :  t . attr ! . Command ,  desc :  t . attr . Description ,  method :  t . method ) ) 
105+             . OrderBy ( t =>  t . command ) ; 
106+         foreach  ( ( string  command ,  string  desc ,  _ )  in  commands ) 
123107        { 
124-             // Stub 
108+             Console . WriteLine ( ".{0,-15} {1}" ,   command ,   desc ) ; 
125109        } 
110+     } 
126111
112+     [ ConsoleCommand ( "memory" ,  "Shows the server-side memory usage." ) ] 
113+     private  static   void  ShowMemory ( ScribuntoConsole  sc ) 
114+     { 
115+         Console . WriteLine ( "Memory used / maximum allowed: {0}/{1}" ,  sc . SessionSize ,  sc . SessionMaxSize ) ; 
127116    } 
128117
129-     [ AttributeUsage ( AttributeTargets . Method ,   Inherited   =   false ,   AllowMultiple   =   false ) ] 
130-     sealed   class   ConsoleCommandAttribute   :   Attribute 
118+     [ ConsoleCommand ( "exit" ,   "Exits the interactive console." ) ] 
119+     private   static   void   Exit ( ScribuntoConsole   sc ) 
131120    { 
121+         // Stub 
122+     } 
132123
133-          public   string   Command   {   get ;   } 
124+ } 
134125
135-         public  string ?  Description  {  get ;  } 
126+ [ AttributeUsage ( AttributeTargets . Method ,  Inherited  =  false ,  AllowMultiple  =  false ) ] 
127+ sealed  class  ConsoleCommandAttribute  :  Attribute 
128+ { 
136129
137-         public  ConsoleCommandAttribute ( string  command )  :  this ( command ,  null ) 
138-         { 
139-         } 
130+     public  string  Command  {  get ;  } 
140131
141-         public  ConsoleCommandAttribute ( string  command ,  string ?  description ) 
142-         { 
143-             Command  =  command ; 
144-             Description  =  description ; 
145-         } 
132+     public  string ?  Description  {  get ;  } 
146133
134+     public  ConsoleCommandAttribute ( string  command )  :  this ( command ,  null ) 
135+     { 
147136    } 
148137
149- } 
138+     public  ConsoleCommandAttribute ( string  command ,  string ?  description ) 
139+     { 
140+         Command  =  command ; 
141+         Description  =  description ; 
142+     } 
143+ 
144+ } 
0 commit comments