1- -- The DFHack in-game launcher
1+ -- The DFHack in-game command launcher
22-- @module =true
33
44local gui = require (' gui' )
@@ -161,10 +161,12 @@ function EditPanel:init()
161161 on_change = self :callback (' on_search_text' ),
162162 on_unfocus = function ()
163163 self :reset_history_idx ()
164+ self .subviews .search .text = ' '
164165 self .subviews .editfield :setFocus (true ) end ,
165166 on_submit = function ()
166- self .subviews .search .text = ' '
167- self .on_submit (self .subviews .editfield .text ) end },
167+ self .on_submit (self .subviews .editfield .text ) end ,
168+ on_submit2 = function ()
169+ self .on_submit2 (self .subviews .editfield .text ) end },
168170 }
169171end
170172
@@ -240,7 +242,10 @@ function EditPanel:onInput(keys)
240242 self :on_search_text (self .subviews .search .text )
241243 return true
242244 elseif keys .A_CARE_MOVE_W then -- Alt-Left
243- self .on_change (self :pop_text ())
245+ local text = self :pop_text ()
246+ if text then
247+ self .on_change (text )
248+ end
244249 return true
245250 end
246251end
250255--
251256HelpPanel = defclass (HelpPanel , widgets .Panel )
252257
258+ local DEFAULT_HELP_TEXT = [[ Welcome to DFHack!
259+
260+ Type a command to see it's help text here. Hit ENTER
261+ to run the command, or Shift-ENTER to run the
262+ command and close this dialog. The dialog also
263+ closes automatically if you run a command that shows
264+ a new GUI screen.
265+
266+ Not sure what to do? Run the "help" command to get
267+ started.
268+
269+ To see help for this command launcher, type
270+ "launcher" and autocomplete to "gui/launcher" with
271+ the TAB key.]]
272+
253273function HelpPanel :init ()
254274 self .cur_entry = " "
255275
@@ -264,7 +284,7 @@ function HelpPanel:init()
264284 STANDARDSCROLL_PAGEUP = ' -page' ,
265285 STANDARDSCROLL_PAGEDOWN = ' +page' ,
266286 },
267- text_to_wrap = ' Welcome to DFHack! ' }
287+ text_to_wrap = DEFAULT_HELP_TEXT }
268288 }
269289end
270290
@@ -369,12 +389,16 @@ function LauncherUI:do_autocomplete(delta)
369389 self .input_is_worth_saving = false
370390end
371391
372- local function launch (initial_help )
392+ local function launch (kwargs )
373393 view = LauncherUI {parent_focus = dfhack .gui .getCurFocus (true )}
374394 view :show ()
375395 view :on_edit_input (' ' )
376- if initial_help then
377- view .subviews .help :set_help (initial_help )
396+ if kwargs .initial_help then
397+ view .subviews .help :set_help (kwargs .initial_help )
398+ end
399+ if kwargs .command then
400+ view .subviews .edit :set_text (kwargs .command )
401+ view :on_edit_input (kwargs .command )
378402 end
379403end
380404
@@ -389,19 +413,22 @@ function LauncherUI:run_command(reappear, text)
389413 dfhack .addCommandToHistory (HISTORY_ID , HISTORY_FILE , text )
390414 add_history (history , history_set , text )
391415 local output = dfhack .run_command_silent (text )
392- if not reappear then
393- return
394- end
395416 -- if we displayed a new dfhack screen, don't come back up even if reappear
396- -- is true so the user can interact with the new screen
417+ -- is true. otherwise, the user can't interact with the new screen. if we're
418+ -- not reappearing with the output, print the output to the console.
397419 local parent_focus = dfhack .gui .getCurFocus (true )
398- if parent_focus :startswith (' dfhack/' ) and
399- parent_focus ~= self .parent_focus then
420+ if not reappear or (parent_focus :startswith (' dfhack/' ) and
421+ parent_focus ~= self .parent_focus ) then
422+ if # output > 0 then
423+ print (' Output from command run from gui/launcher:' )
424+ print (' > ' .. text )
425+ print (output )
426+ end
400427 return
401428 end
402429 -- reappear and show the command output
403430 local initial_help = (' > %s\n\n %s' ):format (text , output )
404- launch (initial_help )
431+ launch ({ initial_help = initial_help } )
405432end
406433
407434function LauncherUI :getWantedFrameSize ()
@@ -466,5 +493,6 @@ if view then
466493 -- hitting the launcher hotkey while it is open should close the dialog
467494 view :dismiss ()
468495else
469- launch ()
496+ local args = {... }
497+ launch ({command = table.concat (args , ' ' )})
470498end
0 commit comments