11local _ENV = mkmodule (' plugins.stockpiles' )
22
33local argparse = require (' argparse' )
4+ local dialogs = require (' gui.dialogs' )
45local gui = require (' gui' )
56local logistics = require (' plugins.logistics' )
67local overlay = require (' plugins.overlay' )
@@ -34,7 +35,7 @@ local function print_status()
3435 end
3536end
3637
37- local function list_dir (path , prefix , filters )
38+ local function list_dir (path , prefix , filters , cb )
3839 local paths = dfhack .filesystem .listdir_recursive (path , 0 , false )
3940 if not paths then
4041 dfhack .printerr ((' Cannot find stockpile settings directory: "%s"' ):format (path ))
@@ -56,14 +57,15 @@ local function list_dir(path, prefix, filters)
5657 end
5758 if not matched then goto continue end
5859 end
59- print ((' %s%s' ):format (prefix , v .path :sub (1 , - 9 )))
60+ cb ((' %s%s' ):format (prefix , v .path :sub (1 , - 9 )))
6061 :: continue::
6162 end
6263end
6364
64- local function list_settings_files (filters )
65- list_dir (STOCKPILES_DIR , ' ' , filters )
66- list_dir (STOCKPILES_LIBRARY_DIR , ' library/' , filters )
65+ local function list_settings_files (filters , cb )
66+ cb = cb or print
67+ list_dir (STOCKPILES_DIR , ' ' , filters , cb )
68+ list_dir (STOCKPILES_LIBRARY_DIR , ' library/' , filters , cb )
6769end
6870
6971local function assert_safe_name (name )
@@ -240,59 +242,52 @@ function parse_commandline(args)
240242 return true
241243end
242244
243- ---- ----------------
244- -- dialogs
245- ---- ----------------
246-
247- StockpilesExport = defclass (StockpilesExport , widgets .Window )
248- StockpilesExport .ATTRS {frame_title = ' Export stockpile settings' , frame = {w = 33 , h = 15 }, resizable = true }
249-
250- function StockpilesExport :init ()
251- self :addviews {
252- widgets .EditField {
253- view_id = ' edit' ,
254- frame = {t = 0 , l = 0 , r = 0 },
255- label_text = ' name: ' ,
256- on_char = function (ch )
257- return not ch :match (BAD_FILENAME_REGEX )
258- end ,
259- }, widgets .Label {frame = {t = 2 , l = 0 }, text = ' Include which elements?' },
260- widgets .ToggleHotkeyLabel {frame = {t = 4 , l = 0 }, label = ' General settings' , initial_option = false },
261- widgets .ToggleHotkeyLabel {
262- frame = {t = 5 , l = 0 },
263- label = ' Container settings' ,
264- initial_option = false ,
265- }, widgets .ToggleHotkeyLabel {frame = {t = 6 , l = 0 }, label = ' Categories' , initial_option = true },
266- widgets .ToggleHotkeyLabel {frame = {t = 7 , l = 0 }, label = ' Subtypes' , initial_option = true },
267- widgets .HotkeyLabel {
268- frame = {t = 10 , l = 0 },
269- label = ' export' ,
270- key = ' SELECT' ,
271- enabled = function ()
272- return # self .subviews .edit .text > 0
273- end ,
274- on_activate = self :callback (' on_submit' ),
275- },
276- }
277- end
245+ ---- ---------------------
246+ -- import/export dialogs
247+ ---- ---------------------
278248
279- function StockpilesExport :on_submit (text )
280- self :dismiss ()
249+ local function get_import_choices ()
250+ local filenames = {}
251+ list_settings_files ({}, function (fname ) table.insert (filenames , fname ) end )
252+ return filenames
281253end
282254
283- StockpilesExportScreen = defclass (StockpilesExportScreen , gui .ZScreenModal )
284- StockpilesExportScreen .ATTRS {focus_path = ' stockpiles/export' }
285-
286- function StockpilesExportScreen :init ()
287- self :addviews {StockpilesExport {}}
288- end
289-
290- function StockpilesExportScreen :onDismiss ()
291- export_view = nil
255+ local function do_import ()
256+ local sp = dfhack .gui .getSelectedStockpile (true )
257+ local dlg
258+ local function get_dlg () return dlg end
259+ dlg = dialogs .ListBox {
260+ frame_title = ' Import/Delete Stockpile Settings' ,
261+ with_filter = true ,
262+ choices = get_import_choices (),
263+ on_select = function (_ , choice ) import_settings (choice .text , {id = sp and sp .id }) end ,
264+ dismiss_on_select2 = false ,
265+ on_select2 = function (_ , choice )
266+ if choice .text :startswith (' library/' ) then return end
267+ local fname = (' %s/%s.dfstock' ):format (STOCKPILES_DIR , choice .text )
268+ if not dfhack .filesystem .isfile (fname ) then return end
269+ dialogs .showYesNoPrompt (' Delete saved stockpile settings file?' ,
270+ ' Are you sure you want to delete "' .. fname .. ' "?' , nil ,
271+ function ()
272+ print (' deleting ' .. fname )
273+ os.remove (fname )
274+ local list = get_dlg ().subviews .list
275+ local filter = list :getFilter ()
276+ list :setChoices (get_import_choices (), list :getSelected ())
277+ list :setFilter (filter )
278+ end )
279+ end ,
280+ select2_hint = ' Delete file' ,
281+ }:show ()
292282end
293283
294284local function do_export ()
295- export_view = export_view and export_view :raise () or StockpilesExportScreen {}:show ()
285+ local sp = dfhack .gui .getSelectedStockpile (true )
286+ dialogs .InputBox {
287+ frame_title = ' Export Stockpile Settings' ,
288+ text = ' Please enter a filename' ,
289+ on_input = function (text ) export_settings (text , {id = sp and sp .id }) end ,
290+ }:show ()
296291end
297292
298293---- ----------------
@@ -428,10 +423,11 @@ end
428423StockpilesOverlay = defclass (StockpilesOverlay , overlay .OverlayWidget )
429424StockpilesOverlay .ATTRS {
430425 desc = ' Shows a panel when a stockpile is selected for stockpile automation.' ,
431- default_pos = {x = 5 , y = 44 },
426+ default_pos = {x = 5 , y = 43 },
432427 default_enabled = true ,
428+ version = 2 ,
433429 viewscreens = ' dwarfmode/Stockpile/Some/Default' ,
434- frame = {w = 49 , h = 5 },
430+ frame = {w = 49 , h = 6 },
435431}
436432
437433function StockpilesOverlay :init ()
@@ -447,21 +443,21 @@ function StockpilesOverlay:init()
447443 frame_background = gui .CLEAR_PEN ,
448444 visible = is_expanded ,
449445 subviews = {
450- -- widgets.HotkeyLabel{
451- -- frame={t=0, l=0},
452- -- label='import settings',
453- -- auto_width=true,
454- -- key='CUSTOM_CTRL_I',
455- -- on_activate=do_import,
456- -- }, widgets.HotkeyLabel{
457- -- frame={t=1, l=0},
458- -- label='export settings',
459- -- auto_width=true,
460- -- key='CUSTOM_CTRL_E',
461- -- on_activate=do_export,
462- -- },
463- widgets .Panel {
446+ widgets .HotkeyLabel {
464447 frame = {t = 0 , l = 0 },
448+ label = ' import' ,
449+ auto_width = true ,
450+ key = ' CUSTOM_CTRL_I' ,
451+ on_activate = do_import ,
452+ }, widgets .HotkeyLabel {
453+ frame = {t = 0 , l = 16 },
454+ label = ' export' ,
455+ auto_width = true ,
456+ key = ' CUSTOM_CTRL_E' ,
457+ on_activate = do_export ,
458+ },
459+ widgets .Panel {
460+ frame = {t = 1 , l = 0 },
465461 subviews = {
466462 widgets .Label {
467463 frame = {t = 0 , l = 0 , h = 1 },
0 commit comments