MCPcopy Create free account
hub / github.com/duckdb/duckdb / Truncate

Method Truncate

tools/shell/shell_render_table_metadata.cpp:349–397  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

347}
348
349void ShellTableRenderInfo::Truncate(idx_t max_render_width) {
350 if (render_width <= max_render_width) {
351 return;
352 }
353
354 // we exceeded the render width - we need to truncate
355 TruncateValueIfRequired(table.table_name, table_name_length, max_render_width - 4);
356 // figure out what we need to truncate
357 idx_t total_column_length = PerColumnWidth();
358 if (total_column_length > max_render_width) {
359 // we need to truncate either column names or column types
360 // prefer to keep the name as long as possible - only truncate it if it by itself almost exceeds the
361 // width
362 static constexpr const idx_t MIN_COMPONENT_SIZE = 5;
363 idx_t component_count = max_component_widths.size();
364 idx_t min_leftover_size = component_count * MIN_COMPONENT_SIZE;
365 if (max_component_widths[0] + min_leftover_size > max_render_width) {
366 max_component_widths[0] = max_render_width - min_leftover_size;
367 }
368 total_column_length = PerColumnWidth();
369 if (total_column_length > max_render_width) {
370 // truncate other components if required
371 for (idx_t i = 1; i < max_component_widths.size(); i++) {
372 if (max_component_widths[i] <= MIN_COMPONENT_SIZE) {
373 // cannot truncate below the min component size
374 continue;
375 }
376 idx_t truncate_amount = duckdb::MinValue<idx_t>(total_column_length - max_render_width,
377 max_component_widths[i] - MIN_COMPONENT_SIZE);
378 max_component_widths[i] -= truncate_amount;
379 total_column_length -= truncate_amount;
380 if (total_column_length <= render_width) {
381 break;
382 }
383 }
384 }
385 // truncate all column components that we need to truncate
386 for (auto &column_render : column_renders) {
387 for (auto &col : column_render.columns) {
388 for (idx_t component_idx = 0; component_idx < col.components.size(); component_idx++) {
389 auto &component = col.components[component_idx];
390 TruncateValueIfRequired(component.text, component.render_width,
391 max_component_widths[component_idx]);
392 }
393 }
394 }
395 }
396 render_width = max_render_width;
397}
398
399void RenderLineDisplay(ShellHighlight &highlight, string text, idx_t total_render_width,
400 HighlightElementType element_type) {

Callers 1

RenderTableMetadataMethod · 0.45

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected