public
Last active

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.

  • Download Gist
Readme.md
Markdown

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.

features/read-layout.feature
Cucumber
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
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")
features/support/env.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.