Skip to content

Commit

Permalink
Change size from ioctl to extract window buffer format and size to co…
Browse files Browse the repository at this point in the history
…nstants
  • Loading branch information
piotrmurach committed Dec 3, 2023
1 parent 2ec6af3 commit 1b44eb3
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Changed
* Change the ioctl system call support check to use the output method
* Change size from ioctl to extract window buffer format and size to constants

### Fixed
* Fix ioctl call test to stub terminal size encoding for big-endian systems
Expand Down
21 changes: 18 additions & 3 deletions lib/tty/screen.rb
Original file line number Diff line number Diff line change
Expand Up @@ -188,20 +188,35 @@ def size_from_io_console(verbose: false)
TIOCGWINSZ_PPC = 0x40087468 # macos, freedbsd, netbsd, openbsd
TIOCGWINSZ_SOL = 0x5468 # solaris

# The ioctl window buffer format
#
# @return [String]
#
# @api private
TIOCGWINSZ_FORMAT = "SSSS"
private_constant :TIOCGWINSZ_FORMAT

# The ioctl window buffer format size
#
# @return [Integer]
#
# @api private
TIOCGWINSZ_FORMAT_SIZE = TIOCGWINSZ_FORMAT.size
private_constant :TIOCGWINSZ_FORMAT_SIZE

# Read terminal size from Unix ioctl
#
# @return [nil, Array[Integer, Integer]]
#
# @api private
def size_from_ioctl
format = "SSSS"
buffer = ([0] * format.size).pack(format)
buffer = ([0] * TIOCGWINSZ_FORMAT_SIZE).pack(TIOCGWINSZ_FORMAT)

if ioctl?(TIOCGWINSZ, buffer) ||
ioctl?(TIOCGWINSZ_PPC, buffer) ||
ioctl?(TIOCGWINSZ_SOL, buffer)

rows, cols, = buffer.unpack(format)[0..1]
rows, cols, = buffer.unpack(TIOCGWINSZ_FORMAT)[0..1]
return [rows, cols] if nonzero_column?(cols)
end
end
Expand Down

0 comments on commit 1b44eb3

Please sign in to comment.