Discussion:
[iterm2-discuss] Capture in shell variable output of CSI GetWinSizePixels
Raymond Page
2018-06-28 18:23:03 UTC
Permalink
I can use some assistance with how to capture the CSI GetWinSizePixels
output in a shell variable?

The CSI GetWinSizePixels works (in iterm2 or nested tmux), but the output
doesn't seem to be to stdout, so I'm wondering if I need to read a tty/pts
to capture the output into a shell variable. Should I print these escapes
to the tty or pts directly, or to stdout of the shell?
iterm2
printf "\u1b[14t" > /dev/tty

tmux+iterm2
printf "\u1bPtmux;]\u1b\u1b[14t\u1b\\" > /dev/tty


I'm hoping I can do something like:
getpix() { read PIXEL_SIZE < /dev/tty & printf "\u1b[14t" > /dev/tty ; echo
"${PIXEL_SIZE}" ; }


Real world use, I'm trying to convert tput lines/cols to pixels for imgcat
rendering via gnuplot so the gnuplot png output will fill the terminal
window.
alias gnuplot="__gnuplot"
__gnuplot() {
SIZE=$(stty size 2>/dev/null)
SIZE=${SIZE:-$(tput lines) $(tput cols)}
COLS=${SIZE#* }
ROWS=${SIZE% *}
COLUMNS=${COLUMNS:-${COLS}}
LINES=${LINES:-${ROWS}}
case "${GNUTERM%% *}" in
dumb) X=${COLUMNS} ; Y=$((LINES-3));;
png|sixelgd) PIXEL_SIZE=`echo -ne "\e[14t" >/dev/tty` ;
PX=${PIXEL_SIZE#*;}; PX=${X%t}; PY=${PIXEL_SIZE%;*}
X=$((PX*COLUMNS/COLS))
Y=$((PX*LINES/ROWS))
;;
esac
sed -i "s/^set term[[:space:]][^[:space:]]*/set term ${GNUTERM%% *}/"
~/.gnuplot
case "${GNUTERM%% *}" in
png) GNUTERM="${GNUTERM} size $X,$Y" \gnuplot "$@" | imgcat;;
*) GNUTERM="${GNUTERM} size $X,$Y" \gnuplot "$@" ;;
esac
}


LINES=$((LINES/2)) gnuplot -e "plot sin(x)"
--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
George Nachman
2018-06-28 18:32:31 UTC
Permalink
If stdout isn't a tty you're going to have a bad time. There are
inescapable race conditions where two process try to write to the tty at
the same time and what comes out the other side is a mélange of the two.

That notwithstanding, if you're feeling lucky, here's a script you could
use to work from:
https://gist.github.com/gnachman/3f3b78030e02b6949de9fe7f813ae47c
Post by Raymond Page
I can use some assistance with how to capture the CSI GetWinSizePixels
output in a shell variable?
The CSI GetWinSizePixels works (in iterm2 or nested tmux), but the output
doesn't seem to be to stdout, so I'm wondering if I need to read a tty/pts
to capture the output into a shell variable. Should I print these escapes
to the tty or pts directly, or to stdout of the shell?
iterm2
printf "\u1b[14t" > /dev/tty
tmux+iterm2
printf "\u1bPtmux;]\u1b\u1b[14t\u1b\\" > /dev/tty
getpix() { read PIXEL_SIZE < /dev/tty & printf "\u1b[14t" > /dev/tty ;
echo "${PIXEL_SIZE}" ; }
Real world use, I'm trying to convert tput lines/cols to pixels for imgcat
rendering via gnuplot so the gnuplot png output will fill the terminal
window.
alias gnuplot="__gnuplot"
__gnuplot() {
SIZE=$(stty size 2>/dev/null)
SIZE=${SIZE:-$(tput lines) $(tput cols)}
COLS=${SIZE#* }
ROWS=${SIZE% *}
COLUMNS=${COLUMNS:-${COLS}}
LINES=${LINES:-${ROWS}}
case "${GNUTERM%% *}" in
dumb) X=${COLUMNS} ; Y=$((LINES-3));;
png|sixelgd) PIXEL_SIZE=`echo -ne "\e[14t" >/dev/tty` ;
PX=${PIXEL_SIZE#*;}; PX=${X%t}; PY=${PIXEL_SIZE%;*}
X=$((PX*COLUMNS/COLS))
Y=$((PX*LINES/ROWS))
;;
esac
sed -i "s/^set term[[:space:]][^[:space:]]*/set term ${GNUTERM%% *}/"
~/.gnuplot
case "${GNUTERM%% *}" in
esac
}
LINES=$((LINES/2)) gnuplot -e "plot sin(x)"
--
You received this message because you are subscribed to the Google Groups
"iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Raymond Page
2018-06-29 22:00:33 UTC
Permalink
That script provides everything I need for my terminal window probing,
thank you!

If I can ask one more question, do you know how I can have the tty buffer
arbitrary data?
Specifically, after executing the GetWinSizePixels CSI, I'd like to append
a newline to the tty's buffer so that I can use the shell builtin read
instead of dd. Is that possible?
Post by George Nachman
If stdout isn't a tty you're going to have a bad time. There are
inescapable race conditions where two process try to write to the tty at
the same time and what comes out the other side is a mélange of the two.
That notwithstanding, if you're feeling lucky, here's a script you could
https://gist.github.com/gnachman/3f3b78030e02b6949de9fe7f813ae47c
Post by Raymond Page
I can use some assistance with how to capture the CSI GetWinSizePixels
output in a shell variable?
The CSI GetWinSizePixels works (in iterm2 or nested tmux), but the output
doesn't seem to be to stdout, so I'm wondering if I need to read a tty/pts
to capture the output into a shell variable. Should I print these escapes
to the tty or pts directly, or to stdout of the shell?
iterm2
printf "\u1b[14t" > /dev/tty
tmux+iterm2
printf "\u1bPtmux;]\u1b\u1b[14t\u1b\\" > /dev/tty
getpix() { read PIXEL_SIZE < /dev/tty & printf "\u1b[14t" > /dev/tty ;
echo "${PIXEL_SIZE}" ; }
Real world use, I'm trying to convert tput lines/cols to pixels for
imgcat rendering via gnuplot so the gnuplot png output will fill the
terminal window.
alias gnuplot="__gnuplot"
__gnuplot() {
SIZE=$(stty size 2>/dev/null)
SIZE=${SIZE:-$(tput lines) $(tput cols)}
COLS=${SIZE#* }
ROWS=${SIZE% *}
COLUMNS=${COLUMNS:-${COLS}}
LINES=${LINES:-${ROWS}}
case "${GNUTERM%% *}" in
dumb) X=${COLUMNS} ; Y=$((LINES-3));;
png|sixelgd) PIXEL_SIZE=`echo -ne "\e[14t" >/dev/tty` ;
PX=${PIXEL_SIZE#*;}; PX=${X%t}; PY=${PIXEL_SIZE%;*}
X=$((PX*COLUMNS/COLS))
Y=$((PX*LINES/ROWS))
;;
esac
sed -i "s/^set term[[:space:]][^[:space:]]*/set term ${GNUTERM%% *}/"
~/.gnuplot
case "${GNUTERM%% *}" in
esac
}
LINES=$((LINES/2)) gnuplot -e "plot sin(x)"
--
You received this message because you are subscribed to the Google Groups
"iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
George Nachman
2018-07-02 18:06:46 UTC
Permalink
As far as I know that isn't possible.
Post by Raymond Page
That script provides everything I need for my terminal window probing,
thank you!
If I can ask one more question, do you know how I can have the tty buffer
arbitrary data?
Specifically, after executing the GetWinSizePixels CSI, I'd like to append
a newline to the tty's buffer so that I can use the shell builtin read
instead of dd. Is that possible?
Post by George Nachman
If stdout isn't a tty you're going to have a bad time. There are
inescapable race conditions where two process try to write to the tty at
the same time and what comes out the other side is a mélange of the two.
That notwithstanding, if you're feeling lucky, here's a script you could
https://gist.github.com/gnachman/3f3b78030e02b6949de9fe7f813ae47c
Post by Raymond Page
I can use some assistance with how to capture the CSI GetWinSizePixels
output in a shell variable?
The CSI GetWinSizePixels works (in iterm2 or nested tmux), but the
output doesn't seem to be to stdout, so I'm wondering if I need to read a
tty/pts to capture the output into a shell variable. Should I print these
escapes to the tty or pts directly, or to stdout of the shell?
iterm2
printf "\u1b[14t" > /dev/tty
tmux+iterm2
printf "\u1bPtmux;]\u1b\u1b[14t\u1b\\" > /dev/tty
getpix() { read PIXEL_SIZE < /dev/tty & printf "\u1b[14t" > /dev/tty ;
echo "${PIXEL_SIZE}" ; }
Real world use, I'm trying to convert tput lines/cols to pixels for
imgcat rendering via gnuplot so the gnuplot png output will fill the
terminal window.
alias gnuplot="__gnuplot"
__gnuplot() {
SIZE=$(stty size 2>/dev/null)
SIZE=${SIZE:-$(tput lines) $(tput cols)}
COLS=${SIZE#* }
ROWS=${SIZE% *}
COLUMNS=${COLUMNS:-${COLS}}
LINES=${LINES:-${ROWS}}
case "${GNUTERM%% *}" in
dumb) X=${COLUMNS} ; Y=$((LINES-3));;
png|sixelgd) PIXEL_SIZE=`echo -ne "\e[14t" >/dev/tty` ;
PX=${PIXEL_SIZE#*;}; PX=${X%t}; PY=${PIXEL_SIZE%;*}
X=$((PX*COLUMNS/COLS))
Y=$((PX*LINES/ROWS))
;;
esac
sed -i "s/^set term[[:space:]][^[:space:]]*/set term ${GNUTERM%%
*}/" ~/.gnuplot
case "${GNUTERM%% *}" in
esac
}
LINES=$((LINES/2)) gnuplot -e "plot sin(x)"
--
You received this message because you are subscribed to the Google
Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups
"iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "iterm2-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to iterm2-discuss+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...