(content string, args map[string]interface{})
| 1467 | } |
| 1468 | |
| 1469 | func paginateReadFileContent(content string, args map[string]interface{}) string { |
| 1470 | const readFileMaxChars = 50000 |
| 1471 | |
| 1472 | lines := strings.Split(content, "\n") |
| 1473 | total := len(lines) |
| 1474 | if total == 0 { |
| 1475 | return "" |
| 1476 | } |
| 1477 | |
| 1478 | tailLines := parseIntValue(args["tail_lines"], 0) |
| 1479 | offset := parseIntValue(args["offset"], 0) |
| 1480 | limit := parseIntValue(args["limit"], 0) |
| 1481 | |
| 1482 | start := 0 |
| 1483 | end := total |
| 1484 | |
| 1485 | if tailLines > 0 { |
| 1486 | if tailLines < total { |
| 1487 | start = total - tailLines |
| 1488 | } |
| 1489 | } else { |
| 1490 | if offset > 0 { |
| 1491 | if offset >= total { |
| 1492 | return fmt.Sprintf("(offset %d exceeds file length of %d lines)", offset, total) |
| 1493 | } |
| 1494 | start = offset |
| 1495 | } |
| 1496 | if limit > 0 && start+limit < end { |
| 1497 | end = start + limit |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | sliced := lines[start:end] |
| 1502 | output := strings.Join(sliced, "\n") |
| 1503 | if runeLen(output) > readFileMaxChars { |
| 1504 | keptLines := make([]string, 0, len(sliced)) |
| 1505 | usedChars := 0 |
| 1506 | for _, line := range sliced { |
| 1507 | lineChars := runeLen(line) |
| 1508 | additional := lineChars |
| 1509 | if len(keptLines) > 0 { |
| 1510 | additional += 1 // newline between lines |
| 1511 | } |
| 1512 | if usedChars+additional > readFileMaxChars { |
| 1513 | break |
| 1514 | } |
| 1515 | keptLines = append(keptLines, line) |
| 1516 | usedChars += additional |
| 1517 | } |
| 1518 | if len(keptLines) == 0 && len(sliced) > 0 { |
| 1519 | runes := []rune(sliced[0]) |
| 1520 | keep := min(len(runes), readFileMaxChars) |
| 1521 | keptLines = append(keptLines, string(runes[:keep])) |
| 1522 | } |
| 1523 | shownLines := len(keptLines) |
| 1524 | if shownLines <= 0 { |
| 1525 | return fmt.Sprintf("[Output capped at %d chars. File has %d total lines. Use offset=0 limit=<n> to continue reading.]", readFileMaxChars, total) |
| 1526 | } |
no test coverage detected