Skip to content

Commit 36c1dea

Browse files
committed
[format] json field width should use column width
Related to #1500
1 parent 57acbbe commit 36c1dea

48 files changed

Lines changed: 275 additions & 257 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/base/attr_line.hh

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ rfind_string_attr_if(const string_attrs_t& sa, ssize_t near, T predicate)
153153
auto nearest = sa.end();
154154
ssize_t last_diff = INT_MAX;
155155

156-
157156
for (auto iter = sa.begin(); iter != sa.end(); ++iter) {
158157
const auto& lr = iter->sa_range;
159158

@@ -298,7 +297,11 @@ public:
298297
{
299298
size_t start_len = this->al_string.length();
300299

301-
this->al_string.append(str);
300+
if constexpr (std::is_same_v<S, string_fragment>) {
301+
this->al_string.append(str.data(), str.length());
302+
} else {
303+
this->al_string.append(str);
304+
}
302305

303306
line_range lr{(int) start_len, (int) this->al_string.length()};
304307

@@ -312,7 +315,11 @@ public:
312315
{
313316
size_t start_len = this->al_string.length();
314317

315-
this->al_string.append(std::move(value.first));
318+
if constexpr (std::is_same_v<S, string_fragment>) {
319+
this->al_string.append(value.first.data(), value.first.length());
320+
} else {
321+
this->al_string.append(std::move(value.first));
322+
}
316323

317324
line_range lr{(int) start_len, (int) this->al_string.length()};
318325

src/base/intern_string.hh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,8 @@ public:
820820

821821
const char* get() const { return this->is_str.c_str(); };
822822

823+
const char* data() const { return this->is_str.c_str(); };
824+
823825
size_t size() const { return this->is_str.size(); }
824826

825827
std::string to_string() const { return this->is_str; }
@@ -872,6 +874,8 @@ public:
872874

873875
const char* c_str() const { return this->get(); }
874876

877+
const char* data() const { return this->get(); }
878+
875879
iterator begin() const { return this->get(); }
876880

877881
iterator end() const { return this->get() + this->size(); }

src/bookmarks.cc

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,17 @@ bookmark_type_t::get_all_types()
109109
return all_types;
110110
}
111111

112-
std::vector<const char*>
112+
std::vector<string_fragment>
113113
bookmark_type_t::get_type_names()
114114
{
115-
std::vector<const char*> retval;
115+
std::vector<string_fragment> retval;
116116

117117
for (const auto& bt : get_all_types()) {
118-
retval.emplace_back(bt->get_name().data());
118+
retval.emplace_back(bt->get_name());
119119
}
120120
std::stable_sort(
121-
retval.begin(), retval.end(), [](const char* lhs, const char* rhs) {
122-
return strcmp(lhs, rhs) < 0;
121+
retval.begin(), retval.end(), [](const auto& lhs, const auto& rhs) {
122+
return lhs < rhs;
123123
});
124124
return retval;
125125
}

src/bookmarks.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public:
191191

192192
static std::vector<bookmark_type_t*>& get_all_types();
193193

194-
static std::vector<const char*> get_type_names();
194+
static std::vector<string_fragment> get_type_names();
195195

196196
template<typename T, std::size_t N>
197197
explicit bookmark_type_t(const T (&name)[N])

src/cmds.display.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ static readline_context::command_t DISPLAY_COMMANDS[] = {
164164
.with_summary("Set the display mode for text files")
165165
.with_parameter(help_text{"mode"}
166166
.with_summary("The display mode")
167-
.with_enum_values({"raw", "rendered"}))
167+
.with_enum_values({
168+
"raw"_frag,
169+
"rendered"_frag,
170+
}))
168171
.with_tags({"display"}),
169172
},
170173
{

src/cmds.io.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,7 +1567,7 @@ com_pipe_to(exec_context& ec,
15671567
sql_strftime(tmp_str, sizeof(tmp_str), ldh.ldh_line->get_timeval());
15681568
extra_env["log_time"] = tmp_str;
15691569
extra_env["log_path"] = ldh.ldh_file->get_filename();
1570-
extra_env["log_level"] = ldh.ldh_line->get_level_name();
1570+
extra_env["log_level"] = ldh.ldh_line->get_level_name().to_string();
15711571
if (ldh.ldh_line_values.lvv_opid_value) {
15721572
extra_env["log_opid"] = ldh.ldh_line_values.lvv_opid_value.value();
15731573
}
@@ -1888,7 +1888,7 @@ static readline_context::command_t IO_COMMANDS[] = {
18881888
.with_parameter(
18891889
help_text("--view", "The view to use as the source of data")
18901890
.optional()
1891-
.with_enum_values({"log", "db"}))
1891+
.with_enum_values({"log"_frag, "db"_frag}))
18921892
.with_parameter(
18931893
help_text("--anonymize", "Anonymize the lines").flag())
18941894
.with_parameter(

src/fstat_vtab.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -512,13 +512,13 @@ register_fstat_vtab(sqlite3* db)
512512
.with_result({"st_ino", "The inode number"})
513513
.with_result(help_text{"st_type", "The type of the entry"}
514514
.with_enum_values({
515-
"reg",
516-
"blk",
517-
"chr",
518-
"dir",
519-
"fifo",
520-
"lnk",
521-
"sock",
515+
"reg"_frag,
516+
"blk"_frag,
517+
"chr"_frag,
518+
"dir"_frag,
519+
"fifo"_frag,
520+
"lnk"_frag,
521+
"sock"_frag,
522522
}))
523523
.with_result({"st_mode", "The protection mode"})
524524
.with_result(

src/help_text.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ help_text::is_trailing_arg() const
9191

9292
help_text&
9393
help_text::with_enum_values(
94-
const std::initializer_list<const char*>& enum_values) noexcept
94+
const std::initializer_list<string_fragment>& enum_values) noexcept
9595
{
9696
this->ht_enum_values = enum_values;
9797
return *this;

src/help_text.hh

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
#include <string>
3636
#include <vector>
3737

38+
#include "base/intern_string.hh"
39+
3840
enum class help_context_t {
3941
HC_NONE,
4042
HC_PARAMETER,
@@ -120,7 +122,7 @@ struct help_text {
120122
std::vector<help_example> ht_example;
121123
help_nargs_t ht_nargs{help_nargs_t::HN_REQUIRED};
122124
help_parameter_format_t ht_format{help_parameter_format_t::HPF_STRING};
123-
std::vector<const char*> ht_enum_values;
125+
std::vector<string_fragment> ht_enum_values;
124126
std::vector<const char*> ht_tags;
125127
std::vector<const char*> ht_opposites;
126128
help_function_type_t ht_function_type{help_function_type_t::HFT_REGULAR};
@@ -274,21 +276,21 @@ struct help_text {
274276
bool is_trailing_arg() const;
275277

276278
help_text& with_enum_values(
277-
const std::initializer_list<const char*>& enum_values) noexcept;
279+
const std::initializer_list<string_fragment>& enum_values) noexcept;
278280

279281
template<std::size_t N>
280282
help_text& with_enum_values(
281-
const std::array<const char*, N>& enum_values) noexcept
283+
const std::array<string_fragment, N>& enum_values) noexcept
282284
{
283285
this->ht_enum_values.reserve(N);
284-
for (const auto* val : enum_values) {
286+
for (const auto& val : enum_values) {
285287
this->ht_enum_values.emplace_back(val);
286288
}
287289

288290
return *this;
289291
}
290292

291-
help_text& with_enum_values(const std::vector<const char*>& ev)
293+
help_text& with_enum_values(const std::vector<string_fragment>& ev)
292294
{
293295
this->ht_enum_values = ev;
294296
return *this;

src/help_text_formatter.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ format_help_text_for_term(const help_text& ht,
515515
.append("Values"_h5)
516516
.append(": ");
517517
auto initial = true;
518-
for (const auto* ename : param.ht_enum_values) {
518+
for (const auto& ename : param.ht_enum_values) {
519519
if (!initial) {
520520
alb.append("|");
521521
}

0 commit comments

Comments
 (0)