Remember commands from the execute buffer between sessions

Posted by Kahenraz on Fri 16 Sep 2016 11:37 PM — 3 posts, 17,684 views.

#0
It would be nice if a configurable amount of commands last entered into the execute buffer could be remembered between sessions.

Sometimes I have aliases setup for testing plugins and I want to close the world and reconnect for testing. But then I lose my buffer history where I had some tests for easy access with the up arrow.

It would help a lot for debugging if the last few commands could be saved in the world file and reloaded once reopened.
Australia Forum Administrator #1
You can serialize any data (including tables) into a string. See: http://www.gammon.com.au/forum/?id=4960

Now, given that, you can make a world function for "on world save" (see the Scripting configuration tab). In that function you can find some or all of the command history. See:

Template:function=GetCommandList
GetCommandList

The documentation for the GetCommandList script function is available online. It is also in the MUSHclient help file.



Now, given that table of commands, serialize that into a variable, and then that variable will be saved when you save the world (or write it to a file if you want to not bother saving the world file).

Then next time you start the client you can put things back into the command history with SetCommand and PushCommand:

Template:function=SetCommand
SetCommand

The documentation for the SetCommand script function is available online. It is also in the MUSHclient help file.



Template:function=PushCommand
PushCommand

The documentation for the PushCommand script function is available online. It is also in the MUSHclient help file.



Example code in script file:


local CommandsFile = GetInfo (66) .. "commands.txt"  -- MUSHclient directory
require "serialize"
  
function OnWorldSave ()
  local f = assert (io.open (CommandsFile, "w"))
  f:write ("commands = " .. serialize.save_simple (GetCommandList (100) or {} ))
  f:close ()
end -- OnWorldSave

function OnWorldOpen ()
  local f = io.open (CommandsFile, "r")
  if not f then
    return
  end -- of no file found

  -- read file in 
  local commandsTable = assert (f:read ("*a"))  -- read all of it
  f:close ()

  -- load into "t.commands"
  local t = {}  
  setfenv (assert (loadstring (commandsTable)), t) ()

  -- push into command history
  
  for i = #t.commands, 1, -1 do
    SetCommand (t.commands [i])
    PushCommand ()
  end -- for
  
  ColourNote ("orange", "", "Loaded " .. #t.commands .. 
              " commands into command history")
end -- OnWorldOpen


You need to put "OnWorldOpen" and "OnWorldSave" into the appropriate boxes in the scripting configuration dialog.

This saves the last 100 commands. Change the "100" in the code to something else if you want.
Amended on Sat 17 Sep 2016 03:05 AM by Nick Gammon
#2
I'm still learning the nuances of Lua and Mush but I'm picking it up pretty fast. The scripting capabilities really make this client amazing.

Thanks a lot for the code and your advice. :)