Skip to content

Commit 0058646

Browse files
committed
Also use -g grep for tabular output.
1 parent 8f78c8a commit 0058646

File tree

12 files changed

+79
-18
lines changed

12 files changed

+79
-18
lines changed

bant/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ cc_binary(
5454
"//bant/tool:edit-callback",
5555
"//bant/tool:workspace",
5656
"//bant/util:file-utils",
57+
"//bant/util:grep-highlighter",
5758
"//bant/util:stat",
5859
"//bant/util:table-printer",
5960
"@abseil-cpp//absl/container:flat_hash_set",

bant/cli-commands.cc

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "bant/types-bazel.h"
3939
#include "bant/types.h"
4040
#include "bant/util/file-utils.h"
41+
#include "bant/util/grep-highlighter.h"
4142
#include "bant/util/stat.h"
4243
#include "bant/util/table-printer.h"
4344
#include "bant/workspace.h"
@@ -84,8 +85,13 @@ enum class Command {
8485
void PrintOneToN(bant::Session &session, const BazelTargetMatcher &pattern,
8586
const OneToN<BazelTarget, BazelTarget> &table,
8687
const std::string &header1, const std::string &header2) {
87-
auto printer = TablePrinter::Create(
88-
session.out(), session.flags().output_format, {header1, header2});
88+
GrepHighlighter highlighter(session.flags().do_color);
89+
highlighter.AddExpressions(session.flags().grep_expressions,
90+
session.flags().regex_case_insesitive,
91+
session.error());
92+
auto printer =
93+
TablePrinter::Create(session.out(), session.flags().output_format,
94+
highlighter, {header1, header2});
8995
std::vector<std::string> repeat_print;
9096
for (const auto &d : table) {
9197
if (!pattern.Match(d.first)) continue;
@@ -329,8 +335,14 @@ CliStatus RunCommand(Session &session, Command cmd,
329335
break;
330336

331337
case Command::kListPackages: {
332-
auto printer = TablePrinter::Create(
333-
session.out(), session.flags().output_format, {"bazel-file", "package"});
338+
GrepHighlighter highlighter(session.flags().do_color);
339+
highlighter.AddExpressions(session.flags().grep_expressions,
340+
session.flags().regex_case_insesitive,
341+
session.error());
342+
343+
auto printer =
344+
TablePrinter::Create(session.out(), session.flags().output_format,
345+
highlighter, {"bazel-file", "package"});
334346
for (const auto &[package, parsed] : project.ParsedFiles()) {
335347
printer->AddRow({std::string(parsed->name()), package.ToString()});
336348
}
@@ -339,9 +351,14 @@ CliStatus RunCommand(Session &session, Command cmd,
339351

340352
case Command::kListLeafs:
341353
case Command::kListTargets: {
354+
GrepHighlighter highlighter(session.flags().do_color);
355+
highlighter.AddExpressions(session.flags().grep_expressions,
356+
session.flags().regex_case_insesitive,
357+
session.error());
358+
342359
auto printer =
343360
TablePrinter::Create(session.out(), session.flags().output_format,
344-
{"file-location", "rule", "target"});
361+
highlighter, {"file-location", "rule", "target"});
345362
for (const auto &[package, parsed] : project.ParsedFiles()) {
346363
FindTargets(parsed->ast, {}, [&](const Result &target) {
347364
auto target_name =

bant/explore/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ cc_library(
2020
"//bant/frontend:parsed-project",
2121
"//bant/frontend:parser",
2222
"//bant/util:file-utils",
23+
"//bant/util:grep-highlighter",
2324
"//bant/util:stat",
2425
"//bant/util:table-printer",
2526
"//bant/util:thread-pool",
@@ -69,6 +70,7 @@ cc_library(
6970
"//bant/frontend:parsed-project",
7071
"//bant/frontend:parser",
7172
"//bant/util:file-utils",
73+
"//bant/util:grep-highlighter",
7274
"//bant/util:table-printer",
7375
"@abseil-cpp//absl/container:btree",
7476
"@abseil-cpp//absl/strings",

bant/explore/dependency-graph.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "bant/types.h"
4040
#include "bant/util/file-utils.h"
4141
#include "bant/util/filesystem.h"
42+
#include "bant/util/grep-highlighter.h"
4243
#include "bant/util/stat.h"
4344
#include "bant/util/table-printer.h"
4445
#include "bant/util/thread-pool.h"
@@ -370,10 +371,12 @@ DependencyGraph BuildDependencyGraph(Session &session,
370371
"Dependency graph: Did not find these packages\n",
371372
error_packages);
372373
}
374+
const GrepHighlighter highlighter(false);
373375
if (!error_target_example.empty()) {
374376
session.info() << "Dependency graph: Did not find these targets\n";
375-
auto printer = TablePrinter::Create(session.info(), OutputFormat::kNative,
376-
{"Dependency", "needed-by"});
377+
auto printer =
378+
TablePrinter::Create(session.info(), OutputFormat::kNative, highlighter,
379+
{"Dependency", "needed-by"});
377380
// Ascii table does not have header, so add our own here.
378381
printer->AddRow({"[--- Dependency ---]", "[--- Example Needed By ---]"});
379382
for (const auto &[dep, example] : error_target_example) {

bant/explore/header-providers.cc

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "bant/types-bazel.h"
3939
#include "bant/types.h"
4040
#include "bant/util/filesystem.h"
41+
#include "bant/util/grep-highlighter.h"
4142
#include "bant/util/table-printer.h"
4243

4344
// The header providers maps header filenames to all the libraries that
@@ -470,9 +471,13 @@ std::optional<FindResult> FindBySuffix(const ProvidedFromTargetSet &index,
470471
void PrintProvidedSources(Session &session, const std::string &table_header,
471472
const BazelTargetMatcher &pattern,
472473
const ProvidedFromTarget &provided_from_lib) {
474+
GrepHighlighter highlighter(session.flags().do_color);
475+
highlighter.AddExpressions(session.flags().grep_expressions,
476+
session.flags().regex_case_insesitive,
477+
session.error());
473478
auto printer =
474479
TablePrinter::Create(session.out(), session.flags().output_format,
475-
{table_header, "providing-rule"});
480+
highlighter, {table_header, "providing-rule"});
476481
for (const auto &[provided, lib] : provided_from_lib) {
477482
if (!pattern.Match(lib)) continue;
478483
printer->AddRow({provided, lib.ToString()});
@@ -483,9 +488,14 @@ void PrintProvidedSources(Session &session, const std::string &table_header,
483488
void PrintProvidedSources(Session &session, const std::string &table_header,
484489
const BazelTargetMatcher &pattern,
485490
const ProvidedFromTargetSet &provided_from_lib) {
491+
GrepHighlighter highlighter(session.flags().do_color);
492+
highlighter.AddExpressions(session.flags().grep_expressions,
493+
session.flags().regex_case_insesitive,
494+
session.error());
495+
486496
auto printer =
487497
TablePrinter::Create(session.out(), session.flags().output_format,
488-
{table_header, "providing-rule"});
498+
highlighter, {table_header, "providing-rule"});
489499
for (const auto &[provided, libs] : provided_from_lib) {
490500
std::vector<std::string> list;
491501
for (const BazelTarget &target : libs) {

bant/tool/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ cc_library(
134134
"//bant/explore:query-utils",
135135
"//bant/frontend:parsed-project",
136136
"//bant/util:file-utils",
137+
"//bant/util:grep-highlighter",
137138
"//bant/util:table-printer",
138139
],
139140
)

bant/tool/workspace.cc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "bant/types-bazel.h"
2828
#include "bant/types.h"
2929
#include "bant/util/file-utils.h"
30+
#include "bant/util/grep-highlighter.h"
3031
#include "bant/util/table-printer.h"
3132
#include "bant/workspace.h"
3233

@@ -37,9 +38,14 @@ namespace bant {
3738
static void PrintExternalRepos(
3839
Session &session,
3940
const OneToOne<VersionedProject, FilesystemPath> &external_repos) {
41+
GrepHighlighter highlighter(session.flags().do_color);
42+
highlighter.AddExpressions(session.flags().grep_expressions,
43+
session.flags().regex_case_insesitive,
44+
session.error());
45+
4046
auto printer =
4147
TablePrinter::Create(session.out(), session.flags().output_format,
42-
{"project", "version", "directory"});
48+
highlighter, {"project", "version", "directory"});
4349
for (const auto &[project, file] : external_repos) {
4450
printer->AddRow({project.project,
4551
project.version.empty() ? "-" : project.version,

bant/util/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ cc_library(
3939
srcs = ["table-printer.cc"],
4040
hdrs = ["table-printer.h"],
4141
deps = [
42+
":grep-highlighter",
4243
"//bant:session",
4344
"@abseil-cpp//absl/log:check",
4445
"@abseil-cpp//absl/strings",
@@ -66,6 +67,7 @@ cc_test(
6667
name = "table-printer_test",
6768
srcs = ["table-printer_test.cc"],
6869
deps = [
70+
":grep-highlighter",
6971
":table-printer",
7072
"//bant:session",
7173
"@googletest//:gtest",

bant/util/grep-highlighter.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ bool GrepHighlighter::EmitMatch(std::string_view content, bool want_all,
133133
return true;
134134
}
135135

136+
// TODO: when we have nested elements inside a colored region, we should
137+
// reset, add colored insert and re-establish that outer color.
138+
136139
out << prefix;
137140
int highlight_depth = 0; // Only when zero, emit the end match.
138141
const char *last_end = content.data();

bant/util/table-printer.cc

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
#include <iterator>
2323
#include <memory>
2424
#include <ostream>
25+
#include <sstream>
2526
#include <string>
2627
#include <vector>
2728

2829
#include "absl/log/check.h"
2930
#include "absl/strings/escaping.h"
3031
#include "absl/strings/str_format.h"
3132
#include "bant/output-format.h"
33+
#include "bant/util/grep-highlighter.h"
3234

3335
namespace bant {
3436
namespace {
@@ -37,8 +39,12 @@ namespace {
3739
class AlignedTextColumnPrinter : public TablePrinter {
3840
public:
3941
AlignedTextColumnPrinter(std::ostream &out,
42+
const GrepHighlighter &highligther,
4043
const std::vector<std::string> &headers)
41-
: out_(out), headers_(headers), widths_(headers.size()) {}
44+
: out_(out),
45+
highligther_(highligther),
46+
headers_(headers),
47+
widths_(headers.size()) {}
4248

4349
void AddRow(const std::vector<std::string> &row) final {
4450
CHECK_EQ(row.size(), widths_.size());
@@ -65,21 +71,25 @@ class AlignedTextColumnPrinter : public TablePrinter {
6571

6672
void Finish() final {
6773
for (const auto &row : buffer_) {
74+
std::stringstream row_out;
6875
for (size_t i = 0; i < widths_.size(); ++i) {
69-
out_ << absl::StrFormat("%*s", -widths_[i] - 1, row[i]);
76+
row_out << absl::StrFormat("%*s", -widths_[i] - 1, row[i]);
7077
}
71-
out_ << "\n";
78+
row_out << "\n";
79+
highligther_.EmitMatch(row_out.str(), true, out_);
7280
}
7381
}
7482

7583
private:
7684
std::ostream &out_;
85+
const GrepHighlighter &highligther_;
7786
const std::vector<std::string> headers_;
7887
// Buffer to keep while determining the print width;
7988
std::vector<int> widths_;
8089
std::vector<std::vector<std::string>> buffer_;
8190
};
8291

92+
// TODO: also implement greph highlighter for these other table printers.
8393
class SExprTablePrinter : public TablePrinter {
8494
public:
8595
SExprTablePrinter(std::ostream &out, const std::vector<std::string> &headers,
@@ -225,7 +235,7 @@ class CSVTablePrinter : public TablePrinter {
225235
} // namespace
226236

227237
std::unique_ptr<TablePrinter> TablePrinter::Create(
228-
std::ostream &out, OutputFormat format,
238+
std::ostream &out, OutputFormat format, const GrepHighlighter &highlighter,
229239
const std::vector<std::string> &headers) {
230240
switch (format) {
231241
case OutputFormat::kSExpr:
@@ -236,7 +246,9 @@ std::unique_ptr<TablePrinter> TablePrinter::Create(
236246
return std::make_unique<JSonTablePrinter>(out, headers);
237247
case OutputFormat::kCSV:
238248
return std::make_unique<CSVTablePrinter>(out, headers);
239-
default: return std::make_unique<AlignedTextColumnPrinter>(out, headers);
249+
default:
250+
return std::make_unique<AlignedTextColumnPrinter>(out, highlighter,
251+
headers);
240252
}
241253
}
242254
} // namespace bant

0 commit comments

Comments
 (0)