Fix handling of cycling windows

Style code more according to Crystal convention
master v0.2
knotteye 2020-03-14 23:48:32 -05:00
parent ee31ea8b75
commit da4f6fd47b
1 changed files with 21 additions and 38 deletions

View File

@ -2,7 +2,6 @@ require "json"
require "option_parser" require "option_parser"
version = "0.1" version = "0.1"
puts ""
class Window class Window
JSON.mapping({ JSON.mapping({
@ -24,36 +23,23 @@ end
def node_loop (root : Window) def node_loop (root : Window)
list = [] of Window list = [] of Window
root.nodes.each do |node| root.nodes.each do |node|
if(node.wtype == "con") if node.wtype == "con"
list << node list << node
elsif(node.nodes != [] of Window) elsif !node.nodes.empty?
list.concat(node_loop(node)) list.concat node_loop node
end end
end end
return list list
end end
client_tree = Window.from_json(`swaymsg -t get_tree`) client_tree = Window.from_json(`swaymsg -t get_tree`)
if(client_tree.wtype != "root") if client_tree.wtype != "root"
puts "Malformed client tree." puts "Malformed client tree."
exit(1) exit(1)
end end
window_list = node_loop(client_tree) window_list = node_loop client_tree
def parse_marks(marks : Array(String), mark : String)
res : Bool = false
marks.each do |cm|
if(cm.includes? mark)
res = true
end
end
return !res
end
def check_type
end
cycle = false cycle = false
OptionParser.parse do |parser| OptionParser.parse do |parser|
@ -81,45 +67,42 @@ OptionParser.parse do |parser|
end end
parser.on "-n WNAME", "--name=WNAME", "Match against window name" do |wname| parser.on "-n WNAME", "--name=WNAME", "Match against window name" do |wname|
window_list.reject! {|w| !w.name.includes? wname} window_list.select! {|w| w.name.includes? wname}
end end
parser.on "-m WMARK", "--mark=WMARK", "Match against window mark" do |wmark| parser.on "-m WMARK", "--mark=WMARK", "Match against window mark" do |wmark|
window_list.reject! {|w| parse_marks(w.marks, wmark)} window_list.reject! {|w| w.marks.none?{|m| m.includes? wmark}}
end end
parser.on "-t WTYPE", "--type=WTYPE", "Match against window type (app_id for wayland, class for xwayland)" do |wtype| parser.on "-t WTYPE", "--type=WTYPE", "Match against window type (app_id for wayland, class for xwayland)" do |wtype|
window_list.reject! {|w| !(w.app_id.includes?(wtype) || w.wclass.includes?(wtype))} window_list.select! {|w| w.app_id.includes?(wtype) || w.wclass.includes?(wtype)}
end end
parser.invalid_option do |flag| parser.invalid_option do |flag|
STDERR.puts "#{flag} is not a valid option" STDERR.puts "#{flag} is not a valid option"
STDERR.puts ""
STDERR.puts parser STDERR.puts parser
exit(1) exit(1)
end end
parser.missing_option do |flag| parser.missing_option do |flag|
STDERR.puts "#{flag} requires an argument" STDERR.puts "#{flag} requires an argument"
STDERR.puts ""
STDERR.puts parser STDERR.puts parser
exit(1) exit(1)
end end
parser.unknown_args do
exit(0)
end
end end
if(window_list == [] of Window) if window_list.empty?
puts "No matching window." puts "No matching window."
exit(1) exit(1)
end end
if(cycle)
while(!window_list[window_list.size - 1].focused)
window_list.push window_list[0]
window_list.delete_at(0)
end
end
Process.exec("swaymsg", [%<[con_id=>+window_list[0].id.to_s+%<]>,"focus"]) if cycle
windex = window_list.index {|w| w.focused}
if windex.nil? || windex == window_list.size - 1
windex = -1
end
windex += 1
Process.exec("swaymsg", ["[con_id=#{window_list[windex].id.to_s}]","focus"])
else
Process.exec("swaymsg", ["[con_id=#{window_list[0].id.to_s}]","focus"])
end