Skip to content

Instantly share code, notes, and snippets.

@felixrabe
Created March 13, 2012 16:17
Show Gist options
  • Save felixrabe/2029692 to your computer and use it in GitHub Desktop.
Save felixrabe/2029692 to your computer and use it in GitHub Desktop.
hiera.rb
selection = (ARGV[0] || 0).to_i
input = STDIN.readlines
if input[-1]
input[-1] += "\n" unless input[-1].end_with? "\n"
else
input << "\n"
end
whitespace_re = /^(\s*)/
i = 0
whitespace = selection == 0 ? "" : nil
output = ""
for line in input
i += 1
if i == selection
whitespace = line[whitespace_re]
elsif whitespace
w = line[whitespace_re]
break unless w.start_with? whitespace
break unless selection == 0 or w != whitespace
else
next
end
output << line[whitespace.length..-1]
end
print output
Feature: Only display input chunks selected by line number
Scenario: Empty input
Given the input
"""
"""
When "hiera" is run
Then the output should be
"""
"""
Scenario: Identical input
Given the input
"""
a
b
c
d
e
"""
When "hiera" is run
Then the output should be
"""
a
b
c
d
e
"""
Scenario: Select first input chunk
Given the input
"""
a
b
c
d
e
"""
When "hiera 1" is run
Then the output should be
"""
a
b
"""
Scenario: Select second input chunk
Given the input
"""
a
b
c
d
e
"""
When "hiera 3" is run
Then the output should be
"""
c
d
e
"""
Scenario: Select subchunk
Given the input
"""
a
b
c
d
e
"""
When "hiera 4" is run
Then the output should be
"""
d
e
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment