From b215cb850e7940fb6789f0e6f5c91054fa5fb927 Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Fri, 8 May 2020 18:09:08 -0500 Subject: [PATCH] Added functions to retrieve terminal width --- include/indicators/terminal_size.hpp | 63 ++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 include/indicators/terminal_size.hpp diff --git a/include/indicators/terminal_size.hpp b/include/indicators/terminal_size.hpp new file mode 100644 index 0000000..e67ad63 --- /dev/null +++ b/include/indicators/terminal_size.hpp @@ -0,0 +1,63 @@ +/* +Activity Indicators for Modern C++ +https://github.com/p-ranav/indicators + +Licensed under the MIT License . +SPDX-License-Identifier: MIT +Copyright (c) 2019 Pranav Srinivas Kumar . + +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: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +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. +*/ +#pragma once +#include + +namespace indicators { + +#if defined(_MSC_VER) +#include + +std::pair terminal_size() { + CONSOLE_SCREEN_BUFFER_INFO csbi; + int columns, rows; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); + columns = csbi.srWindow.Right - csbi.srWindow.Left + 1; + rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; + return {static_cast(rows), static_cast(cols)}; +} + +size_t terminal_width() { + return terminal_size().second; +} + +#else +#include //ioctl() and TIOCGWINSZ +#include // for STDOUT_FILENO + +std::pair terminal_size() { + struct winsize size; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &size); + return {static_cast(size.ws_row), static_cast(size.ws_col)}; +} + +size_t terminal_width() { + return terminal_size().second; +} +#endif + +} \ No newline at end of file