Originally posted by RizlaUK
View Post
Quite useful ^^

Quite useful ^^
)








missing string/menu extension library errors
)





Enumeration
#Main ;The main window :)
EndEnumeration


; PureBasic wrapper for the PrettyPrinter by Mikail
; Wrappercode written by Imagine Programming.
; cscript.exe::scriptpath [Sourcefile] [Output] [-o(verwrite)] [-s(trip comments)] [-help] [-verbose] [-debug] [-unix]
;{ Enumerations
Enumeration ;The windows
#Main
EndEnumeration
Enumeration ;The objects
#Frame
#Label_InFile
#Input_InFile
#Browse_InFile
#Label_OutFile
#Input_OutFile
#Browse_OutFile
#Check_Frame
#Check_Debug
#Check_StripComments
#Check_OverwriteOld
#Start
#Exit
#Log_Frame
#Log_List
EndEnumeration
NewList DisableItems()
AddElement(DisableItems()):DisableItems()=#Input_InFile
AddElement(DisableItems()):DisableItems()=#Browse_InFile
AddElement(DisableItems()):DisableItems()=#Input_OutFile
AddElement(DisableItems()):DisableItems()=#Browse_OutFile
For i=#Check_StripComments To #Exit
AddElement(DisableItems()):DisableItems()=i
Next
Procedure DisableGadgets(state)
Shared DisableItems()
ForEach DisableItems()
DisableGadget(DisableItems(),state)
Next
EndProcedure
;}
;XIncludeFile(#PB_Compiler_Home+"Include\Macro.pbi") ;I'll just write it down here, because you don't have it :p
Macro Enquote(s)
Chr(34)+s+Chr(34)
EndMacro
Procedure.s FixedSys32Dir()
Protected Sys32.s = GetSystemDirectory() ; You probably don't have this function, use GetEnvironmentVariable("systemroot")+"\system32\"
If(Right(Sys32,1)<>"\")
Sys32+"\"
EndIf
ProcedureReturn Sys32
EndProcedure
Procedure.s FixedTempDir()
Protected Temp.s = GetTemporaryDirectory()
If(Right(Temp,1)<>"\")
Temp+"\"
EndIf
ProcedureReturn Temp
EndProcedure
Procedure PrettyPrinter(szInput.s, szOutput.s, Overwrite.b, StripComments.b, *lpCallbackProcedure)
Protected szCommandlineString.s = "", hFile, PrettyPrinter, PFlags, PString.s
If(FileSize(szInput)<0)
MessageBox_(WindowID(#Main),"Selected file could not be found!", "Error", #MB_ICONERROR)
ProcedureReturn 0
EndIf
Protected Script.s = FixedTempDir()+"HW2_PrettyPrinter.js"
szCommandlineString = Enquote(Script) + " " + Enquote(szInput)
If(Overwrite=0)
szCommandlineString + " " + Enquote(szOutput)
Else
szCommandlineString + " -o"
EndIf
If(StripComments=1)
szCommandlineString + " -s"
EndIf
szCommandlineString + " -verbose"
hFile = OpenFile(#PB_Any, Script)
If hFile
WriteData(hFile, ?HW2_PrettyPrinter, (?HW2_End-?HW2_PrettyPrinter))
CloseFile(hFile)
Else
ProcedureReturn -1
EndIf
PFlags = #PB_Program_Open | #PB_Program_Read | #PB_Program_Hide
PrettyPrinter = RunProgram(FixedSys32Dir()+"cscript.exe", szCommandlineString, GetPathPart(szInput), PFlags)
While ProgramRunning(PrettyPrinter)
PString = ReadProgramString(PrettyPrinter)
If PString
If *lpCallbackProcedure And FindString(LCase(PString),"microsoft",1)=0 ;Screw them! :p It's the script I'm interested in!
CallFunctionFast(*lpCallbackProcedure, PString) ; The callback procedure should recieve only 1 string.
EndIf
EndIf
Wend
If *lpCallbackProcedure
CallFunctionFast(*lpCallbackProcedure, "+OK Ready...")
EndIf
CloseProgram(PrettyPrinter)
DeleteFile(Script)
ProcedureReturn 1
EndProcedure
Procedure Logger(szString.s)
szString = ReplaceString(szString,Chr(9)," ")
AddGadgetItem(#Log_List, -1, szString)
SetGadgetState(#Log_List, CountGadgetItems(#Log_List)-1) ; Select the last item, so it scrolls there aswell.
EndProcedure
If OpenWindow(#Main, #PB_Ignore, #PB_Ignore, 350, 340, "PrettyPrinter")
If CreateGadgetList(WindowID(#Main))
Frame3DGadget(#Frame, 5, 5, 340, 170, "PrettyPrinter GUI by Imagine Programming, PrettyPrinter by Mikail")
Distance = 30 ; Ignore this, I just do this to keep even margins :p
TextGadget (#Label_InFile, 10, Distance+3, 40 , 22, "Input:")
StringGadget(#Input_InFile, 50, Distance, 225, 22, "", #PB_String_ReadOnly)
ButtonGadget(#Browse_InFile,280, Distance, 60, 22, "Browse")
Distance + 27
TextGadget (#Label_OutFile, 10, Distance+3, 40 , 22, "Output:")
StringGadget(#Input_OutFile, 50, Distance, 225, 22, "", #PB_String_ReadOnly)
ButtonGadget(#Browse_OutFile,280, Distance, 60, 22, "Browse")
ButtonGadget(#Start, 265, 168-20, 75, 22, "&Start")
ButtonGadget(#Exit , 265-80, 168-20, 75, 22, "&Exit")
Distance + 27
Frame3DGadget(#Check_Frame, 10, Distance, 330, 60, "Options")
Distance + 15
CheckBoxGadget(#Check_StripComments, 20, Distance, 100, 22, "Strip Comments")
Distance + 18
CheckBoxGadget(#Check_OverwriteOld, 20, Distance, 100, 22, "Overwrite Original")
Frame3DGadget(#Log_Frame, 5, 180, 340, 155, "Log")
ListViewGadget(#Log_List, 10, 195, 330, 135)
SetGadgetColor(#Log_List, #PB_Gadget_BackColor, RGB($CE,$CE,$CE))
EndIf
Repeat
Event = WaitWindowEvent()
E_GID = EventGadget()
Select Event
Case #PB_Event_Gadget
Select E_GID
Case #Exit : Event = #PB_Event_CloseWindow
Case #Start
In.s = GetGadgetText(#Input_InFile)
Out.s= GetGadgetText(#Input_OutFile)
SC = GetGadgetState(#Check_StripComments)
OO = GetGadgetState(#Check_OverwriteOld)
If(FileSize(In)<0)
MessageBox_(WindowID(#Main),"Please give a valid input file!", "Error", #MB_ICONERROR)
Continue
EndIf
If(FileSize(Out)<0)And(OO=1)
MessageBox_(WindowID(#Main),"Please give a valid output file!", "Error", #MB_ICONERROR)
Continue
EndIf
ClearGadgetItemList(#Log_List)
DisableGadgets(1)
PrettyPrinter(In,Out,OO,SC,@Logger())
DisableGadgets(0)
Case #Browse_InFile
f.s = OpenFileRequester("Select an input file", "", "Lua Scripts (*.lua)|*.lua|", 0)
If f
SetGadgetText(#Input_InFile, f)
If GetGadgetText(#Input_OutFile) = f
SetGadgetState(#Check_OverwriteOld,1)
DisableGadget(#Check_OverwriteOld,1)
Else
DisableGadget(#Check_OverwriteOld,0)
SetGadgetState(#Check_OverwriteOld,0)
EndIf
EndIf
Case #Browse_OutFile
f.s = OpenFileRequester("Select an output file", "", "Lua Scripts (*.lua)|*.lua|", 0)
If f
SetGadgetText(#Input_OutFile, f)
If GetGadgetText(#Input_InFile) = f
SetGadgetState(#Check_OverwriteOld,1)
DisableGadget(#Check_OverwriteOld,1)
Else
DisableGadget(#Check_OverwriteOld,0)
SetGadgetState(#Check_OverwriteOld,0)
EndIf
EndIf
EndSelect
EndSelect
Until Event = #PB_Event_CloseWindow
EndIf
End
;{ Datasection
DataSection
HW2_PrettyPrinter: IncludeBinary("HW2_PrettyPrinter.js")
HW2_End : Chr(0) ;To determine the length ?Label2-?Label1 :)
EndDataSection
;}





Leave a comment: