Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #442 from pvande/tickets/2.7.x/12339-module-search…
…-formatting Tickets/2.7.x/12339 module search formatting
- Loading branch information
Showing
4 changed files
with
229 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Puppet::Util::Terminal | ||
# Attempts to determine the width of the terminal. This is currently only | ||
# supported on POSIX systems, and relies on the claims of `stty` (or `tput`). | ||
# | ||
# Inspired by code from Thor; thanks wycats! | ||
# @return [Number] The column width of the terminal. Defaults to 80 columns. | ||
def self.width | ||
if Puppet.features.posix? | ||
result = %x{stty size 2>/dev/null}.split[1] || | ||
%x{tput cols 2>/dev/null}.split[0] || | ||
'80' | ||
end | ||
return result.to_i | ||
rescue | ||
return 80 | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/env rspec | ||
require 'spec_helper' | ||
require 'puppet/util/terminal' | ||
|
||
describe Puppet::Util::Terminal do | ||
describe '.width' do | ||
it 'should invoke `stty` and return the width' do | ||
height, width = 100, 200 | ||
subject.expects(:`).with('stty size 2>/dev/null').returns("#{height} #{width}\n") | ||
subject.width.should == width | ||
end | ||
|
||
it 'should use `tput` if `stty` is unavailable' do | ||
width = 200 | ||
subject.expects(:`).with('stty size 2>/dev/null').returns("\n") | ||
subject.expects(:`).with('tput cols 2>/dev/null').returns("#{width}\n") | ||
subject.width.should == width | ||
end | ||
|
||
it 'should default to 80 columns if `tput` and `stty` are unavailable' do | ||
width = 80 | ||
subject.expects(:`).with('stty size 2>/dev/null').returns("\n") | ||
subject.expects(:`).with('tput cols 2>/dev/null').returns("\n") | ||
subject.width.should == width | ||
end | ||
|
||
it 'should default to 80 columns if `tput` or `stty` raise exceptions' do | ||
width = 80 | ||
subject.expects(:`).with('stty size 2>/dev/null').raises() | ||
subject.stubs(:`).with('tput cols 2>/dev/null').returns("#{width + 1000}\n") | ||
subject.width.should == width | ||
end | ||
end | ||
end |