From 9b5b9b95627930ee9626313d286fd88d0967fb06 Mon Sep 17 00:00:00 2001 From: knotteye Date: Sat, 6 Nov 2021 22:14:02 -0500 Subject: [PATCH] Updated to crystal 1.0 json api Now excludes windows without a name in the list and handles null values for names --- src/swayfocus.cr | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/src/swayfocus.cr b/src/swayfocus.cr index 06c900e..35a19c7 100644 --- a/src/swayfocus.cr +++ b/src/swayfocus.cr @@ -3,17 +3,32 @@ require "option_parser" version = "0.1" +struct Nil + def includes? ( a : String) + false + end +end + class Window - JSON.mapping({ - id: UInt32, - name: String, - wtype: {type: String, key: "type"}, - app_id: {type: String, default: ""}, - wclass: {type: String, key: "class", default: ""}, - marks: {type: Array(String), default: [] of String}, - nodes: {type: Array(Window), default: [] of Window}, - focused: {type: Bool, default: false} - }) + include JSON::Serializable + + property id : UInt32 = 0 + + @[JSON::Field(emit_null: true)] + property name : String? + + @[JSON::Field(key: "type")] + property wtype : String = "" + + property app_id : String = "" + + @[JSON::Field(key: "class")] + property wclass : String = "" + + property marks : Array(String) = [] of String + property nodes : Array(Window) = [] of Window + + property focused : Bool = false def clear_nodes @nodes = [] of Window @@ -23,7 +38,7 @@ end def node_loop (root : Window) list = [] of Window root.nodes.each do |node| - if node.wtype == "con" + if node.wtype == "con" && !node.name.nil? list << node elsif !node.nodes.empty? list.concat node_loop node