Skip to content

Commit 1799c2c

Browse files
committed
[attr_line] fix some text wrapping issues
1 parent aba5e27 commit 1799c2c

9 files changed

+245
-218
lines changed

src/base/attr_line.cc

+35-29
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,8 @@ consume(const string_fragment text)
244244
static const auto SPACE_RE
245245
= lnav::pcre2pp::code::from_const(R"((*UTF)^\s)");
246246

247+
require(text.is_valid());
248+
247249
if (text.empty()) {
248250
return eof{text};
249251
}
@@ -270,16 +272,8 @@ consume(const string_fragment text)
270272
return space{split_res.first, split_res.second};
271273
}
272274

273-
auto csize_res = ww898::utf::utf8::char_size(
274-
[&text]() { return std::make_pair(text.front(), text.length()); });
275-
276-
if (csize_res.isErr()) {
277-
auto split_res = text.split_n(1);
278-
279-
return corrupt{split_res->first, split_res->second};
280-
}
281-
282-
auto split_res = text.split_n(csize_res.unwrap());
275+
auto next_char_byte_index = text.column_to_byte_index(1);
276+
auto split_res = text.split_n(next_char_byte_index);
283277

284278
return word{split_res->first, split_res->second};
285279
}
@@ -314,6 +308,8 @@ attr_line_t::insert(size_t index,
314308
const attr_line_t& al,
315309
text_wrap_settings* tws)
316310
{
311+
require(!tws || tws->tws_width > 0);
312+
317313
if (index < this->al_string.length()) {
318314
shift_string_attrs(this->al_attrs, index, al.al_string.length());
319315
}
@@ -323,7 +319,7 @@ attr_line_t::insert(size_t index,
323319
for (const auto& sa : al.al_attrs) {
324320
this->al_attrs.emplace_back(sa);
325321

326-
line_range& lr = this->al_attrs.back().sa_range;
322+
auto& lr = this->al_attrs.back().sa_range;
327323

328324
lr.shift(0, index);
329325
if (lr.lr_end == -1) {
@@ -335,7 +331,8 @@ attr_line_t::insert(size_t index,
335331
return *this;
336332
}
337333

338-
auto starting_line_index = this->al_string.rfind('\n', index);
334+
auto starting_line_index = index == 0 ? std::string::npos
335+
: this->al_string.rfind('\n', index - 1);
339336
if (starting_line_index == std::string::npos) {
340337
starting_line_index = 0;
341338
} else {
@@ -378,12 +375,20 @@ attr_line_t::insert(size_t index,
378375
auto pre_iter = find_string_attr_containing(
379376
this->al_attrs, &SA_PREFORMATTED, text_to_wrap.sf_begin);
380377
if (pre_iter != this->al_attrs.end()) {
378+
require_ge(pre_iter->sa_range.lr_start, text_to_wrap.sf_begin);
381379
auto pre_len = pre_iter->sa_range.lr_end - text_to_wrap.sf_begin;
382380
auto pre_lf = text_to_wrap.find('\n');
383-
if (pre_lf && pre_lf.value() < pre_len) {
381+
if (pre_lf && pre_lf.value() + 1 < pre_len) {
384382
pre_len = pre_lf.value() + 1;
383+
auto lr_copy = pre_iter->sa_range;
384+
const_cast<int&>(pre_iter->sa_range.lr_end)
385+
= pre_iter->sa_range.lr_start + pre_len;
386+
this->al_attrs.emplace_back(lr_copy, SA_PREFORMATTED.value());
387+
this->al_attrs.back().sa_range.lr_start
388+
= lr_copy.lr_start + pre_len;
385389
}
386390

391+
require_ge(text_to_wrap.length(), pre_len);
387392
auto pre_pair = text_to_wrap.split_n(pre_len);
388393
next_chunk = text_stream::word{
389394
pre_pair->first,
@@ -395,9 +400,8 @@ attr_line_t::insert(size_t index,
395400
}
396401

397402
text_to_wrap = next_chunk.match(
398-
[&](text_stream::word word) {
399-
auto ch_count
400-
= word.w_word.utf8_length().unwrapOr(word.w_word.length());
403+
[&](const text_stream::word& word) {
404+
auto ch_count = word.w_word.column_width();
401405

402406
if (line_ch_count > line_indent_count && !last_was_pre
403407
&& (line_ch_count + ch_count) > usable_width)
@@ -421,8 +425,8 @@ attr_line_t::insert(size_t index,
421425
auto trailing_space_count = 0;
422426
if (!last_word.empty()) {
423427
trailing_space_count
424-
= word.w_word.sf_begin - last_word.sf_begin;
425-
this->erase(last_word.sf_begin, trailing_space_count);
428+
= word.w_word.sf_begin - last_word.sf_end;
429+
this->erase(last_word.sf_end, trailing_space_count);
426430
}
427431
return word.w_remaining
428432
.erase_before(this->al_string.data(),
@@ -439,7 +443,7 @@ attr_line_t::insert(size_t index,
439443

440444
return word.w_remaining;
441445
},
442-
[&](text_stream::space space) {
446+
[&](const text_stream::space& space) {
443447
if (space.s_value == "\n") {
444448
line_ch_count = 0;
445449
line_indent_count = 0;
@@ -448,8 +452,7 @@ attr_line_t::insert(size_t index,
448452
}
449453

450454
if (line_ch_count > 0) {
451-
auto ch_count = space.s_value.utf8_length().unwrapOr(
452-
space.s_value.length());
455+
auto ch_count = space.s_value.column_width();
453456

454457
if ((line_ch_count + ch_count) > usable_width
455458
&& find_string_attr_containing(this->al_attrs,
@@ -467,15 +470,18 @@ attr_line_t::insert(size_t index,
467470
auto trailing_space_count = 0;
468471
if (!last_word.empty()) {
469472
trailing_space_count
470-
= space.s_value.sf_begin - last_word.sf_begin;
473+
= space.s_value.sf_begin - last_word.sf_end;
471474
this->erase(last_word.sf_end, trailing_space_count);
472475
}
473-
474-
return space.s_remaining
475-
.erase_before(
476-
this->al_string.data(),
477-
space.s_value.length() + trailing_space_count)
478-
.prepend(this->al_string.data(), 1);
476+
last_word.clear();
477+
478+
auto retval
479+
= space.s_remaining
480+
.erase_before(this->al_string.data(),
481+
space.s_value.length()
482+
+ trailing_space_count)
483+
.prepend(this->al_string.data(), 1);
484+
return retval;
479485
}
480486
line_ch_count += ch_count;
481487
} else if (find_string_attr_containing(this->al_attrs,
@@ -494,7 +500,7 @@ attr_line_t::insert(size_t index,
494500
[](text_stream::eof eof) { return eof.e_remaining; });
495501

496502
if (next_chunk.is<text_stream::word>()) {
497-
last_word = text_to_wrap;
503+
last_word = next_chunk.get<text_stream::word>().w_word;
498504
}
499505
last_was_pre = (pre_iter != this->al_attrs.end());
500506

src/lnav.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3368,7 +3368,7 @@ SELECT tbl_name FROM sqlite_master WHERE sql LIKE 'CREATE VIRTUAL TABLE%'
33683368

33693369
for (const auto& file_path_str : file_args) {
33703370
auto file_path_without_trailer = file_path_str;
3371-
auto file_loc = file_location_t{file_location_tail{}};
3371+
auto file_loc = file_location_t{mapbox::util::no_init{}};
33723372
auto_mem<char> abspath;
33733373
struct stat st;
33743374

src/lnav.indexing.cc

+14-2
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,20 @@ rebuild_indexes(std::optional<ui_clock::time_point> deadline)
254254
std::optional<vis_line_t> new_top_opt;
255255
if (cb.front_top.valid()) {
256256
cb.front_top.match(
257-
[](file_location_tail tail) {
258-
log_info("file open request to tail");
257+
[&new_top_opt, &cb](file_location_tail tail) {
258+
switch (cb.front_file->get_text_format()) {
259+
case text_format_t::TF_UNKNOWN:
260+
case text_format_t::TF_LOG:
261+
log_info("file open request to tail");
262+
break;
263+
default:
264+
log_info("file open is %s, moving to top",
265+
fmt::to_string(
266+
cb.front_file->get_text_format())
267+
.c_str());
268+
new_top_opt = 0_vl;
269+
break;
270+
}
259271
},
260272
[&new_top_opt](vis_line_t vl) {
261273
log_info("file open request to jump to line: %d",

src/md2attr_line.cc

+7-4
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,11 @@ md2attr_line::leave_block(const md4cpp::event_handler::block& bl)
197197
last_block.append(block_text);
198198
} else if (bl.is<MD_BLOCK_LI_DETAIL*>()) {
199199
auto last_list_block = this->ml_list_stack.back();
200-
auto li_detail = bl.get<MD_BLOCK_LI_DETAIL*>();
201-
text_wrap_settings tws = {0, 60};
200+
const auto* li_detail = bl.get<MD_BLOCK_LI_DETAIL*>();
201+
auto tws = text_wrap_settings{
202+
0,
203+
63 - (int) (this->ml_list_stack.size() * 3),
204+
};
202205

203206
attr_line_builder alb(last_block);
204207
{
@@ -324,7 +327,7 @@ md2attr_line::leave_block(const md4cpp::event_handler::block& bl)
324327
for (auto& line : code_lines) {
325328
line.pad_to(std::max(max_width + 4, size_t{40}))
326329
.with_attr_for_all(VC_ROLE.value(role_t::VCR_QUOTED_CODE));
327-
padded_text.append(lnav::string::attrs::preformatted(" "))
330+
padded_text.append(" ")
328331
.append("\u258c"_code_border)
329332
.append(line)
330333
.append("\n");
@@ -384,7 +387,7 @@ md2attr_line::leave_block(const md4cpp::event_handler::block& bl)
384387
auto quoted_lines = wrapped_text.split_lines();
385388
auto max_width = quoted_lines
386389
| lnav::itertools::map(&attr_line_t::column_width)
387-
| lnav::itertools::max(0);
390+
| lnav::itertools::max(tws.tws_width);
388391
attr_line_t padded_text;
389392

390393
for (auto& line : quoted_lines) {

src/themes/dracula.json

+16-14
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
"dracula": {
66
"vars": {
77
"background": "#282A36",
8+
"bglighter": "#424450",
89
"bglight": "#343746",
910
"bgdark": "#21222C",
11+
"bgdarker": "#191A21",
1012
"foreground": "#F8F8F2",
1113
"selection": "#44475A",
1214
"black": "#21222C",
@@ -27,19 +29,19 @@
2729
"color": "semantic()"
2830
},
2931
"text": {
30-
"color": "#F8F8F2",
32+
"color": "$foreground",
3133
"background-color": "$background"
3234
},
3335
"selected-text": {
34-
"background-color": "$foreground"
36+
"background-color": "$bgdarker"
3537
},
3638
"fuzzy-match": {
3739
"color": "$orange",
3840
"underline": true,
3941
"bold": true
4042
},
4143
"alt-text": {
42-
"background-color": "#1c1c1c"
44+
"background-color": "$bgdarker"
4345
},
4446
"ok": {
4547
"color": "$green",
@@ -98,14 +100,14 @@
98100
},
99101
"popup": {
100102
"color": "$foreground",
101-
"background-color": "$bglight"
103+
"background-color": "$bgdarker"
102104
},
103105
"popup-border": {
104-
"background-color": "$bgdark"
106+
"background-color": "$bgdarker"
105107
},
106108
"scrollbar": {
107-
"color": "$black",
108-
"background-color": "#888"
109+
"color": "$background",
110+
"background-color": "$foreground"
109111
},
110112
"h1": {
111113
"color": "$purple",
@@ -148,25 +150,25 @@
148150
},
149151
"quote-border": {
150152
"color": "#666",
151-
"background-color": "#444"
153+
"background-color": "$bgdarker"
152154
},
153155
"quoted-text": {
154156
"color": "$yellow",
155-
"background-color": "#444"
157+
"background-color": "$bgdarker"
156158
},
157159
"footnote-border": {
158160
"color": "$blue",
159-
"background-color": "#444"
161+
"background-color": "$bgdarker"
160162
},
161163
"footnote-text": {
162164
"color": "$cyan",
163-
"background-color": "#444"
165+
"background-color": "$bgdarker"
164166
},
165167
"snippet-border": {
166168
"color": "$cyan"
167169
},
168170
"indent-guide": {
169-
"color": "#444"
171+
"color": "$bglighter"
170172
}
171173
},
172174
"syntax-styles": {
@@ -285,7 +287,7 @@
285287
"background-color": "#353535"
286288
},
287289
"warn": {
288-
"color": "$yellow",
290+
"color": "$orange",
289291
"background-color": "#353535"
290292
},
291293
"alert": {
@@ -310,7 +312,7 @@
310312
},
311313
"log-level-styles": {
312314
"warning": {
313-
"color": "$yellow"
315+
"color": "$orange"
314316
},
315317
"error": {
316318
"color": "$red"

0 commit comments

Comments
 (0)