Skip to content
Create a gist now

Instantly share code, notes, and snippets.

@JoshCheek /Readme.md
Last active Dec 10, 2015

Solving an algorithm for a friend to be able to tell his emacs what window he wants to replace his current window when he closes it.

Solving an algorithm for a friend to be able to tell his emacs what window he wants to replace his current window when he closes it.

def borderless(string)
string.gsub(/^[-+|]/ , '')
.gsub(/[-+|]$/ , '')
.sub(/\A[-+|]*\n/ , '')
.sub(/^[-+|]*\Z/ , '')
end
def leaf?(string)
string !~ /[-+|]/
end
def split_cols(raw_cols)
raw_cols[/\A.*$/].size
.times
.select { |index| raw_cols.each_line.all? { |line| '|+'.include? line[index] } }
.unshift(-1)
.push(0)
.each_cons(2)
.map { |left, right| raw_cols.each_line.map { |line| line[left+1..right-1].chomp } }
.map { |lines| lines.join "\n" }
end
def rows_for(raw_rows)
raw_rows.split(/^[-+]*\n/)
.map { |raw_cols| leaf?(raw_cols) ? raw_cols.strip : cols_for(raw_cols) }
.unshift(:rows)
end
def cols_for(raw_cols)
split_cols(raw_cols).map { |raw_rows| leaf?(raw_rows) ? raw_rows.strip : rows_for(raw_rows) }
.unshift(:cols)
end
def read_layout(string)
rows_for borderless string
end
When 'I read the window layout:' do |s|
@layout = read_layout(s.to_s)
end
Then 'the layout should be $struct' do |structure|
structure.gsub!('(', '[')
structure.gsub!(')', ']')
structure.gsub!('] ', '], ')
structure.gsub!(/(rows|cols) /, ':\1, ')
structure.gsub!('" ', '", ')
@layout.should == eval(structure)
end
Feature: Reading window layouts
Scenario: one window
When I read the window layout:
"""
+---+
| A |
+---+
"""
Then the layout should be (rows "A")
Scenario: A|B
When I read the window layout:
"""
+---+---+
| A | B |
+---+---+
"""
Then the layout should be (rows (cols "A" "B"))
Scenario: A|B--C
When I read the window layout:
"""
+---+---+
| A | B |
+---+---+
| C |
+-------+
"""
Then the layout should be (rows (cols "A" "B") "C")
Scenario: A|B--C|D
When I read the window layout:
"""
+---+---+
| A | B |
+---+---+
| C | D |
+-------+
"""
Then the layout should be (rows (cols "A" "B") (cols "C" "D"))
Scenario: 3 rows
When I read the window layout:
"""
+---+
| A |
+---+
| B |
+---+
| C |
+---+
"""
Then the layout should be (rows "A" "B" "C")
Scenario: Inner horizontal split
When I read the window layout:
"""
+---+---+
| A | |
+---+ B |
| F | |
+---+---+
| C |
+-------+
| D | E |
+---+---+
"""
Then the layout should be (rows (cols (rows "A" "F") "B") "C" (cols "D" "E"))
Scenario: Inner vertical split
When I read the window layout:
"""
+---+---+---+
| | B | C |
| A +---+---+
| | D |
+---+---+---+
| E |
+-----------+
"""
Then the layout should be (rows (cols "A" (rows (cols "B" "C") "D")) "E")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.