Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

stty -> io/console, STDIN -> $stdin

Shelling out is totally fine, but io/console is a
standard library and is portable, let's use it.

On the other hand let's switch to the global vars
for standard streams. That's a best practice, and
they may be handy for mocks in tests.
  • Loading branch information...
commit 89830c3922c884801e20c52939c3788819d80e64 1 parent eed393e
@fxn authored
Showing with 11 additions and 12 deletions.
  1. +11 −12 bin/tkn
View
23 bin/tkn
@@ -5,6 +5,7 @@
Encoding.default_external = 'utf-8'
Encoding.default_internal = 'utf-8'
+require 'io/console'
require 'active_support/core_ext/string/strip'
require 'pygments'
@@ -61,10 +62,9 @@ def ansi_length(str)
end
# Returns the number of rows and columns of the terminal as an array of two
-# integers [rows, cols]. We could use io/console here but shelling out is also
-# fine.
+# integers [rows, cols].
def winsize
- `stty size`.split.map(&:to_i)
+ $stdout.winsize
end
@@ -135,15 +135,14 @@ end
# configure Terminal.app so that PageDown and PageUp get passed down the
# script. Echoing is turned off while doing this.
def read_command
- begin
- system 'stty raw -echo'
- command = STDIN.getc
- # Consume PageUp or PageDown if present. No other ANSI escape sequence is
- # supported so a blind 3.times getc is enough.
- 3.times { command << STDIN.getc } if command == "\e"
- command
- ensure
- system "stty -raw echo"
+ $stdin.noecho do |noecho|
+ noecho.raw do |raw|
+ raw.getc.tap do |command|
+ # Consume PageUp or PageDown if present. No other ANSI escape sequence is
+ # supported so a blind 3.times getc is enough.
+ 3.times { command << raw.getc } if command == "\e"
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.