Skip to content

Commit a5618cb

Browse files
authored
fix(table): do not shrink table with offset (#373)
* test(table): test to show table shrinking with offset * fix(table): do not shrink table height
1 parent 9b809dc commit a5618cb

File tree

2 files changed

+126
-1
lines changed

2 files changed

+126
-1
lines changed

table/table.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,11 @@ func (t *Table) String() string {
371371
topHeight := lipgloss.Height(sb.String()) - 1
372372
availableLines := t.height - (topHeight + lipgloss.Height(bottom))
373373

374+
// if the height is larger than the number of rows, use the number
375+
// of rows.
376+
if availableLines > t.data.Rows() {
377+
availableLines = t.data.Rows()
378+
}
374379
sb.WriteString(t.constructRows(availableLines))
375380

376381
default:
@@ -496,13 +501,19 @@ func (t *Table) constructRows(availableLines int) string {
496501
offsetRowCount := t.data.Rows() - t.offset
497502

498503
// The number of rows to render. We always render at least one row.
499-
rowsToRender := min(availableLines, offsetRowCount)
504+
rowsToRender := availableLines
500505
rowsToRender = max(rowsToRender, 1)
501506

502507
// Check if we need to render an overflow row.
503508
needsOverflow := rowsToRender < offsetRowCount
504509

510+
// only use the offset as the starting value if there is overflow.
505511
rowIdx := t.offset
512+
if !needsOverflow {
513+
// if there is no overflow, just render to the height of the table
514+
// check there's enough content to fill the table
515+
rowIdx = t.data.Rows() - rowsToRender
516+
}
506517
for rowsToRender > 0 && rowIdx < t.data.Rows() {
507518
// Whenever the height is too small to render all rows, the bottom row will be an overflow row (ellipsis).
508519
isOverflow := needsOverflow && rowsToRender == 1

table/table_test.go

+114
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,120 @@ func TestCarriageReturn(t *testing.T) {
12141214
}
12151215
}
12161216

1217+
func TestTableShrinkWithOffset(t *testing.T) {
1218+
rows := [][]string{
1219+
{"1", "Tokyo", "Japan", "37,274,000"},
1220+
{"2", "Delhi", "India", "32,065,760"},
1221+
{"3", "Shanghai", "China", "28,516,904"},
1222+
{"4", "Dhaka", "Bangladesh", "22,478,116"},
1223+
{"5", "São Paulo", "Brazil", "22,429,800"},
1224+
{"6", "Mexico City", "Mexico", "22,085,140"},
1225+
{"7", "Cairo", "Egypt", "21,750,020"},
1226+
{"8", "Beijing", "China", "21,333,332"},
1227+
{"9", "Mumbai", "India", "20,961,472"},
1228+
{"10", "Osaka", "Japan", "19,059,856"},
1229+
{"11", "Chongqing", "China", "16,874,740"},
1230+
{"12", "Karachi", "Pakistan", "16,839,950"},
1231+
{"13", "Istanbul", "Turkey", "15,636,243"},
1232+
{"14", "Kinshasa", "DR Congo", "15,628,085"},
1233+
{"15", "Lagos", "Nigeria", "15,387,639"},
1234+
{"16", "Buenos Aires", "Argentina", "15,369,919"},
1235+
{"17", "Kolkata", "India", "15,133,888"},
1236+
{"18", "Manila", "Philippines", "14,406,059"},
1237+
{"19", "Tianjin", "China", "14,011,828"},
1238+
{"20", "Guangzhou", "China", "13,964,637"},
1239+
{"21", "Rio De Janeiro", "Brazil", "13,634,274"},
1240+
{"22", "Lahore", "Pakistan", "13,541,764"},
1241+
{"23", "Bangalore", "India", "13,193,035"},
1242+
{"24", "Shenzhen", "China", "12,831,330"},
1243+
{"25", "Moscow", "Russia", "12,640,818"},
1244+
{"26", "Chennai", "India", "11,503,293"},
1245+
{"27", "Bogota", "Colombia", "11,344,312"},
1246+
{"28", "Paris", "France", "11,142,303"},
1247+
{"29", "Jakarta", "Indonesia", "11,074,811"},
1248+
{"30", "Lima", "Peru", "11,044,607"},
1249+
{"31", "Bangkok", "Thailand", "10,899,698"},
1250+
{"32", "Hyderabad", "India", "10,534,418"},
1251+
{"33", "Seoul", "South Korea", "9,975,709"},
1252+
{"34", "Nagoya", "Japan", "9,571,596"},
1253+
{"35", "London", "United Kingdom", "9,540,576"},
1254+
{"36", "Chengdu", "China", "9,478,521"},
1255+
{"37", "Nanjing", "China", "9,429,381"},
1256+
{"38", "Tehran", "Iran", "9,381,546"},
1257+
{"39", "Ho Chi Minh City", "Vietnam", "9,077,158"},
1258+
{"40", "Luanda", "Angola", "8,952,496"},
1259+
{"41", "Wuhan", "China", "8,591,611"},
1260+
{"42", "Xi An Shaanxi", "China", "8,537,646"},
1261+
{"43", "Ahmedabad", "India", "8,450,228"},
1262+
{"44", "Kuala Lumpur", "Malaysia", "8,419,566"},
1263+
{"45", "New York City", "United States", "8,177,020"},
1264+
{"46", "Hangzhou", "China", "8,044,878"},
1265+
{"47", "Surat", "India", "7,784,276"},
1266+
{"48", "Suzhou", "China", "7,764,499"},
1267+
{"49", "Hong Kong", "Hong Kong", "7,643,256"},
1268+
{"50", "Riyadh", "Saudi Arabia", "7,538,200"},
1269+
{"51", "Shenyang", "China", "7,527,975"},
1270+
{"52", "Baghdad", "Iraq", "7,511,920"},
1271+
{"53", "Dongguan", "China", "7,511,851"},
1272+
{"54", "Foshan", "China", "7,497,263"},
1273+
{"55", "Dar Es Salaam", "Tanzania", "7,404,689"},
1274+
{"56", "Pune", "India", "6,987,077"},
1275+
{"57", "Santiago", "Chile", "6,856,939"},
1276+
{"58", "Madrid", "Spain", "6,713,557"},
1277+
{"59", "Haerbin", "China", "6,665,951"},
1278+
{"60", "Toronto", "Canada", "6,312,974"},
1279+
{"61", "Belo Horizonte", "Brazil", "6,194,292"},
1280+
{"62", "Khartoum", "Sudan", "6,160,327"},
1281+
{"63", "Johannesburg", "South Africa", "6,065,354"},
1282+
{"64", "Singapore", "Singapore", "6,039,577"},
1283+
{"65", "Dalian", "China", "5,930,140"},
1284+
{"66", "Qingdao", "China", "5,865,232"},
1285+
{"67", "Zhengzhou", "China", "5,690,312"},
1286+
{"68", "Ji Nan Shandong", "China", "5,663,015"},
1287+
{"69", "Barcelona", "Spain", "5,658,472"},
1288+
{"70", "Saint Petersburg", "Russia", "5,535,556"},
1289+
{"71", "Abidjan", "Ivory Coast", "5,515,790"},
1290+
{"72", "Yangon", "Myanmar", "5,514,454"},
1291+
{"73", "Fukuoka", "Japan", "5,502,591"},
1292+
{"74", "Alexandria", "Egypt", "5,483,605"},
1293+
{"75", "Guadalajara", "Mexico", "5,339,583"},
1294+
{"76", "Ankara", "Turkey", "5,309,690"},
1295+
{"77", "Chittagong", "Bangladesh", "5,252,842"},
1296+
{"78", "Addis Ababa", "Ethiopia", "5,227,794"},
1297+
{"79", "Melbourne", "Australia", "5,150,766"},
1298+
{"80", "Nairobi", "Kenya", "5,118,844"},
1299+
{"81", "Hanoi", "Vietnam", "5,067,352"},
1300+
{"82", "Sydney", "Australia", "5,056,571"},
1301+
{"83", "Monterrey", "Mexico", "5,036,535"},
1302+
{"84", "Changsha", "China", "4,809,887"},
1303+
{"85", "Brasilia", "Brazil", "4,803,877"},
1304+
{"86", "Cape Town", "South Africa", "4,800,954"},
1305+
{"87", "Jiddah", "Saudi Arabia", "4,780,740"},
1306+
{"88", "Urumqi", "China", "4,710,203"},
1307+
{"89", "Kunming", "China", "4,657,381"},
1308+
{"90", "Changchun", "China", "4,616,002"},
1309+
{"91", "Hefei", "China", "4,496,456"},
1310+
{"92", "Shantou", "China", "4,490,411"},
1311+
{"93", "Xinbei", "Taiwan", "4,470,672"},
1312+
{"94", "Kabul", "Afghanistan", "4,457,882"},
1313+
{"95", "Ningbo", "China", "4,405,292"},
1314+
{"96", "Tel Aviv", "Israel", "4,343,584"},
1315+
{"97", "Yaounde", "Cameroon", "4,336,670"},
1316+
{"98", "Rome", "Italy", "4,297,877"},
1317+
{"99", "Shijiazhuang", "China", "4,285,135"},
1318+
{"100", "Montreal", "Canada", "4,276,526"},
1319+
}
1320+
table := New().
1321+
Rows(rows...).
1322+
Offset(80).
1323+
Height(45)
1324+
1325+
got := lipgloss.Height(table.String())
1326+
if got != table.height {
1327+
t.Fatalf("expected the height to be %d with an offset of %d. got: table with height %d\n%s", table.height, table.offset, got, table.String())
1328+
}
1329+
}
1330+
12171331
func debug(s string) string {
12181332
return strings.ReplaceAll(s, " ", ".")
12191333
}

0 commit comments

Comments
 (0)