How do I get the terminal size in Go. In C it would look like this:
struct ttysize ts;
ioctl(0, TIOCGWINSZ, &ts);
But how to i access TIOCGWINSZ in Go
How do I get the terminal size in Go. In C it would look like this:
struct ttysize ts;
ioctl(0, TIOCGWINSZ, &ts);
But how to i access TIOCGWINSZ in Go
The cgo compiler can t handle variable arguments in a c function and macros in c header files at present, so you can t do a simple
// #include <sys/ioctl.h>
// typedef struct ttysize ttysize;
import "C"
func GetWinSz() {
var ts C.ttysize;
C.ioctl(0,C.TIOCGWINSZ,&ts)
}
To get around the macros use a constant, so
// #include <sys/ioctl.h>
// typedef struct ttysize ttysize;
import "C"
const TIOCGWINSZ C.ulong = 0x5413; // Value from Jed Smith s answer
func GetWinSz() {
var ts C.ttysize;
C.ioctl(0,TIOCGWINSZ,&ts)
}
However cgo will still barf on the ... in ioctl s prototype. Your best bet would be to wrap ioctl with a c function taking a specific number of arguments and link that in. As a hack you can do that in the comment above import "C"
// #include <sys/ioctl.h>
// typedef struct ttysize ttysize;
// void myioctl(int i, unsigned long l, ttysize * t){ioctl(i,l,t);}
import "C"
const TIOCGWINSZ C.ulong = 0x5413; // Value from Jed Smith s answer
func GetWinSz() {
var ts C.ttysize;
C.myioctl(0,TIOCGWINSZ,&ts)
}
I ve not tested this, but something similar should work.
The best way to do this would be to use the syscall package. The syscall package does not define an ioctl function because it just does so many different things, but you can still call it like this:
syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(TIOCGWINSZ), uintptr(unsafe.Pointer(&ts)))
The two things left are to duplicate the winsize structure and the constant you need. The tool for this is godefs, which will generate a .go source file by looking at structures and constants in C headers. Create a termios.c file that looks like this:
#include <termios.h>
enum {
$TIOCGWINSZ = TIOCGWINSZ
};
typedef winsize $winsize;
Now run
godefs -gpackagename termios.c > termios.go
Now you should have everything you need to get the terminal size. Setting the size is as simple as adding another constant to termios.c.
read: http://www.darkcoding.net/software/pretty-command-line-console-output-on-unix-in-python-and-go-lang/
const (
TIOCGWINSZ = 0x5413
TIOCGWINSZ_OSX = 1074295912
)
type window struct {
Row uint16
Col uint16
Xpixel uint16
Ypixel uint16
}
func terminalWidth() (int, error) {
w := new(window)
tio := syscall.TIOCGWINSZ
if runtime.GOOS == "darwin" {
tio = TIOCGWINSZ_OSX
}
res, _, err := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(tio),
uintptr(unsafe.Pointer(w)),
)
if int(res) == -1 {
return 0, err
}
return int(w.Col), nil
}
It doesn t look like much work has been done on this yet from a casual glance of the documentation -- in fact, I can t find ioctl
at all.
With a language this early in its infancy, it s safe to say that you re treading unwalked ground. TIOCGWINSZ
, itself, is just a constant integer (I found its value in the Linux source code):
#define TIOCGWINSZ 0x5413
Good luck, though.
When working in Unix with various programming languages I often use Kate as my primary editor. It has a really nice function of being able to open a built-in terminal window which is quite useful. ...
Consider the following: me@mine:~$ cat a.sh #!/bin/bash echo "Lines: " $LINES echo "Columns: " $COLUMNS me@mine:~$ ./a.sh Lines: Columns: me@mine:~$ echo "Lines: " $LINES Lines: 52 me@mine:~$ ...
Is there any way to show compilation errors in colors on the terminal? I mean when we do "g++ filename.cpp", is there a way to show the compiler messages in colors? By default it is always in Black ...
I am running my java application where i have used threads... i am running this application using ant command on the terminal.. But when i close my terminal or press ctrl+c,then java program which was ...
My configuration is windows server 2003 (i m logged in with admin privileges), office 2003, vtso runtime 2005 se. After installing my addin, all registry keys are in the correct locations and I have ...
For those who are using vim/emacs/terminals,etc (ie, not an IDE proper) what sort of projects are you working on? Are they big? Production? Are these the tools you use at work? Or mostly for smaller ...
How do I get the terminal size in Go. In C it would look like this: struct ttysize ts; ioctl(0, TIOCGWINSZ, &ts); But how to i access TIOCGWINSZ in Go
I ve been googling around trying to figure out if it s possible to use my mouse wheel to scroll while inside Vim in Mac s Terminal, with no luck. It seems as if only X11 or iTerm support this. Before ...