package main import "core:fmt" import "core:os" import "core:sys/unix" foreign import libc "system:c" // Terminal codes CLEAR_CODE :: "\033[2J" HIDE_CURSOR :: "\033[?25l" RESET :: "\0033[0m" TIOCGWINSZ :: 0x5413 winsize :: struct { ws_row: u16, ws_col: u16, ws_xpixel: u16, ws_ypixel: u16, } @(default_calling_convention="c") foreign libc { ioctl :: proc(fd: i32, request: u64, #c_vararg args: ..any) -> i32 --- signal :: proc(signum: i32, handler: proc "c" (i32)) -> rawptr --- } SIGWINCH :: 28 resize_occurred := false signal_handler :: proc "c" (sig: i32) { resize_occurred = true } get_terminal_size :: proc() -> (rows, cols: int, ok: bool) { ws: winsize result := ioctl(i32(os.stdout), TIOCGWINSZ, &ws) if result == -1 { return 0, 0, false } return int(ws.ws_row), int(ws.ws_col), true } main :: proc() { signal(SIGWINCH, signal_handler) if len(os.args) != 2 { fmt.eprintln("INCORRECT NUMBER OF COMMAND LINE ARGUMENTS") return } filename := os.args[1] data, ok := os.read_entire_file_from_filename(os.args[1]) if !ok { fmt.eprintfln("ERROR: failed to open file: %s", filename) } for { if resize_occurred { resize_occurred = false rows, cols, ok := get_terminal_size() if ok { fmt.printf("Terminal resized! New size: %d rows x %d cols\n", rows, cols) } } } }