From 50e80029b3a9ac8f92198c97111f0b7085bf9f80 Mon Sep 17 00:00:00 2001 From: Ben Grande Date: Thu, 7 Mar 2024 17:38:45 +0100 Subject: [PATCH] feat: add tool to resize terminal size Inspired by the Archlinux Wiki. The Arch Wiki does not provide a copyrights page, it is therefore hard to credit the contributors Recent commit c6de5e8046d4965fde910ee3b0c9c0709899609c is 7 years old, with reference to $wgRightsPage. Individual contributors can't be referenced but there is credit to the specific page in the Arch wiki. https://gitlab.archlinux.org/archlinux/infrastructure/-/blob/master/roles/archwiki/templates/LocalSettings.php.j2 --- files/sh/.config/bash/bashrc | 4 +- files/sh/.config/zsh/.zshrc | 4 +- files/sh/.local/bin/resize-terminal | 83 +++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 4 deletions(-) create mode 100755 files/sh/.local/bin/resize-terminal diff --git a/files/sh/.config/bash/bashrc b/files/sh/.config/bash/bashrc index a076183..974f3c1 100644 --- a/files/sh/.config/bash/bashrc +++ b/files/sh/.config/bash/bashrc @@ -77,9 +77,9 @@ newline=$'\n' if test "${color_prompt:-}" = "yes"; then # shellcheck disable=SC2154 - PS1="\$(_reset_line)\[\033[35m\][\[${reset_color}\]${debian_chroot:+($debian_chroot)}\[${usercolor}\]\u@\h \[${dircolor}\]\w\[${reset_color}\]\$(_git_prompt_info)\[\033[35m\]]\[${reset_color}\]${newline-}\$(_print_ec)${ps1_symbol} " + PS1="\$(_reset_line)\$(resize-terminal)\[\033[35m\][\[${reset_color}\]${debian_chroot:+($debian_chroot)}\[${usercolor}\]\u@\h \[${dircolor}\]\w\[${reset_color}\]\$(_git_prompt_info)\[\033[35m\]]\[${reset_color}\]${newline-}\$(_print_ec)${ps1_symbol} " else - PS1="\$(_reset_line)[${debian_chroot:+($debian_chroot)}\u@\h:\w\$(_git_prompt_info)]${newline-}\$(_print_ec)${ps1_symbol} " + PS1="\$(_reset_line)\$(resize-terminal)[${debian_chroot:+($debian_chroot)}\u@\h:\w\$(_git_prompt_info)]${newline-}\$(_print_ec)${ps1_symbol} " fi case "${TERM-}" in diff --git a/files/sh/.config/zsh/.zshrc b/files/sh/.config/zsh/.zshrc index 0b3cd38..051b192 100644 --- a/files/sh/.config/zsh/.zshrc +++ b/files/sh/.config/zsh/.zshrc @@ -71,10 +71,10 @@ if test "${color_prompt-}" = "yes"; then autoload -U colors && colors [[ "${COLORTERM-}" == (24bit|truecolor) || "${terminfo[colors]}" -eq '16777216' ]] || zmodload zsh/nearcolor - PS1="%F{magenta}[%{$usercolor%}%n@%M%F{reset_color%} %{$dircolor%}%50<...<%~%<<%F{reset_color%}\$(_git_prompt_info)%F{magenta}]%F{reset_color}${newline-}${ps1_symbol} " + PS1="\$(resize-terminal)%F{magenta}[%{$usercolor%}%n@%M%F{reset_color%} %{$dircolor%}%50<...<%~%<<%F{reset_color%}\$(_git_prompt_info)%F{magenta}]%F{reset_color}${newline-}${ps1_symbol} " RPS1="%(?..(%{"$'\e[31m'"%}%?%{$reset_color%}%)%<<)" else - PS1="[%n@%M %~\$(_git_prompt_info)]${newline}${ps1_symbol} " + PS1="\$(resize-terminal)[%n@%M %~\$(_git_prompt_info)]${newline}${ps1_symbol} " RPS1="%(?..(%?%)%<<)" fi diff --git a/files/sh/.local/bin/resize-terminal b/files/sh/.local/bin/resize-terminal new file mode 100755 index 0000000..07aceed --- /dev/null +++ b/files/sh/.local/bin/resize-terminal @@ -0,0 +1,83 @@ +#!/bin/sh + +## SPDX-FileCopyrightText: 2024 Benjamin Grande M. S. +## +## SPDX-License-Identifier: GFDL-1.3-or-later +## +## Credits: https://wiki.archlinux.org/title/Working_with_the_serial_console#Resizing_a_terminal +## +## Resize terminal columns and lines to current window size. +## Useful for terminals over link (serial console, socat's pty etc). + +test -t 0 || exit + +## If argument is provided, allow user to bypass tty check. +if test "${#}" -eq 0; then + ## Shells on graphical sessions (terminal emulators) are skipped. + test "${XDG_SESSION_TYPE:-}" = "tty" || exit + ## Serial ports and devices are desired. + term_file="$(tty)" + term_file_wanted="ttyUSB ttyS" + ## Consoles are desired. + if test -r /sys/class/tty/console/active; then + term_file_wanted="${term_file_wanted} $(cat /sys/class/tty/console/active)" + fi + term_file_active=0 + for tf in $(printf %s"${term_file_wanted}"); do + case "${term_file}" in + *"/${tf}"*) term_file_active=1;; + *) ;; + esac + done + unset tf term_file term_file_wanted + ## Terminal can handle screen resizing by itself. + if test "${term_file_active}" = "0"; then + unset term_file_active + exit + fi + unset term_file_active +fi + +## POSIX compliant. +# shellcheck disable=SC3045 +if ! echo R | read -r -t 1 -sd R 2>/dev/null; then + ## Fast but depends on XTerm. + if has resize; then + eval "$(resize)" >/dev/null + exit + fi + ## Slow due to heavy stty calls. + termios="$(stty -g)" + stty raw -echo min 0 time 1 + printf '\0337\033[r\033[99999;99999H\033[6n\0338' >/dev/tty + IFS='[;R' read -r _ rows cols _ /dev/tty +# shellcheck disable=3045,SC2034 +IFS='[;R' read -r -t 1 -s -d R _ rows cols _ &2 + stty echo + unset rows cols + exit 1 +} + +if test ${COLUMNS} -eq "${cols}" && test ${LINES} -eq "${rows}";then + stty echo + unset rows cols + exit +elif test "${rows}" -gt 0 && test "${cols}" -gt 0;then + stty echo cols "${cols}" rows "${rows}" + unset rows cols + exit +fi + +echo "error: cannot resize screen: unsupported terminal emulator" >&2 +stty echo +unset rows cols +exit 1