summaryrefslogtreecommitdiff
path: root/plugins/lua
diff options
context:
space:
mode:
authorAlexander Gavrilov2012-04-21 15:43:52 +0400
committerAlexander Gavrilov2012-04-21 15:43:52 +0400
commit3282ac3db216ef43cab5744631b57ed05bcf8a12 (patch)
tree330517cf6663113e0b9f4a575209ab658c5003d4 /plugins/lua
parentadbd351462f71db6a98b681eb9336524cc530092 (diff)
downloaddfhack-3282ac3db216ef43cab5744631b57ed05bcf8a12.tar.gz
dfhack-3282ac3db216ef43cab5744631b57ed05bcf8a12.tar.bz2
dfhack-3282ac3db216ef43cab5744631b57ed05bcf8a12.tar.xz
Add a hotkey command that sorts units in lists using lua comparators.
Diffstat (limited to 'plugins/lua')
-rw-r--r--plugins/lua/sort.lua32
-rw-r--r--plugins/lua/sort/units.lua20
2 files changed, 52 insertions, 0 deletions
diff --git a/plugins/lua/sort.lua b/plugins/lua/sort.lua
new file mode 100644
index 00000000..2318d7e0
--- /dev/null
+++ b/plugins/lua/sort.lua
@@ -0,0 +1,32 @@
+local _ENV = mkmodule('plugins.sort')
+
+local utils = require('utils')
+local units = require('plugins.sort.units')
+
+orders = orders or {}
+orders.units = units.orders
+
+function parse_ordering_spec(type,...)
+ local group = orders[type]
+ if group == nil then
+ dfhack.printerr('Invalid ordering class: '..tostring(type))
+ return nil
+ end
+
+ local specs = table.pack(...)
+ local rv = { }
+ for _,spec in ipairs(specs) do
+ local cm = group[spec]
+ if cm == nil then
+ dfhack.printerr('Unknown order for '..type..': '..tostring(spec))
+ return nil
+ end
+ rv[#rv+1] = cm
+ end
+
+ return rv
+end
+
+make_sort_order = utils.make_sort_order
+
+return _ENV \ No newline at end of file
diff --git a/plugins/lua/sort/units.lua b/plugins/lua/sort/units.lua
new file mode 100644
index 00000000..872e4942
--- /dev/null
+++ b/plugins/lua/sort/units.lua
@@ -0,0 +1,20 @@
+local _ENV = mkmodule('plugins.sort.units')
+
+local utils = require('utils')
+
+orders = orders or {}
+
+orders.name = {
+ key = function(unit)
+ return dfhack.TranslateName(unit.name)
+ end,
+ compare = utils.compare_name
+}
+
+orders.age = {
+ key = function(unit)
+ return dfhack.units.getAge(unit)
+ end
+}
+
+return _ENV \ No newline at end of file