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.
Last active
December 10, 2015 21:59
-
-
Save JoshCheek/4498756 to your computer and use it in GitHub Desktop.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
My friend's final version (in Elisp) https://github.com/pd/swallow-window.el/blob/master/swallow-window.el