Skip to content

Commit f12b0a9

Browse files
committed
Be smarter about terminal width and handle command-line wildcards on Win32.
1 parent f62d01a commit f12b0a9

File tree

3 files changed

+66
-31
lines changed

3 files changed

+66
-31
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
cmake_minimum_required(VERSION 3.1.0)
1+
cmake_minimum_required(VERSION 3.15 FATAL_ERROR)
22

33
project(cambio)
44

@@ -161,6 +161,10 @@ add_executable( ${PROJECT_NAME} ${GUI_TYPE} main.cpp ${sources} ${headers} )
161161

162162
if( BUILD_CAMBIO_COMMAND_LINE )
163163
target_link_libraries( ${PROJECT_NAME} PRIVATE ${BOOST_PROGRAM_OPTIONS} )
164+
165+
if(WIN32)
166+
set_target_properties( ${PROJECT_NAME} PROPERTIES LINK_FLAGS "wsetargv.obj")
167+
endif(WIN32)
164168
endif()
165169

166170
if( NOT BUILD_CAMBIO_GUI )

main.cpp

100644100755
Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,20 @@ Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin);
4949
#endif
5050

5151
#ifdef _WIN32
52-
void getUtf8Args( int &argc, char ** &argv );
52+
void getUtf8Args( int &argc, wchar_t **wargv, char ** &argv );
5353
#endif
54+
#include <iostream>
5455

56+
#ifdef _WIN32
57+
int wmain( int argc, wchar_t *wargv[] )
58+
{
59+
char **argv;
60+
getUtf8Args( argc, wargv, argv );
61+
#else
5562
int main( int argc, char *argv[] )
5663
{
57-
#ifdef _WIN32
58-
getUtf8Args( argc, argv );
5964
#endif
60-
65+
6166
#if( BUILD_CAMBIO_COMMAND_LINE )
6267
#if( BUILD_CAMBIO_GUI )
6368
if( CommandLineUtil::requested_command_line_from_gui( argc, argv ) )
@@ -77,29 +82,13 @@ int main( int argc, char *argv[] )
7782

7883

7984
#ifdef _WIN32
80-
81-
#define WIN32_LEAN_AND_MEAN 1
82-
#include <windows.h>
83-
#include <stdlib.h>
84-
#include <stdio.h>
85-
#include <shellapi.h>
86-
87-
#include <string>
88-
#include "SpecUtils/Filesystem.h"
8985
#include "SpecUtils/StringAlgo.h"
9086

9187
/** Get command line arguments encoded as UTF-8.
9288
This function just leaks the memory
9389
*/
94-
void getUtf8Args( int &argc, char ** &argv )
90+
void getUtf8Args( int &argc, wchar_t **argvw, char ** &argv )
9591
{
96-
LPWSTR *argvw = CommandLineToArgvW( GetCommandLineW(), &argc );
97-
if( !argvw )
98-
{
99-
printf( "CommandLineToArgvW failed - good luck\n" );
100-
return ;
101-
}
102-
10392
argv = (char **)malloc(sizeof(char *)*argc);
10493

10594
for( int i = 0; i < argc; ++i)
@@ -109,9 +98,6 @@ void getUtf8Args( int &argc, char ** &argv )
10998
argv[i] = (char *)malloc( sizeof(char)*(asutf8.size()+1) );
11099
strcpy( argv[i], asutf8.c_str() );
111100
}//for( int i = 0; i < argc; ++i)
112-
113-
// Free memory allocated for CommandLineToArgvW arguments.
114-
LocalFree(argvw);
115101
}//void processCustomArgs()
116102

117103
#endif //_WIN32

src/CommandLineUtil.cpp

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@
3232
#include "SpecUtils/Filesystem.h"
3333
#include "cambio/CommandLineUtil.h"
3434

35+
36+
// Some includes to get terminal width
37+
#if defined(__APPLE__) || defined(linux) || defined(unix) || defined(__unix) || defined(__unix__)
38+
#include <sys/ioctl.h>
39+
#include <unistd.h>
40+
#elif defined(_WIN32)
41+
#define NOMINMAX
42+
#define WIN32_LEAN_AND_MEAN 1
43+
#include <Windows.h>
44+
#endif
45+
46+
3547
using namespace std;
3648
namespace po = boost::program_options;
3749
using SpecUtils::convert_from_utf8_to_utf16;
@@ -64,6 +76,7 @@ namespace {
6476
}
6577
#endif
6678

79+
6780
bool html_page_header( std::ostream &ostr, const std::string &title )
6881
{
6982
const char *endline = "\r\n";
@@ -464,6 +477,35 @@ namespace
464477
}
465478
}//if( print_debug )
466479
}//void normalize_det_name_to_n42( SpecUtils::SpecFile &info )
480+
481+
482+
#if defined(__APPLE__) || defined(unix) || defined(__unix) || defined(__unix__)
483+
unsigned terminal_width()
484+
{
485+
winsize ws = {};
486+
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) <= -1)
487+
return 80;
488+
unsigned w = (ws.ws_col);
489+
return std::max( 40u, w );
490+
}
491+
#elif defined(_WIN32)
492+
unsigned terminal_width()
493+
{
494+
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
495+
if( handle == INVALID_HANDLE_VALUE )
496+
return 80;
497+
498+
CONSOLE_SCREEN_BUFFER_INFO info;
499+
if( !GetConsoleScreenBufferInfo(handle, &info) )
500+
return 80;
501+
502+
return unsigned(info.srWindow.Right - info.srWindow.Left);
503+
}
504+
#else
505+
static_assert( 0, "Not unix and not win32? Unsupported getting terminal width" );
506+
#endif
507+
508+
467509
}//namespace
468510

469511
namespace CommandLineUtil
@@ -518,8 +560,11 @@ int run_command_util( const int argc, char *argv[] )
518560
string template_file;
519561
bool strip_template_blocks;
520562
#endif
521-
522-
po::options_description cl_desc("Allowed options");
563+
564+
unsigned term_width = terminal_width();
565+
unsigned min_description_length = ((term_width < 80u) ? term_width/2u : 40u);
566+
567+
po::options_description cl_desc("Allowed options", term_width, min_description_length);
523568
cl_desc.add_options()
524569
("help,h", "produce this help message")
525570
("about,a", "produce the about message")
@@ -532,8 +577,8 @@ int run_command_util( const int argc, char *argv[] )
532577
("format,f", po::value<string>(&outputformatstr),
533578
"Format of output spectrum file. Must be specified when there"
534579
" are multiple input files, or if the output name for a single"
535-
" spectrum file is ambigous. For a single file, if this option"
536-
" is not specified, the output names file extention will be used"
580+
" spectrum file is ambiguous. For a single file, if this option"
581+
" is not specified, the output names file extension will be used"
537582
" to guess output format desired.\n"
538583
"Possible values are: \tTXT, CSV, PCF, XML (2006 N42 format),"
539584
" N42 (defaults to 2012 variant), 2012N42, 2006N42,"
@@ -699,7 +744,7 @@ int run_command_util( const int argc, char *argv[] )
699744
" \n"
700745
"Please email wcjohns@sandia.gov AND cambio@sandia.gov with bug reports, \n"
701746
"suggestions, requests, or new file formats. \n"
702-
"Please visit https://hekili.ca.sandia.gov/CAMBIO/ for updates and additional \n"
747+
"Please visit https://github.com/sandialabs/cambio for updates and additional \n"
703748
"information.\n"
704749
"\n"
705750
"Copyright (C) 2014 Sandia National Laboratories, org 08126\n"
@@ -954,7 +999,7 @@ int run_command_util( const int argc, char *argv[] )
954999
if( inputfiles.size() > 1 && !SpecUtils::is_directory(outputname) )
9551000
{
9561001
cerr << "You must specify an output directory when there are mutliple input"
957-
<< " files" << endl;
1002+
<< " files -- SpecUtils::is_directory('" << outputname << "')=" << SpecUtils::is_directory(outputname) << endl;
9581003
return 3;
9591004
}//if( mutliple input files, and not a output directory )
9601005

0 commit comments

Comments
 (0)