diff --git a/README.md b/README.md index 2bc4e79..9217b1e 100644 --- a/README.md +++ b/README.md @@ -12,18 +12,18 @@ TL;DR --- I want the control of my layout. ## Use the layout -Use `layout-machi.layout.create_layout([LAYOUT_NAME}, [DEFAULT_REGIONS])` to instantiate the layout. +Use `layout_machi.layout.create_layout([LAYOUT_NAME}, [DEFAULT_REGIONS])` to instantiate the layout. For example: ``` -layout-machi.layout.create_layout("default", {}) +layout_machi.layout.create_layout("default", {}) ``` Creates a layout with no regions ## Use the editor -Call `layout-machi.editor.start_editor(data)` to enter the editor for the current layout (given it is a machi instance). +Call `layout_machi.editor.start_editor(data)` to enter the editor for the current layout (given it is a machi instance). `data` is am object for storing the history of the editing, initially `{}`. The editor starts with the open area of the entire workarea, taking command to split the current area into multiple sub-areas, then editing each of them. The editor is keyboard driven, accepting a number of command keys. @@ -39,10 +39,6 @@ By default A = B = 1. 7. `Backspace`: undo the last command. 8. `Escape`: exit the editor without saving the layout. -## Other functions - -`layout-machi.editor.cycle_region(c)` will fit a floating client into the closest region, then cycle through all regions. - ## Demos: I used `Super + /` for the editor and `Super + Tab` for fitting the windows. @@ -91,10 +87,21 @@ history ![](https://i.imgur.com/gzFr48V.gif) -## TODO +### Persistent history + +You need to specify the path of the history file in the editor data, then restore the persistent history by `layout_machi.editor.restore_data`. For example, + +``` +machi_layout_data = layout_machi.editor.restore_data({ history_file = ".machi-layout" }) +``` + +Then start the editor with the restored data. +For now the last 10 commands are persisted. + +## Other functions + +`layout_machi.editor.cycle_region(c)` will fit a floating client into the closest region, then cycle through all regions. - - Make history persistent - ## License Apache 2.0 --- See LICENSE diff --git a/editor.lua b/editor.lua index c2d0d5b..631557a 100644 --- a/editor.lua +++ b/editor.lua @@ -448,6 +448,7 @@ function start_editor(data) if key == "BackSpace" then pop_history() elseif key == "Escape" then + table.remove(data.cmds, #data.cmds) to_exit = true elseif key == "Up" or key == "Down" then if current_cmd ~= data.cmds[cmd_index] then @@ -495,6 +496,20 @@ function start_editor(data) table.remove(data.cmds, cmd_index) end data.cmds[#data.cmds + 1] = current_cmd + + if data.history_file then + -- only save the last 10 layouts + local file, err = io.open(data.history_file, "w") + if err then + print("cannot save history to " .. data.history_file) + else + for i = max(1, #data.cmds - 10), #data.cmds do + print("save cmd " .. data.cmds[i]) + file:write(data.cmds[i] .. "\n") + end + end + end + current_info = "Saved!" to_exit = true to_apply = true @@ -530,9 +545,27 @@ function start_editor(data) end) end +function restore_data(data) + if data.history_file then + local file, err = io.open(data.history_file, "r") + if err then + print("cannot read history from " .. data.history_file) + else + data.cmds = {} + for line in file:lines() do + print("restore cmd " .. line) + data.cmds[#data.cmds + 1] = line + end + end + end + + return data +end + return { set_region = set_region, cycle_region = cycle_region, start_editor = start_editor, + restore_data = restore_data, }