syscalls: Add 'terminal-rows'.

* guix/build/syscalls.scm (terminal-dimension): New procedure.
(terminal-columns): Rewrite in terms of 'terminal-dimension'.
(terminal-rows): New procedure.
* tests/syscalls.scm ("terminal-rows"): New test.
This commit is contained in:
Ludovic Courtès 2019-06-25 23:05:00 +02:00
parent 8874faaaac
commit 4593f5a654
No known key found for this signature in database
GPG Key ID: 090B11993D9AEBB5
2 changed files with 29 additions and 13 deletions

View File

@ -146,6 +146,7 @@
window-size-y-pixels window-size-y-pixels
terminal-window-size terminal-window-size
terminal-columns terminal-columns
terminal-rows
utmpx? utmpx?
utmpx-login-type utmpx-login-type
@ -1871,23 +1872,17 @@ corresponds to the TIOCGWINSZ ioctl."
(list (strerror err)) (list (strerror err))
(list err))))) (list err)))))
(define* (terminal-columns #:optional (port (current-output-port))) (define (terminal-dimension window-dimension port fall-back)
"Return the best approximation of the number of columns of the terminal at "Return the terminal dimension defined by WINDOW-DIMENSION, one of
PORT, trying to guess a reasonable value if all else fails. The result is 'window-size-columns' or 'window-size-rows' for PORT. If PORT does not
always a positive integer." correspond to a terminal, return the value returned by FALL-BACK."
(define (fall-back)
(match (and=> (getenv "COLUMNS") string->number)
(#f 80)
((? number? columns)
(if (> columns 0) columns 80))))
(catch 'system-error (catch 'system-error
(lambda () (lambda ()
(if (file-port? port) (if (file-port? port)
(match (window-size-columns (terminal-window-size port)) (match (window-dimension (terminal-window-size port))
;; Things like Emacs shell-mode return 0, which is unreasonable. ;; Things like Emacs shell-mode return 0, which is unreasonable.
(0 (fall-back)) (0 (fall-back))
((? number? columns) columns)) ((? number? n) n))
(fall-back))) (fall-back)))
(lambda args (lambda args
(let ((errno (system-error-errno args))) (let ((errno (system-error-errno args)))
@ -1900,6 +1895,24 @@ always a positive integer."
(fall-back) (fall-back)
(apply throw args)))))) (apply throw args))))))
(define* (terminal-columns #:optional (port (current-output-port)))
"Return the best approximation of the number of columns of the terminal at
PORT, trying to guess a reasonable value if all else fails. The result is
always a positive integer."
(define (fall-back)
(match (and=> (getenv "COLUMNS") string->number)
(#f 80)
((? number? columns)
(if (> columns 0) columns 80))))
(terminal-dimension window-size-columns port fall-back))
(define* (terminal-rows #:optional (port (current-output-port)))
"Return the best approximation of the number of rows of the terminal at
PORT, trying to guess a reasonable value if all else fails. The result is
always a positive integer."
(terminal-dimension window-size-rows port (const 25)))
;;; ;;;
;;; utmpx. ;;; utmpx.

View File

@ -1,5 +1,5 @@
;;; GNU Guix --- Functional package management for GNU ;;; GNU Guix --- Functional package management for GNU
;;; Copyright © 2014, 2015, 2016, 2017, 2018 Ludovic Courtès <ludo@gnu.org> ;;; Copyright © 2014, 2015, 2016, 2017, 2018, 2019 Ludovic Courtès <ludo@gnu.org>
;;; Copyright © 2015 David Thompson <davet@gnu.org> ;;; Copyright © 2015 David Thompson <davet@gnu.org>
;;; ;;;
;;; This file is part of GNU Guix. ;;; This file is part of GNU Guix.
@ -538,6 +538,9 @@
(> (terminal-columns (open-input-string "Join us now, share the software!")) (> (terminal-columns (open-input-string "Join us now, share the software!"))
0)) 0))
(test-assert "terminal-rows"
(> (terminal-rows) 0))
(test-assert "utmpx-entries" (test-assert "utmpx-entries"
(match (utmpx-entries) (match (utmpx-entries)
(((? utmpx? entries) ...) (((? utmpx? entries) ...)