When you write \e[6m
(the "tell-me-where-I-am" code) to a compliant terminal, it will write back \e[<ROW>;<COLUMN>R
, eg \e75;1R
.
NOTE: all the \e
s here are the escape code, not literal "slash ee"s, so what's actually written back is garbled if not captured.
In bash, you can read & output this via:
#!/bin/bash
echo -en "\E[6n";
read -sdR CURTEXT;
CURPOS=${CURTEXT#*[};
echo $CURPOS;
Or the one-line CLI command echo -en "\E[6n"; read -sdR ROW_COL_RAW; ROW_COL=${ROW_COL_RAW#*[}; printf "%b\n" $ROW_COL;
.
NOTE: I got this from https://stackoverflow.com/a/6003016/3969962.
While bash can obtain this text via read, I can't figure out how to obtain it from C++.
The escaped coordinate text is still being written by the terminal itself, but I can't read these characters from std::cin
, even by fread()
.
- How, and where, is this text actually being written (by the terminal iteslf)?
- Is there some way I can read this data in C++ (eg, to a std::string)? (hopefully portably and without race conditions).
My goal is to determine the current tab width so that output can be nicely displayed. I got the terminal dimensions via ioctl(0, TIOCGWINSZ, &win_size)
.