-
Notifications
You must be signed in to change notification settings - Fork 2
/
Shell.lua
182 lines (148 loc) · 4.23 KB
/
Shell.lua
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
--[[----------------------------------------------------------------------------
Lightroom Lua Shell
Written in 2011 by Andrew Filer <afiler@afiler.com>
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along with
this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
------------------------------------------------------------------------------
Shell.lua
------------------------------------------------------------------------------]]
local LrFunctionContext = import 'LrFunctionContext'
local LrBinding = import 'LrBinding'
local LrDialogs = import 'LrDialogs'
local LrView = import 'LrView'
local LrLogger = import 'LrLogger'
local LrColor = import 'LrColor'
local LrApplication = import 'LrApplication'
local LrTasks = import 'LrTasks'
-- Environment for shell
local env = {}
for k, v in pairs(_G) do env[k] = v end
local function dump(var, level)
if not level then level = 0 end
if level > 3 then return '#' end
-- return DataDumper(result, '')
local t = type(var)
if t == 'string' or t == 'number' then
return var
elseif t == 'table' then
local output = {}
local metatable = getmetatable(var)
if not metatable then metatable = '' end
for k, v in pairs(var) do
if k == '_parent' then
table.insert(output, k..' = duh')
else
table.insert(output, k..' = '..dump(v, level+1))
end
end
return metatable..' {'..table.concat(output, ', ')..'}\n'
else
return '['..t..']'
end
end
local function shell()
LrFunctionContext.callWithContext( "shell", function( context )
local f = LrView.osFactory()
env.catalog = LrApplication.activeCatalog()
env.target = env.catalog:getTargetPhoto()
env.targets = env.catalog:getTargetPhotos()
local historyField = f:edit_field {
fill_horizontal = 1,
fill_vertical = 1,
height = 200,
value = [[
Lightroom Lua shell
Catalog: ]]..env.catalog:getPath()..[[
]]}
env.v = historyView
local function clear()
historyField.value = ""
end
env.clear = clear
local function print(val)
if not val then val = 'nil' end
historyField.value = historyField.value..val.."\n"
end
env.print = print
env.keys = function(t)
s = ''
for k, v in pairs(t) do s = s..k..' ' end
print(s)
end
local function validator(view, cmd, async)
if not async then print("> "..cmd) end
local output, err, f
--async = true
-- Try first as an immediate value
local immediate = true
if async then
f = loadstring("import('LrTasks').startAsyncTask(function() print('=> '.."..cmd..") end)")
else
f = loadstring("return("..cmd..")")
end
-- If this doesn't compile, try as a block without a return value
if not f then
immediate = false
if async then
f, err = loadstring("import('LrTasks').startAsyncTask(function() "..cmd.." end)")
else
f, err = loadstring(cmd)
end
end
if not f then
-- Failed to compile
print("!! "..err)
else
-- Compiled, now run
if false and async then
f = function()
if immediate then
f = loadstring("print ('=> '..dump("..cmd.."))")
end
import('LrTasks').startAsyncTask(f)
end
end
local success, result = LrFunctionContext.pcallWithEnvironment(f, env, cmd)
if success and (result or immediate) then
print("-> "..dump(result))
elseif not success then
if not async and result == 'We can only wait from within a task' then
validator(view, cmd, true)
else
print("## "..result)
end
end
end
return false, cmd
end
local inputField = f:edit_field {
immediate = false,
width_in_chars = 80,
height_in_lines = 3,
value = "",
validate = validator
}
local c = f:column {
spacing = f:dialog_spacing(),
fill_horizontal = 1,
fill_vertical = 1,
f:row {
fill_horizontal = 1,
fill_vertical = 1,
historyField,
}, -- end f:row
f:row {
inputField,
}, -- end row
} -- end column
LrDialogs.presentModalDialog {
title = "Lua Shell",
resizable = true,
contents = c
}
end)
end
shell()