//wine 简单控制台 import win.ui; import JSON; namespace console.wine{}; namespace console; getWindow = function(){ if(!self.form){ self.form = ..thread.get("console.wine.form"); }; if( self.form ){ if( ..win.isWindow(self.form.hwnd) ){ return self.form.hwnd; } self.form = null; ..thread.set("console.wine.form",null) } } open = function(){ return ..thread.lock("std.console.wine.open",__open) } __open = function(){ if( getWindow() ){ self.form.show(); return self.form; } self.form = null; ..thread.invoke( function(){ import win.ui; import win.ui.ctrl.edit; var winform = win.form(text=..io._exefile;right=934;bottom=821) winform.add( edit={cls="edit";left=0;top=0;right=935;bottom=822;bgcolor=0;color=15793151;db=1;dl=1;dr=1;dt=1;edge=1;hscroll=1;multiline=1;vscroll=1;z=1} ) winform.getch = function(){ winform.edit.lastInputChar = null; winform.edit.wndproc = { [0x102/*_WM_CHAR*/] =function(hwnd,message,wParam,lParam){ if( message == 0x102/*_WM_CHAR*/ ){ owner.lastInputChar = wParam; return 0; } } } win.wait( lambda() winform.edit.lastInputChar, winform.hwnd); winform.edit.wndproc = { [0x102/*_WM_CHAR*/] =function(hwnd,message,wParam,lParam){} } return winform.edit.lastInputChar; } winform.getche = function(mask){ var c = ..string.pack( winform.getch() ); winform.edit.appendText( c == '\r'?'\r\n':(mask:c) ); return c; } winform.edit.defaultTranslateAccelerator = function(msg){ if( msg.message == 0x100/*_WM_KEYDOWN*/) { var vk = msg.wParam; if( vk == 0xD/*_VK_RETURN*/ ){ if(!winform.lastInputStart) return; var b = winform.edit.getSel(); var a = winform.lastInputStart; if( a < b) { winform.edit.setSel(a+1,b); winform.lastInputLine = winform.edit.selText; winform.edit.setSel(b); } } elseif( vk == 0x8/*_VK_BACK*/ ){ if(!winform.lastInputStart) return; var b = winform.edit.getSel(); var a = winform.lastInputStart; if( b <= a ) return true; } } } winform.getInputText = function(){ winform.lastInputLine = null; winform.edit.setFocus(); winform.lastInputStart = winform.edit.getSel(); ..win.wait( function(){ return winform.lastInputLine; },winform.hwnd); winform.lastInputStart = null; return winform.lastInputLine; } winform.onCancel = function(){} winform.onOk = function(){} ..thread.set("console.wine.form",winform) winform.edit.setFocus(); win.loopMessage(); import process; process().terminate(); },listener ) ..win.wait(lambda()..thread.get("console.wine.form")); self.form = ..thread.get("console.wine.form"); self.form.show(); return self.form; } getch = function(){ return open().getch(); } getche = function(mask){ return open().getche(mask); } getPassword = function(mask="*"){ var s = {}; var c = getche(mask) while( c != '\r' ){ ..table.push( s,c); c = getche( mask ) } return ..string.join(s) } var moreLines = 1; more = function(count){ count = count : 1; moreLines = moreLines % count; if(!moreLines){ pause(,langMore,"---------- More -----------"); } moreLines++; }; askYesNo = function(title){ log( "[Y/N] " + (title : langYesNo) ) var vk = getch(); if( vk == 'Y'#){ return true; } elseif( vk == 'N'#){ return false; } } pause = function(close,msg,erase){ open(); if( msg ){ msg = ..string.toUtf16(msg); if( erase ){ erase = ..string.toUtf16(erase); if( #erase < #msg ) erase = erase + ..string.repeat((#msg-#erase)/2 ," "); } else erase = msg; } msg := langPause; var a = self.form.edit.getSel(); writeText( msg ); var b = self.form.edit.getSel(); var vk = getch(); if(#erase) { if( b > a ){ self.form.edit.setSel(a,b); self.form.edit.selText = ""; } } else { writeText('\r\n'); } if(close) self.close(); return chr; } logPause = function(...){ log(...); pause(); } fullscreen = function(){ open().form.fullscreen(); } writeText = function(s,...){ s = tostring(s); if( (!..string.isUtf8(s)) && (!..string.isUtf16(s)) ){ ..string.setUtf(s,1); } open().edit.appendText(s,...) } print = function(s,...){ s = tostring(s); if( (!..string.isUtf8(s)) && (!..string.isUtf16(s)) ){ ..string.setUtf(s,1); } open().edit.print(s,...) } log = print; showLoading = function(msg,dots){ log(msg); return; } printf = function(s,...){ s = tostring(s); if( (!..string.isUtf8(s)) && (!..string.isUtf16(s)) ){ ..string.setUtf(s,1); } open().edit.printf(s,...) } hex = function(s){ print( ..string.hex(s," ") ) } hex = function(s){ print( ..string.hex(s," ") ) } writeBack = function( s,... ){ if( ... !== null ) s = ..string.toUtf16( ..string.format(s,... ) ); writeText( ..string.repeat(#s/2,'\b') ); writeText( s ); } debug = function(...){ if( _STUDIO_INVOKED ) writeText(...); } dump = function(v,...){ open(); if( type(v) == type.table ) { if(..com && ..com.IsObject(v)){ writeText( ..com.DumpTypeInfo(v) ) } writeText( ..table.tostring(v) ) } else writeText( (tostring(v) ) ) if(...===null ){ writeText('\r\n'); return; } writeText('\t') return dump(...); } setTitle = function(title){ open().text = title; } getTitle = function(){ return open().text; } getText = function(s){ if( s ) writeText( s ); return open().getInputText() ; } getNumber = function( s,len ){ var n; while( var _; n = tonumber( getText(s),10 ); n === null ) { if( !getWindow()) break ; s = langInvalidNumber ; } return n; } dumpJson = function(v){ log( ..JSON.stringify(v,true,false) ); } varDump = function(...){ open(); var args = {...}; var b,e = ..table.range(args); for(i=b;e){ var arg = args[i]; var t = type(arg); print("参数位置:" ++ i); print("数据类型:" ++ t); if( t != type.table ){ print("变量值:" ++ tostring(arg),'\r\n'); } else { print("变量值: {" ); var logged = { _struct = 1 }; for k,v in ..table.eachName(arg){ logged[k] = true; writeText(' ', k,' = ',tostring(v) ,'\r\n'); } for(i=1;#arg;1){ logged[i] = true; writeText(' [', i,'] = ',tostring(arg[i]) ,'\r\n'); } for(k,v in arg){ if( !logged[k] ){ writeText(' [', tostring(k),'] = ',tostring(v) ,'\r\n'); } } print('}'); if(#arg){ print("序列数组长度:",#arg); } if(arg[["_struct"]]){ try{ print("结构体大小:",..raw.sizeof(arg) ); } catch(e){ print("结构体大小:未知(包含动态数组)"); } } print('\r\n'); } } } close = function(){ if( getWindow() ){ self.form.show(false); } } begin var lcid = ::Kernel32.GetUserDefaultLangID(); if( lcid==0x404/*zh-TW*/ || lcid == 0x0C04/*zh-HK*/ || lcid == 0x1404/*zh-MO*/ ){ langMore = "--- 請按任意鍵顯示更多 ---"; langPause = "請按任意鍵繼續 ... "; langYesNo = "按 Y 鍵繼續,按 N 鍵取消"; langInvalidNumber = "請重新輸入有效的數值: "; } elseif( lcid==0x804/*zh-CN*/ || lcid!==0x1004/*zh-SG*/ ){ langMore = "--- 请按任意键显示更多 ---"; langPause = "请按任意键继续 ... "; langYesNo = "按 Y 键继续,按 N 键取消"; langInvalidNumber = "请重新输入有效的数值: "; } else { langMore = "--- Press any key to read more ---"; langPause = "Press any key to continue . . ."; langYesNo = "Do you want to continue?"; langInvalidNumber = "Please enter a valid number."; } end; /**intellisense() console.wine = console.wine 模拟了部分 console 的函数\n用于在 Wine 环境下创建简单的控制台窗口\n不支持标准输入输出流 end intellisense**/