Created
March 28, 2019 21:47
-
-
Save SamSaffron/9e2947d69a057c1b9bff1a8c8be37e20 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/env ruby | |
require 'i3ipc' | |
class I3Helper | |
def initialize | |
@i3 = I3Ipc::Connection.new | |
end | |
def tree | |
@tree ||= @i3.tree | |
end | |
def recurse(nodes = nil, focused: false, floating: false, depth: 0, &blk) | |
nodes ||= tree.nodes | |
nodes.each do |n| | |
blk.call n, focused: n.focused || focused, depth: depth, floating: floating | |
depth += 1 | |
recurse(n.nodes, depth: depth, floating: floating, &blk) | |
recurse(n.floating_nodes, depth: depth, floating: true, &blk) | |
end | |
end | |
def is_floating? | |
recurse(tree.nodes) do |n, focused:, depth:, floating:| | |
return true if floating && focused | |
end | |
end | |
def layout_exec(command, cols: 2, rows: 3, balance: :auto) | |
windows = [] | |
focused_window = nil | |
focused_workspace = nil | |
workspace = nil | |
recurse do |n, depth: , focused: , floating:| | |
# dim = n.type == "con" ? " #{n.rect.width}x#{n.rect.height}" : "" | |
# puts "#{" " * depth} #{n.name} #{n.type}#{dim} #{n.layout} #{focused ? "*" : ""} #{floating ? "f" : ""}" | |
workspace = n.name if n.type == "workspace" | |
if n.respond_to?(:window_properties) && !floating | |
windows << {window: n, workspace: workspace} | |
if focused | |
focused_window = n if focused | |
focused_workspace = workspace | |
end | |
end | |
end | |
if !focused_window | |
@i3.command("exec #{command}") | |
return | |
end | |
windows = windows.select do |window:, workspace:| | |
workspace == focused_workspace && window.output == focused_window.output && window.window_properties.instance != "i3bar" | |
end.map do |window:, workspace:| | |
window | |
end | |
if balance == :auto && windows.length > 0 | |
outputs = @i3.outputs.sort{|a,b| a.rect.x <=> b.rect.x} | |
left_aligns = [] | |
take = false | |
outputs.each do |output| | |
left_aligns << output.name if take | |
take = true if output.primary | |
end | |
if left_aligns.include?(windows[0].output) | |
balance = :left | |
end | |
end | |
if windows.length < cols | |
@i3.command("split h; exec #{command}") | |
else | |
full_cols = (windows.length - cols) / (rows - 1) | |
full_rows = (windows.length - cols) % (rows - 1) | |
index = cols - full_cols - 1 | |
if balance == :left | |
index = full_cols * rows | |
end | |
window = windows[index] | |
split = full_rows == 0 ? " split v;" : "" | |
@i3.command("[con_id=\"#{window.id}\"] focus; #{split} exec #{command}") | |
end | |
#p @i3.outputs | |
end | |
def move(dir) | |
@i3.command("mark _last") | |
if is_floating? | |
@i3.command("move to output #{dir}") | |
else | |
@i3.command("move #{dir}") | |
end | |
@i3.command('[con_mark="_last"] focus') | |
end | |
def close | |
@i3.close | |
@i3 = nil | |
end | |
end | |
helper = I3Helper.new | |
def usage | |
puts "i3-plus [COMMAND] [PARAMS]" | |
puts "" | |
puts "Commands:" | |
puts " move_left: will move the current window left, if it is floating it will move the window to next monitor " | |
puts " move_right: will move the current window right, if it is floating it will move the window to next monitor " | |
puts " layout_exec: similar to exec except that it tries to maintain a certain layout" | |
end | |
case ARGV[0] | |
when "layout_exec" then | |
helper.layout_exec "i3-sensible-terminal" | |
when "move_left" then | |
helper.move("left") | |
when "move_right" then | |
helper.move("right") | |
else | |
puts "unknown command" | |
usage | |
exit 1 | |
end | |
helper.close |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment