From 2ca22c2cc10ea4201d5f85b4c63428c1632fc2b6 Mon Sep 17 00:00:00 2001 From: Xinhao Yuan Date: Fri, 12 Jul 2019 10:05:58 -0400 Subject: [PATCH] Incorporating comments from reddit. Thanks u/sigprof. 1. Removed unintended global names 2. The default history file changed to `gfs.get_cache_dir() .. "/history_machi"` 3. `editor` will detect if the layout is machi before start 4. Added `machi_` prefix for machi fields in the layout objects. --- README.md | 3 +-- editor.lua | 39 +++++++++++++++++++++++---------------- layout.lua | 13 +++++++------ switcher.lua | 7 ++++--- 4 files changed, 35 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index b1d8a67..fe47867 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,7 @@ The editor will restore the last setups of the layouts based on their names. ## Editor Call `editor = machi.editor.create()` to create an editor. -To edit the current layout on screen `scr`, call `editor.start_interactive(scr)`. -If `screen` is ignored, `screen.focused()` will be used. +To edit the layout `l` on screen `s`, call `editor.start_interactive(s = awful.screen.focused(), l = awful.layout.get(s))`. ### The layout editing command diff --git a/editor.lua b/editor.lua index 722ddcd..65f2053 100644 --- a/editor.lua +++ b/editor.lua @@ -11,12 +11,14 @@ local api = { keygrabber = require("awful.keygrabber"), naughty = require("naughty"), gears = require("gears"), + gfs = require("gears.filesystem"), lgi = require("lgi"), dpi = require("beautiful.xresources").apply_dpi, } local function with_alpha(col, alpha) - _, r, g, b, a = col:get_rgba() + local r, g, b + _, r, g, b, _ = col:get_rgba() return api.lgi.cairo.SolidPattern.create_rgba(r, g, b, alpha) end @@ -69,12 +71,12 @@ end -- @param cycle whether to cycle the region if the window is already in machi -- @return whether any actions have been taken on the client local function fit_region(c, cycle) - layout = api.layout.get(c.screen) - regions = layout.get_regions and layout.get_regions(c.screen.workarea) + local layout = api.layout.get(c.screen) + local regions = layout.machi_get_regions and layout.machi_get_regions(c.screen.workarea) if type(regions) ~= "table" or #regions < 1 then return false end - current_region = c.machi_region or 1 + local current_region = c.machi_region or 1 if not is_tiling(c) then -- find out which region has the most intersection, calculated by a cap b / a cup b c.machi_region = machi.layout.find_region(c, regions) @@ -157,7 +159,7 @@ end local function create(data) if data == nil then data = restore_data({ - history_file = ".machi_history", + history_file = api.gfs.get_cache_dir() .. "/history_machi", history_save_max = 100, gap = api.beautiful.useless_gap, }) @@ -445,11 +447,21 @@ local function create(data) return key end - local function start_interactive(screen) + local function start_interactive(screen, layout) + screen = screen or api.screen.focused() + layout = layout or api.layout.get(screen) + + if layout.machi_set_cmd == nil then + api.naughty.notify({ + text = "The layout to edit is not machi", + timeout = 3, + }) + return + end + local cmd_index = #data.cmds + 1 data.cmds[cmd_index] = "" - screen = screen or api.screen.focused() local screen_x = screen.geometry.x local screen_y = screen.geometry.y @@ -577,7 +589,7 @@ local function create(data) print("restore history #" .. tostring(cmd_index) .. ":" .. data.cmds[cmd_index]) init(screen.workarea) for i = 1, #data.cmds[cmd_index] do - cmd = data.cmds[cmd_index]:sub(i, i) + local cmd = data.cmds[cmd_index]:sub(i, i) push_history() local ret = handle_command(cmd) @@ -608,8 +620,6 @@ local function create(data) end else if key == "Return" then - local layout = api.layout.get(screen) - table.remove(data.cmds, #data.cmds) -- remove duplicated entries local j = 1 @@ -624,7 +634,7 @@ local function create(data) end -- bring the current cmd to the front data.cmds[#data.cmds + 1] = current_cmd - data.last_cmd[layout.name] = current_cmd + data.last_cmd[layout.machi_instance_name] = current_cmd if data.history_file then local file, err = io.open(data.history_file, "w") @@ -654,11 +664,8 @@ local function create(data) if to_exit then print("interactive layout editing ends") if to_apply then - local layout = api.layout.get(screen) - if layout.set_cmd then - layout.set_cmd(current_cmd) - api.layout.arrange(screen) - end + layout.machi_set_cmd(current_cmd) + api.layout.arrange(screen) api.gears.timer{ timeout = 1, autostart = true, diff --git a/layout.lua b/layout.lua index f9903a2..f3a156d 100644 --- a/layout.lua +++ b/layout.lua @@ -36,7 +36,7 @@ local function find_region(c, regions) return choice end -function create(name, editor) +local function create(name, editor) local priv = { name = name, editor = editor, @@ -53,7 +53,7 @@ function create(name, editor) return priv.regions_cache[key] end - function arrange(p) + local function arrange(p) local wa = p.workarea local cls = p.clients local regions = get_regions(wa) @@ -87,7 +87,7 @@ function create(name, editor) end end - function set_cmd(cmd) + local function set_cmd(cmd) if priv.cmd ~= cmd then priv.cmd = cmd priv.regions_cache = {} @@ -129,11 +129,12 @@ function create(name, editor) end return { - name = name, + name = "machi", arrange = arrange, - set_cmd = set_cmd, - get_regions = get_regions, resize_handler = resize_handler, + machi_instance_name = name, + machi_set_cmd = set_cmd, + machi_get_regions = get_regions, } end diff --git a/switcher.lua b/switcher.lua index 9a1227f..04cc10c 100644 --- a/switcher.lua +++ b/switcher.lua @@ -34,7 +34,8 @@ local function max(a, b) end local function with_alpha(col, alpha) - _, r, g, b, a = col:get_rgba() + local r, g, b + _, r, g, b, _ = col:get_rgba() return api.lgi.cairo.SolidPattern.create_rgba(r, g, b, alpha) end @@ -56,9 +57,9 @@ local function start(c) local screen_y = screen.geometry.y local layout = api.layout.get(screen) - if c.floating or layout.get_regions == nil then return end + if c.floating or layout.machi_get_regions == nil then return end - local regions = layout.get_regions(c.screen.workarea) + local regions = layout.machi_get_regions(c.screen.workarea) local infobox = api.wibox({ screen = screen,