-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpink-cli
executable file
·82 lines (73 loc) · 1.91 KB
/
pink-cli
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
#!/bin/env lua
if not arg[1] then
error('\nUsage: ' .. arg[0] .. ' [-v] [format] game.ink\n')
end
local parser = require('pink.parser')
local formatter = require('pink.formatter')
local runtime = require('pink.runtime')
local logging = require('pink.logging')
local function read(file)
local f = io.open(file, 'rb')
if not f then
error('failed to open "' .. file .. '"')
end
local content = f:read('*all')
f:close()
return content
end
local function basedir(str)
return string.gsub(str, '(.*)(/.*)', '%1')
end
local parse
parse = function(file)
local parsed = {}
for _, t in ipairs(parser(read(file), file)) do
if t[2] and t[1] == 'include' then
for _, includedNode in ipairs(parse(basedir(file) .. '/' .. t[2])) do
table.insert(parsed, includedNode)
end
else
table.insert(parsed, t)
end
end
return parsed
end
if arg[1] == '-v' then
logging.debugEnabled = true
table.remove(arg, 1)
end
local format = false
if arg[1] == 'format' then
format = true
table.remove(arg, 1)
end
local filename = arg[1]
if format then
print(formatter(parse(filename)))
return
end
local story = runtime(parse(filename))
while true do
while story.canContinue do
print(story.continue())
if #story.currentTags > 0 then
print('# tags: ' .. table.concat(story.currentTags, ', ')) -- TODO configurable
end
end
if #story.currentChoices == 0 then
break
end
print()
for i = 1, #story.currentChoices do
print(i .. ': ' .. story.currentChoices[i].text)
end
io.write('?> ')
local answer = tonumber(io.read('*number'))
if not answer then
error('missing answer')
end
if not answer or answer > #story.currentChoices then
error('invalid answer: ' .. tostring(answer))
end
story.chooseChoiceIndex(answer)
end