Skip to content

termsize: without python #10

@jraby

Description

@jraby

(this is not really an issue)

First, thanks for the blogpost about termsize, it got me started on some yak shaving I didn't intend to do today... :-)

While reading your post I figured there should be a way to do it without python at all.

Here's a way to do it, I thought you might be interested.

#!/bin/bash
set -eu

# Reset term to initial state on exit
stty_ori=$(stty -g)
function atexit {
    stty $stty_ori
}
trap atexit EXIT

# Turn echo off to avoid writing term size on the screen
stty -echo

# Use bash's "read" to send the control seq to the term and read its answer
# 7: save cursor pos
# [r: scroll screen
# [x;yH: set cursor pos
# [6n: get cursor pos
read -p $'\e7\e[r\e[9999;9999H\e[6n' -d R termsize

# Strip leading control char + [ (e.g. \e[x;yH)
termsize=${termsize#??}

# Reset cursor pos
echo -en '\e8'

# Actually resize the terminal
stty $(echo $termsize  | sed 's/^/rows /; s/;/ cols /')

Also, busybox has resize(1). (which I discovered after writing the above...)
https://git.busybox.net/busybox/tree/console-tools/resize.c#n85

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions