obsolete.computer

awesomeconf/ruari/util/init.lua

File Type: text/plain

--[[

    ruari.util

    Utilities for Awesome WM

]]--

local awful = require("awful")
local lain  = require("lain")
local naughty  = require("naughty")
local r_layout = require("ruari.layout")

local util = { _NAME = "ruari.util" }

function util.max_master_count(t)
    t = t or awful.screen.focused().selected_tag
    s = t.screen

    if s.geometry.width >= s.geometry.height then
        return math.floor(s.geometry.height / 540)
    else
        return math.floor(s.geometry.width / 540)
    end

end

function util.master_count(t)
    local prefer_master_count = 0
    local cls = t:clients()
    for i = 1, #cls do
        if cls[i].prefer_master == true and cls[i].floating == false then
            prefer_master_count = prefer_master_count + 1
        end
    end

    return math.max(math.min(prefer_master_count, util.max_master_count(t)), 1)
end

function util.update_master_count(t)
    t.master_count = util.master_count(t)
end

function util.adjust_tags(s)
    s = s or awful.screen.focused()
    local last_tag_with_clients = 0
    for i = 1, #s.tags do
        if #s.tags[i]:clients() > 0 then
            last_tag_with_clients = i
        end
    end

    --Add tag if all tags have clients
    if last_tag_with_clients == #s.tags then
        util.add_tag(s)
    end

    --Delete tags if there are more than one empty at the end
    if last_tag_with_clients < #s.tags - 1 then
        for i = #s.tags, last_tag_with_clients + 2, -1 do
            util.delete_tag(s.tags[i])
        end
    end
end

function util.add_tag(s)
    s = s or awful.screen.focused()
    awful.tag.add(tostring(#s.tags + 1), { screen = s, layout = util.geometry_appropriate_layouts(s)[1] })
end

function util.delete_tag(t)
    t = t or awful.screen.focused().selected_tag
    if not t then return end
    t:delete()
end

function util.set_tag_names(s)
    s = s or awful.screen.focused()
    for i = 1, #s.tags do
        local cls = s.tags[i]:clients()
        if #cls > 0 then
            s.tags[i].name = tostring(i) .. ":[" .. string.lower(cls[1].class) .. "]"
        else
            s.tags[i].name = tostring(i)
        end
    end
end

function util.set_tag_layouts(s)
    s = s or awful.screen.focused()
    for i = 1, #s.tags do
            s.tags[i].layouts = util.geometry_appropriate_layouts(s)
            s.tags[i].layout = util.geometry_appropriate_layouts(s)[1]
    end
end

function util.geometry_appropriate_layouts(s)
    s = s or awful.screen.focused()
    if s.geometry.width >= s.geometry.height then
        --naughty.notify({ title = "Width: " .. s.geometry.width })
        if s.geometry.width > 1920 then
            --Large, Wide (or square) Layouts
            layouts = {
                r_layout.multicenterwork,
                awful.layout.suit.tile,
                awful.layout.suit.tile.left,
                lain.layout.termfair,
                lain.layout.termfair.center,
                awful.layout.suit.floating,
            }
        else
            --Small (1920 wide or less), Wide layouts
            layouts = {
                awful.layout.suit.tile,
                awful.layout.suit.tile.left,
                lain.layout.centerwork,
                lain.layout.termfair,
                lain.layout.termfair.center,
                awful.layout.suit.floating,
            }
        end 
    else
        if s.geometry.height > 1920 then
            --Large, Tall layouts
            layouts = {
                layout.multicenterwork.horizontal,
                awful.layout.suit.tile.bottom,
                awful.layout.suit.tile.top,
                lain.layout.centerwork.horizontal,
                awful.layout.suit.floating,
            }
        else
            --Small (1920 tall or less), Tall layouts
            layouts = {
                awful.layout.suit.tile.bottom,
                awful.layout.suit.tile.top,
                lain.layout.centerwork.horizontal,
                awful.layout.suit.floating,
            }
        end 
    end

    return layouts
end

function file_exists(file)
  local f = io.open(file, "rb")
  if f then f:close() end
  return f ~= nil
end

function util.get_system_font()
    local handle = io.popen("xfconf-query -c xsettings -p \"/Gtk/FontName\" || echo \"Sans 10\"")
    local result = handle:read("*a")
    handle:close()
    return result
end

function util.get_mono_font()
    local handle = io.popen("xfconf-query -c xsettings -p \"/Gtk/MonospaceFontName\" || echo \"Monospace 10\"")
    local result = handle:read("*a")
    handle:close()
    return result
end

function util.get_wallpaper()
    local file = os.getenv( "HOME" ) .. "/.config/nitrogen/bg-saved.cfg"
    if not file_exists(file) then return "" end
    local handle = io.popen("cat \"" .. file .. "\" | grep file= | head -1 | cut -d'=' -f 2")
    local result = handle:read("*a")
    handle:close()
    return result
end

function util.set_wallpaper()
    os.execute( "( sleep 0.5 && " .. awful.util.getdir("config") .. "/bin/wallpaper ) &" )
end

function util.random_wallpaper()
    os.execute( awful.util.getdir("config") .. "/bin/wallpaper --random" )
end

function util.get_wal_colors()
    local file = os.getenv( "HOME" ) .. "/.cache/wal/colors"
    if not file_exists(file) then return {} end
    local lines = {}
    for line in io.lines(file) do 
        lines[#lines + 1] = line
    end
    return lines
end



return util

Meta