hare

[hare] The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

winsize.ha (616B)


      1 // SPDX-License-Identifier: MPL-2.0
      2 // (c) Hare authors <https://harelang.org>
      3 
      4 use errors;
      5 use io;
      6 use os;
      7 use rt;
      8 
      9 // Returns the dimensions of underlying terminal for an [[io::file]].
     10 export fn winsize(fd: io::file) (ttysize | error) = {
     11 	let wsz = rt::winsize { ... };
     12 	match (rt::ioctl(fd, rt::TIOCGWINSZ, &wsz: *opaque)) {
     13 	case let e: rt::errno =>
     14 		switch (e) {
     15 		case rt::EBADF =>
     16 			return errors::invalid;
     17 		case rt::ENOTTY =>
     18 			return errors::unsupported;
     19 		case =>
     20 			abort("Unexpected error from ioctl");
     21 		};
     22 	case int =>
     23 		return ttysize {
     24 			rows = wsz.ws_row,
     25 			columns = wsz.ws_col,
     26 		};
     27 	};
     28 };