Skip to content

Commit 142ef0d

Browse files
committed
[nits] fixing some minor bugs
1 parent 41dfb1c commit 142ef0d

9 files changed

Lines changed: 101 additions & 29 deletions

File tree

src/base/ansi_scrubber.cc

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ erase_ansi_escapes(string_fragment input)
7373
auto sf = md[0].value();
7474

7575
if (sf == "\x00"_frag) {
76-
*input.writable_data(0) = ' ';
76+
*input.writable_data(fill_index) = ' ';
7777
move_start = sf.sf_end;
7878
fill_index += 1;
7979
continue;
@@ -169,7 +169,7 @@ scrub_ansi_string(std::string& str, string_attrs_t* sa)
169169
if (sf == "\x00"_frag) {
170170
str[cp_dst] = ' ';
171171
cp_start = sf.sf_end;
172-
cp_dst = sf.sf_end;
172+
cp_dst += 1;
173173
continue;
174174
}
175175

@@ -365,7 +365,14 @@ scrub_ansi_string(std::string& str, string_attrs_t* sa)
365365
if ((sep1 == ';' && sep2 == ';')
366366
|| (sep1 == ':' && sep2 == ':'))
367367
{
368-
attrs.ta_fg_color = rgb_color{r, g, b};
368+
if (ansi_code == 38) {
369+
attrs.ta_fg_color
370+
= rgb_color{r, g, b};
371+
} else {
372+
attrs.ta_bg_color
373+
= rgb_color{r, g, b};
374+
}
375+
seq = color_code_pair->second;
369376
}
370377
}
371378
} else if (color_type->value() == 5) {

src/base/auto_fd.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ auto_fd::write_fully(string_fragment sf)
171171
auto rc = write(this->af_fd, sf.data(), sf.length());
172172

173173
if (rc < 0) {
174+
if (errno == EINTR) {
175+
continue;
176+
}
174177
return Err(
175178
fmt::format(FMT_STRING("failed to write {} bytes to FD {}"),
176179
sf.length(),
@@ -236,6 +239,8 @@ auto_pipe::after_fork(pid_t child_pid)
236239
if (new_fd != this->ap_child_fd) {
237240
dup2(new_fd, this->ap_child_fd);
238241
this->close();
242+
} else {
243+
fcntl(new_fd, F_SETFD, 0);
239244
}
240245
}
241246
break;

src/base/fs_util.cc

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -453,21 +453,21 @@ write_file(const std::filesystem::path& path,
453453
auto tmp_pair = TRY(open_temp_file(tmp_pattern));
454454
auto for_res = content.for_each(
455455
[&tmp_pair](string_fragment sf) -> Result<void, std::string> {
456-
auto bytes_written
457-
= write(tmp_pair.second.get(), sf.data(), sf.length());
458-
if (bytes_written < 0) {
459-
return Err(fmt::format(
460-
FMT_STRING("unable to write to temporary file {}: {}"),
461-
tmp_pair.first.string(),
462-
lnav::from_errno()));
463-
}
456+
while (!sf.empty()) {
457+
auto bytes_written
458+
= write(tmp_pair.second.get(), sf.data(), sf.length());
459+
if (bytes_written < 0) {
460+
if (errno == EINTR) {
461+
continue;
462+
}
463+
return Err(fmt::format(
464+
FMT_STRING(
465+
"unable to write to temporary file {}: {}"),
466+
tmp_pair.first.string(),
467+
lnav::from_errno()));
468+
}
464469

465-
if (bytes_written != sf.length()) {
466-
return Err(
467-
fmt::format(FMT_STRING("short write to file {}: {} < {}"),
468-
tmp_pair.first.string(),
469-
bytes_written,
470-
sf.length()));
470+
sf = sf.substr(bytes_written);
471471
}
472472

473473
return Ok();
@@ -568,8 +568,8 @@ formatter<std::filesystem::path>::format(const std::filesystem::path& p,
568568
format_context& ctx)
569569
-> decltype(ctx.out()) const
570570
{
571-
auto esc_res = fmt::v10::detail::find_escape(&(*p.native().begin()),
572-
&(*p.native().end()));
571+
auto esc_res = fmt::v10::detail::find_escape(
572+
p.native().data(), p.native().data() + p.native().size());
573573
if (esc_res.end == nullptr) {
574574
return formatter<string_view>::format(p.native(), ctx);
575575
}

src/base/humanize.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ try_from(const string_fragment& sf)
7575
if (md[real]) {
7676
auto scan_res = scn::scan_value<double>(md[real]->to_string_view());
7777

78+
if (!scan_res) {
79+
return std::nullopt;
80+
}
7881
return scan_res->value();
7982
}
8083

@@ -187,7 +190,7 @@ file_size(file_ssize_t value, alignment align)
187190
const auto divisor = pow(1024, exp);
188191

189192
if (align == alignment::none && divisor <= 1) {
190-
return fmt::format(FMT_STRING("{}B"), value, UNITS[exp]);
193+
return fmt::format(FMT_STRING("{}B"), value);
191194
}
192195
return fmt::format(FMT_STRING("{:.1f}{}B"),
193196
divisor == 0 ? value : value / divisor,

src/base/lnav.gzip.cc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ compress(const void* input, size_t len)
6464
}
6565
rc = deflate(&zs, Z_FINISH);
6666
if (rc != Z_STREAM_END) {
67+
deflateEnd(&zs);
6768
return Err(fmt::format(FMT_STRING("unable to compress data -- {}"),
6869
zError(rc)));
6970
}
@@ -113,17 +114,18 @@ uncompress(const std::string& src, const void* buffer, size_t size)
113114
}
114115
}
115116

116-
if (inflateEnd(&strm) != Z_OK) {
117-
return Err(fmt::format(FMT_STRING("unable to uncompress: {} -- {}"),
118-
src,
119-
strm.msg ? strm.msg : zError(err)));
117+
err = inflateEnd(&strm);
118+
if (err != Z_OK) {
119+
return Err(fmt::format(
120+
FMT_STRING("unable to uncompress: {} -- {}"), src, zError(err)));
120121
}
121122

122123
return Ok(std::move(uncomp.resize(strm.total_out)));
123124
}
124125

125126
struct gunzip_producer : string_fragment_producer {
126-
explicit gunzip_producer(const string_fragment& src, size_t uncompressed_size)
127+
explicit gunzip_producer(const string_fragment& src,
128+
size_t uncompressed_size)
127129
: gp_src(src.to_string()), gp_uncompressed_size(uncompressed_size)
128130
{
129131
}

src/base/small_string_map.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct small_string_map {
4343
char ssm_keys[MAP_SIZE * MAX_KEY_SIZE]{};
4444
uint32_t ssm_values[MAP_SIZE]{};
4545
uint32_t ssm_start_index{0};
46-
bool ssm_age[MAP_SIZE];
46+
bool ssm_age[MAP_SIZE]{};
4747

4848
std::optional<uint32_t> lookup(const string_fragment& in);
4949
void insert(const string_fragment& key, uint32_t value);

src/base/string_util.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -420,8 +420,8 @@ formatter<lnav::tainted_string>::format(const lnav::tainted_string& ts,
420420
format_context& ctx)
421421
-> decltype(ctx.out()) const
422422
{
423-
auto esc_res = fmt::v10::detail::find_escape(&(*ts.ts_str.begin()),
424-
&(*ts.ts_str.end()));
423+
auto esc_res = fmt::v10::detail::find_escape(
424+
ts.ts_str.data(), ts.ts_str.data() + ts.ts_str.size());
425425
if (esc_res.end == nullptr) {
426426
return formatter<string_view>::format(ts.ts_str, ctx);
427427
}

src/sql.formatter.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,10 @@ end_close_scope(std::vector<std::string>& scope_stack)
146146
}
147147

148148
scope_stack.pop_back();
149-
return (scope_stack.back() == "CASE"_frag);
149+
if (scope_stack.empty()) {
150+
return false;
151+
}
152+
return scope_stack.back() == "CASE"_frag;
150153
}
151154

152155
struct keyword_attrs {

test/test_ansi_scrubber.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,40 @@ main(int argc, char* argv[])
7676
assert(" Hello" == input);
7777
}
7878

79+
{
80+
auto input = std::string("AB\x1b[33m\0CD", 10);
81+
string_attrs_t sa;
82+
83+
scrub_ansi_string(input, &sa);
84+
assert(input == "AB CD");
85+
}
86+
87+
{
88+
auto input = std::string("\x1b[33m\0Hello", 11);
89+
string_attrs_t sa;
90+
91+
scrub_ansi_string(input, &sa);
92+
assert(input == " Hello");
93+
}
94+
95+
{
96+
char input[] = "AB\x1b[33m\0CD";
97+
auto sf = string_fragment{input, 0, 10};
98+
99+
auto new_len = erase_ansi_escapes(sf);
100+
auto result = std::string(input, new_len);
101+
assert(result == "AB CD");
102+
}
103+
104+
{
105+
char input[] = "\x1b[33m\0Hello";
106+
auto sf = string_fragment{input, 0, 11};
107+
108+
auto new_len = erase_ansi_escapes(sf);
109+
auto result = std::string(input, new_len);
110+
assert(result == " Hello");
111+
}
112+
79113
{
80114
auto input = std::string("\x1b[0;1;38:2:1:2:3mHello");
81115
string_attrs_t sa;
@@ -91,6 +125,24 @@ main(int argc, char* argv[])
91125
assert(rgb->rc_b == 3);
92126
}
93127

128+
{
129+
auto input = std::string("\x1b[0;48:2:10:20:30mHello");
130+
string_attrs_t sa;
131+
132+
scrub_ansi_string(input, &sa);
133+
assert(input == "Hello");
134+
assert(sa.size() == 1);
135+
assert(sa[0].sa_type == &VC_STYLE);
136+
auto ta = sa[0].sa_value.get<text_attrs>();
137+
auto rgb = std::get_if<rgb_color>(&ta.ta_bg_color.cu_value);
138+
assert(rgb != nullptr);
139+
assert(sa[0].sa_range.lr_start == 0);
140+
assert(sa[0].sa_range.lr_end == -1);
141+
assert(rgb->rc_r == 10);
142+
assert(rgb->rc_g == 20);
143+
assert(rgb->rc_b == 30);
144+
}
145+
94146
{
95147
auto input = std::string("\x1b[0;1;38:5:245mHello");
96148
string_attrs_t sa;

0 commit comments

Comments
 (0)