From 98ab47ec56a83210382beb450d3eb5b43ab6f30f Mon Sep 17 00:00:00 2001 From: James Reed Date: Mon, 16 Sep 2019 08:01:17 -0600 Subject: [PATCH] Add workspaces Workspaces are wrappers around tags that launch accompanying clients. --- README.md | 21 ++++++ config.ld | 1 + rockspec/awesome-launch-devel-1.rockspec | 1 + workspace.lua | 85 ++++++++++++++++++++++++ 4 files changed, 108 insertions(+) create mode 100644 workspace.lua diff --git a/README.md b/README.md index 36a6006..b1e4449 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,27 @@ The new client will have these properties set: See the [API documentation](https://jcrd.github.io/awesome-launch/) for descriptions of all functions. +## Workspaces + +Require the library: +```lua +local ws = require("awesome-launch.workspace") +``` + +Add a new workspace: +```lua +ws.add("code", { + pwd = "/home/user/code", + clients = { + "xterm -e vim", + {"qutebrowser", {factory="qutebrowser"}}, + } +}) +``` + +A new tag named `code` will be created with the working directory and clients +listed above. + ## Widget A `launchbar` widget is provided to visualize pending clients. diff --git a/config.ld b/config.ld index e85a203..de5928c 100644 --- a/config.ld +++ b/config.ld @@ -7,6 +7,7 @@ file={ 'init.lua', 'panel.lua', 'widget.lua', + 'workspace.lua', } backtick_references=true diff --git a/rockspec/awesome-launch-devel-1.rockspec b/rockspec/awesome-launch-devel-1.rockspec index 157421a..702acbd 100644 --- a/rockspec/awesome-launch-devel-1.rockspec +++ b/rockspec/awesome-launch-devel-1.rockspec @@ -20,6 +20,7 @@ build = { ["awesome-launch.panel"] = "panel.lua", ["awesome-launch.shared"] = "shared.lua", ["awesome-launch.widget"] = "widget.lua", + ["awesome-launch.workspace"] = "workspace.lua", }, install = { bin = { diff --git a/workspace.lua b/workspace.lua new file mode 100644 index 0000000..53cc85a --- /dev/null +++ b/workspace.lua @@ -0,0 +1,85 @@ +--- Create new tags and launch accompanying clients. +-- +-- @author James Reed <jcrd@tuta.io> +-- @copyright 2019 James Reed +-- @module awesome-launch.workspace + +local awful = require("awful") +local gtable = require("gears.table") +local launch = require("awesome-launch") + +local ws = {} +ws.client = {} + +--- Spawn a command and add the client to a tag. +-- +-- @param cmd The command. +-- @param args Table containing the single instance ID and additional arguments for spawn +-- @param args.id Single instance ID. +-- @param args.props Properties to apply to the client. +-- @param args.pwd Pathname to the working directory for new clients. +-- @param args.timeout Seconds after which to stop waiting for a client to spawn. +-- @param args.spawn_callback Function to call with client when it spawns. +-- @param args.factory The factory to use (see wm-launch's -f flag). +-- @param args.firejail If true, run cmd with firejail. +-- @param tag The tag. +-- @function client.add +function ws.client.add(cmd, args, tag) + args = args and gtable.clone(args) or {} + tag = tag or awful.screen.focused().selected_tag + args.props = args.props or {} + args.props.tag = tag + args.props.tags = nil + if tag.pwd then + args.pwd = tag.pwd + end + launch.spawn(cmd, args) +end + +--- Create a new workspace and underlying tag. +-- +-- @param name The tag name. +-- @param args Table containing tag properties and additional workspace options +-- @param args.props Properties to apply to the tag. +-- @param args.pwd Pathname to the working directory for new clients. +-- @param args.clients Table containing client commands to spawn. +-- +-- Example: `args.clients = { "xterm", +-- {"qutebrowser", {factory="qutebrowser"}} }` +-- +-- @param args.callback Function to call with newly created tag. +-- @return The new tag. +-- @function add +function ws.add(name, args) + args = args or {} + local props = { + screen = awful.screen.focused(), + volatile = true, + } + gtable.crush(props, args.props or {}) + local tag = awful.tag.add(name, props) + + if args.pwd then + tag.pwd = args.pwd + end + + if args.clients then + for _, c in ipairs(args.clients) do + local cmd = c + local cmdargs + if type(c) == "table" then + cmd = c[1] + cmdargs = gtable.clone(c[2], false) + end + ws.client.add(cmd, cmdargs, tag) + end + end + + if args.callback then + args.callback(tag) + end + + return tag +end + +return ws