├── .gitignore ├── .gitattributes ├── terminal-size ├── .editorconfig ├── terminal-size.c ├── readme.md └── license /.gitignore: -------------------------------------------------------------------------------- 1 | /terminal-size 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /terminal-size: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sindresorhus/macos-terminal-size/HEAD/terminal-size -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | -------------------------------------------------------------------------------- /terminal-size.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include // strerror 3 | #include // errno 4 | #include // open(), O_EVTONLY, O_NONBLOCK 5 | #include // close() 6 | #include // ioctl() 7 | 8 | int main() { 9 | int tty_fd = open("/dev/tty", O_EVTONLY | O_NONBLOCK); 10 | if (tty_fd == -1) { 11 | fprintf(stderr, "Opening `/dev/tty` failed (%d): %s\n", errno, strerror(errno)); 12 | return 1; 13 | } 14 | 15 | struct winsize ws; 16 | int result = ioctl(tty_fd, TIOCGWINSZ, &ws); 17 | close(tty_fd); 18 | 19 | if (result == -1) { 20 | fprintf(stderr, "Getting the size failed (%d): %s\n", errno, strerror(errno)); 21 | return 1; 22 | } 23 | 24 | fprintf(stdout, "%d\n%d\n", ws.ws_col, ws.ws_row); 25 | return 0; 26 | } 27 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # macos-terminal-size 2 | 3 | > Get the terminal window size on macOS 4 | 5 | Works even when run [non-interactively](http://www.tldp.org/LDP/abs/html/intandnonint.html), for example, in a child process or when piped. 6 | 7 | ## Install 8 | 9 | [Download](https://github.com/sindresorhus/macos-terminal-size/releases/latest) the binary and put it in `/usr/local/bin`. 10 | 11 | ## Usage 12 | 13 | ``` 14 | $ terminal-size 15 | 143 16 | 24 17 | ``` 18 | 19 | Where `143` are the number of columns and `24` are the number of rows. 20 | 21 | ## Comparison 22 | 23 | ``` 24 | $ tput cols 25 | 158 26 | $ tput cols &> x; cat x 27 | 80 28 | ``` 29 | 30 | ``` 31 | $ terminal-size 32 | 158 33 | 25 34 | $ terminal-size &> x; cat x 35 | 158 36 | 25 37 | ``` 38 | 39 | ## Build 40 | 41 | ```sh 42 | ./build 43 | ``` 44 | 45 | ## Related 46 | 47 | - [terminal-size](https://github.com/sindresorhus/terminal-size) - Get the terminal window size, cross-platform 48 | - [windows-terminal-size](https://github.com/sindresorhus/windows-terminal-size) - Get the terminal window size on Windows 49 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | --------------------------------------------------------------------------------