diff --git a/.gitignore b/.gitignore index af5d7fa..566b567 100644 --- a/.gitignore +++ b/.gitignore @@ -47,6 +47,10 @@ x64/ *.vcxproj.* +###Visual Studio Code### +.vscode/ + + ###Java### # Compiled class file diff --git a/src/tiv.cpp b/src/tiv.cpp index 3b94bb6..034b9a7 100644 --- a/src/tiv.cpp +++ b/src/tiv.cpp @@ -52,12 +52,16 @@ #ifdef _POSIX_VERSION // Console output size detection #include +// Error explanation, for some reason +#include // Exit codes #include #endif #ifdef _WIN32 #include +// Error explanation +#include // Following codes copied from /usr/include/sysexits.h, // license: https://opensource.org/license/BSD-3-clause/ @@ -626,43 +630,11 @@ enum Mode { AUTO, THUMBNAILS, FULL_SIZE }; int main(int argc, char *argv[]) { std::ios::sync_with_stdio(false); // apparently makes printing faster - - // Platform-specific implementations for determining console size, better - // implementations are welcome + bool detectSize = true; // Fallback sizes when unsuccesful. Sizes are actually 1/4th of the actual int maxWidth = 80; int maxHeight = 24; -#ifdef _POSIX_VERSION - struct winsize w; - // If redirecting STDOUT to one file ( col or row == 0, or the previous - // ioctl call's failed ) - if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != 0 || - (w.ws_col | w.ws_row) == 0) { - std::cerr << "Warning: failed to determine most reasonable size, " - "defaulting to 20x6" - << std::endl; - } else { - maxWidth = w.ws_col * 4; - maxHeight = w.ws_row * 8; - } -#elif defined _WIN32 - CONSOLE_SCREEN_BUFFER_INFO w; - if (GetConsoleScreenBufferInfo( - GetStdHandle(STD_OUTPUT_HANDLE), - &w)) { // just like powershell, but without the hyphens, hooray - maxWidth = w.dwSize.X * 4; - maxHeight = w.dwSize.Y * 8; - } else { - std::cerr - << "Warning: failed to determine most reasonable size: Error code" - << GetLastError() << ", defaulting to 20x6" << std::endl; - } -#else - std::cerr << "Warning: failed to determine most reasonable size: " - "unrecognized system, defaulting to 20x6" - << std::endl; -#endif // Reading input int8_t flags = 0; // bitwise representation of flags, @@ -675,7 +647,7 @@ int main(int argc, char *argv[]) { if (argc <= 1) { printUsage(); - return 0; + return EX_USAGE; } for (int i = 1; i < argc; i++) { @@ -695,16 +667,16 @@ int main(int argc, char *argv[]) { mode = FULL_SIZE; } else if (arg == "-w") { if (i < argc - 1) { - maxWidth = 4 * std::stoi(argv[++i]); + maxWidth = 4 * std::stoi(argv[++i]), detectSize = false; } else { std::cerr << "Error: -w requires a number" << std::endl; ret = EX_USAGE; } } else if (arg == "-h") { if (i < argc - 1) - maxHeight = 8 * std::stoi(argv[++i]); + maxHeight = 8 * std::stoi(argv[++i]), detectSize = false; else - printUsage(); + printUsage(); // people might confuse this with help } else if (arg == "--256" || arg == "-2" || arg == "-256") { flags |= FLAG_MODE_256; } else if (arg == "--help" || arg == "-help") { @@ -721,7 +693,7 @@ int main(int argc, char *argv[]) { if (std::filesystem::is_regular_file(p.path())) file_names.push_back(p.path().string()); } else { - // Check if file can be opened + // Check if file can be opened, @TODO find better way std::ifstream fin(arg.c_str()); if (fin) { file_names.push_back(arg); @@ -734,6 +706,40 @@ int main(int argc, char *argv[]) { } } + if (detectSize) { + // Platform-specific implementations for determining console size, + // better implementations are welcome +#ifdef _POSIX_VERSION + struct winsize w; + // If redirecting STDOUT to one file ( col or row == 0, or the previous + // ioctl call's failed ) + if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) != 0 || + (w.ws_col | w.ws_row) == 0) { + std::cerr << "Warning: failed to determine most reasonable size: " + << strerror(errno) << ", defaulting to 20x6" << std::endl; + } else { + maxWidth = w.ws_col * 4; + maxHeight = w.ws_row * 8; + } +#elif defined _WIN32 + CONSOLE_SCREEN_BUFFER_INFO w; + if (GetConsoleScreenBufferInfo( + GetStdHandle(STD_OUTPUT_HANDLE), + &w)) { // just like powershell, but without the hyphens, hooray + maxWidth = w.dwSize.X * 4; + maxHeight = w.dwSize.Y * 8; + } else { + std::cerr << "Warning: failed to determine most reasonable size: " + << std::system_category().message(GetLastError()) + << ", defaulting to 20x6" << std::endl; + } +#else + std::cerr << "Warning: failed to determine most reasonable size: " + "unrecognized system, defaulting to 20x6" + << std::endl; +#endif + } + if (mode == FULL_SIZE || (mode == AUTO && file_names.size() == 1)) { for (const auto &filename : file_names) { try {