3030using namespace facebook ;
3131namespace po = ::boost::program_options;
3232
33+ namespace {
3334template <typename T>
3435std::optional<T> getOptional (const po::variable_value& val) {
3536 return val.empty () ? std::nullopt : std::optional<T>(val.as <T>());
3637}
3738
39+ bool isColorfulTty () {
40+ auto isTty = isatty (fileno (stdout));
41+ if (!isTty) {
42+ return false ;
43+ }
44+
45+ auto term = std::getenv (" TERM" );
46+ return !(term == nullptr || term[0 ] == ' \0 ' || strcmp (term, " dumb" ) == 0 );
47+ }
48+
49+ } // namespace
50+
3851FOLLY_INIT_LOGGING_CONFIG (" CRITICAL" );
3952int main (int argc, char * argv[]) {
4053 facebook::config::Flags::overrideDefault (" minloglevel" , " 5" );
4154
4255 auto init = init::InitFacebookLight{
4356 &argc, &argv, folly::InitOptions ().useGFlags (false )};
4457
58+ // Enable colored output if we are running in a terminal
59+ bool enableColors = isColorfulTty ();
60+
4561 std::string version{BuildInfo::getBuildTimeISO8601 ()};
4662 if (!version.empty ()) {
4763 auto buildRevision = BuildInfo::getBuildRevision ();
@@ -66,10 +82,11 @@ int main(int argc, char* argv[]) {
6682 " <file>" ,
6783 " Print file information" ,
6884 " Prints file information from the file footer." ,
69- [](const po::variables_map& options,
70- const std::vector<std::string>& /* args*/ ) {
85+ [enableColors](
86+ const po::variables_map& options,
87+ const std::vector<std::string>& /* args*/ ) {
7188 nimble::tools::NimbleDumpLib{
72- std::cout, options[" file" ].as <std::string>()}
89+ std::cout, enableColors, options[" file" ].as <std::string>()}
7390 .emitInfo ();
7491 },
7592 makePositionalArgs ())
@@ -83,10 +100,11 @@ int main(int argc, char* argv[]) {
83100 " <file>" ,
84101 " Print overall layout of the file" ,
85102 " Print overall layout of the file" ,
86- [](const po::variables_map& options,
87- const std::vector<std::string>& /* args*/ ) {
103+ [enableColors](
104+ const po::variables_map& options,
105+ const std::vector<std::string>& /* args*/ ) {
88106 nimble::tools::NimbleDumpLib{
89- std::cout, options[" file" ].as <std::string>()}
107+ std::cout, enableColors, options[" file" ].as <std::string>()}
90108 .emitFileLayout (options[" no_header" ].as <bool >());
91109 },
92110 makePositionalArgs ())
@@ -108,10 +126,11 @@ int main(int argc, char* argv[]) {
108126 " <file>" ,
109127 " Print file schema" ,
110128 " Prints the file schema tree." ,
111- [](const po::variables_map& options,
112- const std::vector<std::string>& /* args*/ ) {
129+ [enableColors](
130+ const po::variables_map& options,
131+ const std::vector<std::string>& /* args*/ ) {
113132 nimble::tools::NimbleDumpLib{
114- std::cout, options[" file" ].as <std::string>()}
133+ std::cout, enableColors, options[" file" ].as <std::string>()}
115134 .emitSchema (!options[" full" ].as <bool >());
116135 },
117136 makePositionalArgs ())
@@ -133,10 +152,11 @@ int main(int argc, char* argv[]) {
133152 " <file>" ,
134153 " Print stripe information" ,
135154 " Prints detailed stripe information." ,
136- [](const po::variables_map& options,
137- const std::vector<std::string>& /* args*/ ) {
155+ [enableColors](
156+ const po::variables_map& options,
157+ const std::vector<std::string>& /* args*/ ) {
138158 nimble::tools::NimbleDumpLib{
139- std::cout, options[" file" ].as <std::string>()}
159+ std::cout, enableColors, options[" file" ].as <std::string>()}
140160 .emitStripes (options[" no_header" ].as <bool >());
141161 },
142162 makePositionalArgs ())
@@ -158,10 +178,11 @@ int main(int argc, char* argv[]) {
158178 " <file>" ,
159179 " Print stream information" ,
160180 " Prints detailed stream information." ,
161- [](const po::variables_map& options,
162- const std::vector<std::string>& /* args*/ ) {
181+ [enableColors](
182+ const po::variables_map& options,
183+ const std::vector<std::string>& /* args*/ ) {
163184 nimble::tools::NimbleDumpLib{
164- std::cout, options[" file" ].as <std::string>()}
185+ std::cout, enableColors, options[" file" ].as <std::string>()}
165186 .emitStreams (
166187 options[" no_header" ].as <bool >(),
167188 options[" labels" ].as <bool >(),
@@ -207,10 +228,11 @@ int main(int argc, char* argv[]) {
207228 " Print encoding histogram" ,
208229 " Prints encoding histogram, counting how many times each encoding "
209230 " appears in the file." ,
210- [](const po::variables_map& options,
211- const std::vector<std::string>& /* args*/ ) {
231+ [enableColors](
232+ const po::variables_map& options,
233+ const std::vector<std::string>& /* args*/ ) {
212234 nimble::tools::NimbleDumpLib{
213- std::cout, options[" file" ].as <std::string>()}
235+ std::cout, enableColors, options[" file" ].as <std::string>()}
214236 .emitHistogram (
215237 options[" root_only" ].as <bool >(),
216238 options[" no_header" ].as <bool >(),
@@ -245,10 +267,11 @@ int main(int argc, char* argv[]) {
245267 " <file>" ,
246268 " Print the content of a stream" ,
247269 " Prints the materialized content (actual values) of a stream." ,
248- [](const po::variables_map& options,
249- const std::vector<std::string>& /* args*/ ) {
270+ [enableColors](
271+ const po::variables_map& options,
272+ const std::vector<std::string>& /* args*/ ) {
250273 nimble::tools::NimbleDumpLib{
251- std::cout, options[" file" ].as <std::string>()}
274+ std::cout, enableColors, options[" file" ].as <std::string>()}
252275 .emitContent (
253276 options[" stream" ].as <uint32_t >(),
254277 getOptional<uint32_t >(options[" stripe" ]),
@@ -282,10 +305,11 @@ int main(int argc, char* argv[]) {
282305 " <file>" ,
283306 " Dumps stream binary content" ,
284307 " Dumps stream binary content to a file." ,
285- [](const po::variables_map& options,
286- const std::vector<std::string>& /* args*/ ) {
308+ [enableColors](
309+ const po::variables_map& options,
310+ const std::vector<std::string>& /* args*/ ) {
287311 nimble::tools::NimbleDumpLib{
288- std::cout, options[" file" ].as <std::string>()}
312+ std::cout, enableColors, options[" file" ].as <std::string>()}
289313 .emitBinary (
290314 [path = options[" output" ].as <std::string>()]() {
291315 return std::make_unique<std::ofstream>(
@@ -322,10 +346,11 @@ int main(int argc, char* argv[]) {
322346 " <file>" ,
323347 " Dumps layout file" ,
324348 " Dumps captured layout file content." ,
325- [](const po::variables_map& options,
326- const std::vector<std::string>& /* args*/ ) {
349+ [enableColors](
350+ const po::variables_map& options,
351+ const std::vector<std::string>& /* args*/ ) {
327352 nimble::tools::NimbleDumpLib{
328- std::cout, options[" file" ].as <std::string>()}
353+ std::cout, enableColors, options[" file" ].as <std::string>()}
329354 .emitLayout (
330355 options[" no_header" ].as <bool >(),
331356 !options[" uncompressed" ].as <bool >());
@@ -354,10 +379,11 @@ int main(int argc, char* argv[]) {
354379 " <file>" ,
355380 " Print stripes metadata information" ,
356381 " Prints stripes metadata information as referenced by the footer." ,
357- [](const po::variables_map& options,
358- const std::vector<std::string>& /* args*/ ) {
382+ [enableColors](
383+ const po::variables_map& options,
384+ const std::vector<std::string>& /* args*/ ) {
359385 nimble::tools::NimbleDumpLib{
360- std::cout, options[" file" ].as <std::string>()}
386+ std::cout, enableColors, options[" file" ].as <std::string>()}
361387 .emitStripesMetadata (options[" no_header" ].as <bool >());
362388 },
363389 makePositionalArgs ())
@@ -379,10 +405,11 @@ int main(int argc, char* argv[]) {
379405 " <file>" ,
380406 " Print stripe groups metadata information" ,
381407 " Prints stripe groups information as referenced by the footer." ,
382- [](const po::variables_map& options,
383- const std::vector<std::string>& /* args*/ ) {
408+ [enableColors](
409+ const po::variables_map& options,
410+ const std::vector<std::string>& /* args*/ ) {
384411 nimble::tools::NimbleDumpLib{
385- std::cout, options[" file" ].as <std::string>()}
412+ std::cout, enableColors, options[" file" ].as <std::string>()}
386413 .emitStripeGroupsMetadata (options[" no_header" ].as <bool >());
387414 },
388415 makePositionalArgs ())
@@ -403,10 +430,11 @@ int main(int argc, char* argv[]) {
403430 " <file>" ,
404431 " Print optional sections information" ,
405432 " Prints optional sections information as referenced by the footer." ,
406- [](const po::variables_map& options,
407- const std::vector<std::string>& /* args*/ ) {
433+ [enableColors](
434+ const po::variables_map& options,
435+ const std::vector<std::string>& /* args*/ ) {
408436 nimble::tools::NimbleDumpLib{
409- std::cout, options[" file" ].as <std::string>()}
437+ std::cout, enableColors, options[" file" ].as <std::string>()}
410438 .emitOptionalSectionsMetadata (options[" no_header" ].as <bool >());
411439 },
412440 makePositionalArgs ())
0 commit comments