forked from DerpleDude/luaconsole
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
229 lines (183 loc) · 6.88 KB
/
Copy pathinit.lua
File metadata and controls
229 lines (183 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
local mq = require('mq')
local ImGui = require('ImGui')
local Icons = require('mq.ICONS')
local Zep = require('Zep')
local luaConsole = Zep.Console.new("##LuaConsole")
luaConsole.maxBufferLines = 1000
luaConsole.autoScroll = true
local luaEditor = Zep.Editor.new('##LuaEditor')
local luaBuffer = luaEditor:InitWithText("editor.lua", "")
local execRequested = false
local showTimestamps = true
local execCoroutine = nil
local status = "Idle..."
local openGUI = true
local shouldDrawGUI = true
local CHANNEL_COLOR = IM_COL32(215, 154, 66)
local settings_path = mq.configDir .. '/luaconsole_settings.lua'
local function LoadSettings()
local settings, err = loadfile(settings_path)
if not err and settings then
luaBuffer:SetText(settings().editbox)
end
end
local function SaveSettings()
mq.pickle(settings_path, { editbox = luaBuffer:GetText(), })
end
local function LogTimestamp()
if showTimestamps then
local now = os.date('%H:%M:%S')
luaConsole:AppendTextUnformatted(string.format('\aw[\at%s\aw] ', now))
end
end
local function LogToConsole(...)
LogTimestamp()
luaConsole:AppendText(CHANNEL_COLOR, ...)
end
local function Exec(scriptText)
local func, err = load(scriptText, "LuaConsoleScript", "t")
if not func then
return false, err
end
local locals = setmetatable({}, { __index = _G, })
locals.mq = setmetatable({}, { __index = mq, })
locals.print = function(...)
LogTimestamp()
luaConsole:PushStyleColor(Zep.ConsoleCol.Text, CHANNEL_COLOR)
for _, arg in ipairs({ ..., }) do
luaConsole:AppendTextUnformatted(tostring(arg))
end
luaConsole:AppendTextUnformatted('\n')
luaConsole:PopStyleColor()
end
locals.printf = function(text, ...)
LogTimestamp()
luaConsole:AppendText(CHANNEL_COLOR, text, ...)
end
locals.mq.exit = function()
execCoroutine = nil
end
locals.hi = 3
setfenv(func, locals)
local success, msg = pcall(func)
return success, msg or ""
end
local function ExecCoroutine()
local scriptText = luaBuffer:GetText()
return coroutine.create(function()
local success, msg = Exec(scriptText)
if not success then
LogToConsole("\ar" .. msg)
end
end)
end
local function RenderConsole()
local contentSizeX, contentSizeY = ImGui.GetContentRegionAvail()
luaConsole:Render(ImVec2(contentSizeX, math.max(200, (contentSizeY - 10))))
end
local function RenderEditor()
local yPos = ImGui.GetCursorPosY()
local footerHeight = 35
local editHeight = (ImGui.GetWindowHeight() * .5) - yPos - footerHeight
luaEditor:Render(ImVec2(ImGui.GetWindowWidth() * 0.98, editHeight))
end
local function RenderTooltip(text)
if ImGui.IsItemHovered() then
ImGui.SetTooltip(text)
end
end
local function CenteredButton(label)
local style = ImGui.GetStyle()
local framePaddingX = style.FramePadding.x * 2
local framePaddingY = style.FramePadding.y * 2
local availableWidth = ImGui.GetContentRegionAvailVec().x
local availableHeight = 30
local textSizeVec = ImGui.CalcTextSizeVec(label)
local textWidth = textSizeVec.x
local textHeight = textSizeVec.y
local paddingX = (availableWidth - textWidth - framePaddingX) / 2
local paddingY = (availableHeight - textHeight - framePaddingY) / 2
if paddingX > 0 then
ImGui.SetCursorPosX(ImGui.GetCursorPosX() + paddingX)
end
if paddingY > 0 then
ImGui.SetCursorPosY(ImGui.GetCursorPosY() + paddingY)
end
return ImGui.SmallButton(string.format("%s", label))
end
local function RenderToolbar()
if ImGui.BeginTable("##LuaConsoleToolbar", 5, ImGuiTableFlags.Borders) then
ImGui.TableSetupColumn("##LuaConsoleToolbarCol1", ImGuiTableColumnFlags.WidthFixed, 30)
ImGui.TableSetupColumn("##LuaConsoleToolbarCol2", ImGuiTableColumnFlags.WidthFixed, 30)
ImGui.TableSetupColumn("##LuaConsoleToolbarCol3", ImGuiTableColumnFlags.WidthFixed, 30)
ImGui.TableSetupColumn("##LuaConsoleToolbarCol4", ImGuiTableColumnFlags.WidthFixed, 180)
ImGui.TableSetupColumn("##LuaConsoleToolbarCol5", ImGuiTableColumnFlags.WidthStretch, 200)
ImGui.TableNextColumn()
if execCoroutine and coroutine.status(execCoroutine) ~= 'dead' then
if CenteredButton(Icons.MD_STOP) then
execCoroutine = nil
end
RenderTooltip("Stop Script")
else
if CenteredButton(Icons.MD_PLAY_ARROW) then
execRequested = true
end
RenderTooltip("Execute Script (Ctrl+Enter)")
end
ImGui.TableNextColumn()
if CenteredButton(Icons.MD_CLEAR) then
luaBuffer:SetText("")
end
RenderTooltip("Clear Script")
ImGui.TableNextColumn()
if CenteredButton(Icons.MD_PHONELINK_ERASE) then
luaConsole:Clear()
end
RenderTooltip("Clear Console")
ImGui.TableNextColumn()
showTimestamps = ImGui.Checkbox("Print Time Stamps", showTimestamps)
ImGui.TableNextColumn()
ImGui.Text("Status: " .. status)
ImGui.EndTable()
end
end
local function LuaConsoleGUI()
ImGui.SetNextWindowSize(ImVec2(800, 600), ImGuiCond.FirstUseEver)
ImGui.SetNextWindowPos(ImVec2(ImGui.GetIO().DisplaySize.x / 2 - 400, ImGui.GetIO().DisplaySize.y / 2 - 300), ImGuiCond.FirstUseEver)
openGUI, shouldDrawGUI = ImGui.Begin("Lua Console - By: Derple", openGUI, ImGuiWindowFlags.None)
if shouldDrawGUI then
if (ImGui.IsWindowHovered(ImGuiHoveredFlags.ChildWindows) and (ImGui.IsKeyChordPressed(ImGuiMod.Ctrl) and ImGui.IsKeyPressed(ImGuiKey.Enter))) then
execRequested = true
end
RenderEditor()
RenderToolbar()
RenderConsole()
end
ImGui.End()
end
mq.imgui.init('LuaConsoleGUI', LuaConsoleGUI)
mq.bind('/lc', function()
openGUI = not openGUI
end)
LogToConsole("\awLua Console by: \amDerple \awLoaded...")
LoadSettings()
while openGUI do
if execRequested then
execRequested = false
execCoroutine = ExecCoroutine()
coroutine.resume(execCoroutine)
status = "Running..."
end
if execCoroutine and coroutine.status(execCoroutine) ~= 'dead' then
coroutine.resume(execCoroutine)
else
execCoroutine = nil
status = "Idle..."
end
if luaBuffer:HasFlag(Zep.BufferFlags.Dirty) then
SaveSettings()
luaBuffer:ClearFlags(Zep.BufferFlags.Dirty)
end
mq.delay(10)
end
SaveSettings()