-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
139 lines (127 loc) · 4.18 KB
/
Copy pathinit.lua
File metadata and controls
139 lines (127 loc) · 4.18 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
print("FeelsDankMan!!!!")
function cmdEval(ctx)
table.remove(ctx.words, 1)
local input = table.concat(ctx.words, " ")
local source = ("local args = {...}\n" .. "local ctx = args[1]\n" .. "return " .. input)
ctx.channel:add_system_message(">>>" .. input)
local f, err = load(source)
if f == nil then
ctx.channel:add_system_message("!<" .. tostring(err))
else
ctx.channel:add_system_message("<< " .. tostring(f(ctx)))
end
end
function startswith(str, prefix)
return string.sub(str, 1, prefix:len()) == prefix
end
function cmdFoo(ctx)
table.remove(ctx.words, 1)
local sub = ctx.words[1]
if sub == "barbazA" then
ctx.channel:add_system_message("You used the first subcommand")
elseif sub == "bazbarB" then
ctx.channel:add_system_message("You used the second subcommand")
else
ctx.channel:add_system_message("You used neither subcommand. Try barbazA and bazbarB")
end
end
function cmd_filesystem(ctx)
table.remove(ctx.words, 1)
local filename = "test"
local TEST_STRING = "This is a test!\nHere is second line\n"
ctx.channel:add_system_message('Opening file "' .. filename .. '" for writing.')
local file = io.open(filename, "w")
ctx.channel:add_system_message("Writing " .. tostring(#TEST_STRING) .. " bytes.")
print("Write", txt)
file:write(TEST_STRING)
file:close()
ctx.channel:add_system_message('Opening file "' .. filename .. '" for reading.')
file = io.open(filename)
txt = file:read("a")
ctx.channel:add_system_message("Read " .. tostring(#txt) .. " bytes.")
file:close()
print(txt)
ctx.channel:add_system_message('Opening file "' .. filename .. '" for reading using alternate API.')
io.input(filename)
local i = 1
for l in io.lines() do
print("Line " .. tostring(i) .. ": " .. l)
i = i + 1
end
end
function cmd_http(ctx)
table.remove(ctx.words, 1)
local url = "http://localhost:8080/" .. table.concat(ctx.words, "%20")
local request = c2.HTTPRequest.create("Post", url)
print("Created request!")
_G.request = request
request:set_timeout(1000)
request:set_payload("TEST!")
request:set_header("X-Test", "Testing!")
request:set_header("X-Test", "Testing 2!")
request:set_header("Content-Type", "text/plain")
request:on_success(function(res)
print("Success!")
print(res.data)
ctx.channel:add_system_message("Success! " .. res.status)
end)
request:on_error(function(res)
print("Error!")
print(res.data)
print("--------")
ctx.channel:add_system_message("Error! " .. (res.status or "nil") .. " " .. res.error)
end)
request:finally(function()
print("Finally")
ctx.channel:add_system_message("Finally!")
end)
request:execute()
end
function onCompletion(ev)
local cursor_position = ev.cursor_position
local full_text_content = ev.full_text_content
local is_first_word = ev.is_first_word
local query = ev.query
local list = {
hide_others = false,
values = {},
}
c2.Channel
.by_name("pajlada", c2.Platform.Twitch)
:add_system_message("Completing! " .. query .. "; " .. full_text_content .. "!")
if startswith(full_text_content, "/foo ") then
-- note this doesn't account for completion being anything after the 2nd argument
list.values = { "barbazA", "bazbarB" }
list.hide_others = true
end
return list
end
c2.register_command("/eval", cmdEval)
c2.register_command("/foo", cmdFoo)
c2.register_command("/test-fs", cmd_filesystem)
c2.register_command("/test-http", cmd_http)
c2.register_callback(c2.EventType.CompletionRequested, onCompletion)
-- Ensure context menu works
c2.windows:on_channelview_context_menu_requested(function(args)
args.menu:add_action("Testing", function()
args.channel:add_system_message("Clicked!")
end)
args.menu:insert_action(1, "Show me the element!", function()
local msg = "text='" .. args.message.message_text .. "'"
if args.message_element then
msg = msg .. " element=" .. args.message_element.type
end
if args.split then
msg = msg .. " split={channel=" .. args.split.channel:get_name() .. "}"
end
args.channel:add_system_message(msg)
end)
local sub = args.menu:add_menu("Submenu")
sub:add_action("An action", function()
args.channel:add_system_message("An action... not much else")
end)
sub:add_separator()
sub:add_action("Second one", function()
args.channel:add_system_message("second one")
end)
end)