Created
February 22, 2022 19:14
-
-
Save aalhitennf/aff768ac1b060261959c8c513ddec6bc to your computer and use it in GitHub Desktop.
awesomewm preferences
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local awful = require('awful') | |
local beautiful = require('beautiful') | |
local gears = require('gears') | |
local json = require('util.json') | |
local confdir = gears.filesystem.get_configuration_dir() | |
local icons = require('theme.icons').taglist | |
-- File | |
local prefdir = confdir .. 'config/preferences/' | |
_G.preferences = { | |
screens = {} | |
} | |
local read_successfull = false | |
local function deep_compare_tables(t1, t2, ignore_mt) | |
local ty1 = type(t1) | |
local ty2 = type(t2) | |
if ty1 ~= ty2 then | |
return false | |
end | |
if ty1 ~= 'table' and ty2 ~= 'table' then | |
return t1 == t2 | |
end | |
local mt = getmetatable(t1) | |
if not ignore_mt and mt and mt.__eq then | |
return t1 == t2 | |
end | |
for k1, v1 in pairs(t1) do | |
local v2 = t2[k1] | |
if v2 == nil or not deep_compare_tables(v1, v2) then | |
return false | |
end | |
end | |
for k2, v2 in pairs(t2) do | |
local v1 = t1[k2] | |
if v1 == nil or not deep_compare_tables(v1, v2) then | |
return false | |
end | |
end | |
return true | |
end | |
local function read_config_file(file) | |
read_successfull = false | |
local filename = prefdir .. file .. '.json' | |
local f = io.open(filename, 'r') | |
if f then | |
io.input(f) | |
local data = f:read('*a') | |
if data then | |
local decoded = json.decode(data) | |
if decoded then | |
_G.preferences = decoded | |
read_successfull = true | |
end | |
end | |
io.close(f) | |
end | |
end | |
local function save_config_file(file) | |
local filename = prefdir .. file .. '.json' | |
local f = io.open(filename, 'w') | |
if f then | |
io.output(f) | |
local data = json.encode(_G.preferences) | |
if data then | |
io.write(data) | |
end | |
io.close(f) | |
end | |
end | |
local function format_tag_name(name) | |
if _G.tools.string_starts(name, 'tile') and string.len(name) > 4 then | |
return name:gsub('tile', 'tile.') | |
end | |
return name | |
end | |
local function screen_data(s) | |
local data = { | |
index = s.index or s.outputs.name or 'error', | |
tags = {}, | |
has_fake = s.has_fake or false | |
} | |
for i, t in pairs(s.tags) do | |
data.tags[i] = { | |
layout = format_tag_name(t.layout.name) or 'error', | |
gap = t.gap or 0, | |
name = t.name, | |
icon_name = t.icon_name, | |
selected = t.selected | |
} | |
end | |
return data | |
end | |
local function generate_from_current() | |
local data = { | |
screens = {} | |
} | |
for s in _G.screen do | |
data.screens[tostring(s.index)] = screen_data(s) | |
end | |
return data | |
end | |
local function generate_and_save() | |
local current_config = generate_from_current() | |
if not deep_compare_tables(_G.preferences, current_config) then | |
_G.preferences = current_config | |
save_config_file('current') | |
end | |
end | |
-- Read config at startup | |
read_config_file('current') | |
if not read_successfull then | |
read_config_file('backup') | |
end | |
local function validate_layout(name) | |
if not name then | |
return nil | |
end | |
if string.find(name, 'tile.') then | |
local dir = name:gsub('tile.', '') | |
return awful.layout.suit.tile[dir] | |
end | |
return awful.layout.suit[name] | |
end | |
-- Create tags for each screen | |
_G.screen.connect_signal('request::desktop_decoration', | |
function(s) | |
local si = tostring(s.index) | |
if _G.preferences.screens[si] ~= nil and _G.preferences.screens[si].tags ~= nil then | |
for i = 1, _G.tools.table_size(_G.preferences.screens[si].tags), 1 do | |
local tag = _G.preferences.screens[si].tags[i] | |
awful.tag.add(i, { | |
name = tag.name, | |
icon = icons[tag.icon_name] or icons['web'], | |
icon_name = tag.icon_name or 'web', | |
icon_only = false, | |
layout = validate_layout(tag.layout) or awful.layout.suit.tile, | |
gap_single_client = true, | |
gap = tag.gap or beautiful.useless_gap, | |
screen = s, | |
selected = tag.selected | |
}) | |
end | |
else | |
awful.tag.add(1, { | |
name = 'Tag 1', | |
icon = icons.web, | |
icon_only = false, | |
layout = awful.layout.suit.tile, | |
gap_single_client = true, | |
gap = 0, | |
screen = s, | |
selected = true | |
}) | |
end | |
end | |
) | |
_G.awesome.connect_signal('preferences::save', function () | |
generate_and_save() | |
end) | |
-- Signals | |
_G.screen.connect_signal( | |
'fake_created', | |
function() | |
generate_and_save() | |
end | |
) | |
_G.screen.connect_signal( | |
'tag::history::update', | |
function() | |
if _G.initializing then return end | |
generate_and_save() | |
end | |
) | |
_G.tag.connect_signal( | |
'property::layout', | |
function() | |
if _G.initializing then return end | |
generate_and_save() | |
end | |
) | |
_G.tag.connect_signal( | |
'property::name', | |
function() | |
if _G.initializing then return end | |
generate_and_save() | |
end | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment