Skip to content

Instantly share code, notes, and snippets.

@genki
Last active January 19, 2017 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save genki/5a84deca67d1c03610c8e7108160fa39 to your computer and use it in GitHub Desktop.
Save genki/5a84deca67d1c03610c8e7108160fa39 to your computer and use it in GitHub Desktop.
Parser for tmux's layout string (tmux display -p "#{window_visible_layout}")
class Parser
def initialize(input)
@input = input
@s = 0
end
def parse
wid = scan_wid
pick
w, h = scan_wh; pick
x = scan_d; pick
y = scan_d
c = case pick
when '['; scan_panes :vert, ']'
when '{'; scan_panes :horz, '}'
else []
end
[:win, wid, w, h, x, y, c]
end
private
def scan_panes(type, last)
panes = []
loop do
panes << scan_pane
case peek
when ','; pick
when last; pick; break
end
end
[type, panes]
end
def scan_pane
w, h = scan_wh; pick
x = scan_d; pick
y = scan_d
c = case pick
when '['; scan_panes :vert, ']'
when '{'; scan_panes :horz, '}'
else scan_d
end
[:pane, w, h, x, y, c]
end
def scan_wid
case peek
when /\h/; pick + scan_wid
when ','; ''
end
end
def scan_wh
w = scan_d
pick
h = scan_d
[w, h]
end
def scan_d
case peek
when /\d/; pick + scan_d
else ''
end
end
def pick
@input[@s]
ensure
@s += 1
end
def peek; @input[@s] end
end
require 'pp'
pp Parse.new.parse(<<EOH)
a158,80x64,0,0[81x16,0,0{40x16,0,0,108,40x16,41,0,127},81x15,0,17,126,81x32,0,33{31x32,0,33[31x16,0,33,122,31x15,0,50,128],49x32,32,33,123}]
EOH #=>
# [:win,
# "a158",
# "80",
# "64",
# "0",
# "0",
# [:vert,
# [[:pane,
# "81",
# "16",
# "0",
# "0",
# [:horz,
# [[:pane, "40", "16", "0", "0", "108"],
# [:pane, "40", "16", "41", "0", "127"]]]],
# [:pane, "81", "15", "0", "17", "126"],
# [:pane,
# "81",
# "32",
# "0",
# "33",
# [:horz,
# [[:pane,
# "31",
# "32",
# "0",
# "33",
# [:vert,
# [[:pane, "31", "16", "0", "33", "122"],
# [:pane, "31", "15", "0", "50", "128"]]]],
# [:pane, "49", "32", "32", "33", "123"]]]]]]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment