wrap.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2014 Oleku Konko All rights reserved.
  2. // Use of this source code is governed by a MIT
  3. // license that can be found in the LICENSE file.
  4. // This module is a Table Writer API for the Go Programming Language.
  5. // The protocols were written in pure Go and works on windows and unix systems
  6. package tablewriter
  7. import (
  8. "math"
  9. "strings"
  10. "unicode/utf8"
  11. )
  12. var (
  13. nl = "\n"
  14. sp = " "
  15. )
  16. const defaultPenalty = 1e5
  17. // Wrap wraps s into a paragraph of lines of length lim, with minimal
  18. // raggedness.
  19. func WrapString(s string, lim int) ([]string, int) {
  20. words := strings.Split(strings.Replace(s, nl, sp, -1), sp)
  21. var lines []string
  22. max := 0
  23. for _, v := range words {
  24. max = len(v)
  25. if max > lim {
  26. lim = max
  27. }
  28. }
  29. for _, line := range WrapWords(words, 1, lim, defaultPenalty) {
  30. lines = append(lines, strings.Join(line, sp))
  31. }
  32. return lines, lim
  33. }
  34. // WrapWords is the low-level line-breaking algorithm, useful if you need more
  35. // control over the details of the text wrapping process. For most uses,
  36. // WrapString will be sufficient and more convenient.
  37. //
  38. // WrapWords splits a list of words into lines with minimal "raggedness",
  39. // treating each rune as one unit, accounting for spc units between adjacent
  40. // words on each line, and attempting to limit lines to lim units. Raggedness
  41. // is the total error over all lines, where error is the square of the
  42. // difference of the length of the line and lim. Too-long lines (which only
  43. // happen when a single word is longer than lim units) have pen penalty units
  44. // added to the error.
  45. func WrapWords(words []string, spc, lim, pen int) [][]string {
  46. n := len(words)
  47. length := make([][]int, n)
  48. for i := 0; i < n; i++ {
  49. length[i] = make([]int, n)
  50. length[i][i] = utf8.RuneCountInString(words[i])
  51. for j := i + 1; j < n; j++ {
  52. length[i][j] = length[i][j-1] + spc + utf8.RuneCountInString(words[j])
  53. }
  54. }
  55. nbrk := make([]int, n)
  56. cost := make([]int, n)
  57. for i := range cost {
  58. cost[i] = math.MaxInt32
  59. }
  60. for i := n - 1; i >= 0; i-- {
  61. if length[i][n-1] <= lim {
  62. cost[i] = 0
  63. nbrk[i] = n
  64. } else {
  65. for j := i + 1; j < n; j++ {
  66. d := lim - length[i][j-1]
  67. c := d*d + cost[j]
  68. if length[i][j-1] > lim {
  69. c += pen // too-long lines get a worse penalty
  70. }
  71. if c < cost[i] {
  72. cost[i] = c
  73. nbrk[i] = j
  74. }
  75. }
  76. }
  77. }
  78. var lines [][]string
  79. i := 0
  80. for i < n {
  81. lines = append(lines, words[i:nbrk[i]])
  82. i = nbrk[i]
  83. }
  84. return lines
  85. }
  86. // getLines decomposes a multiline string into a slice of strings.
  87. func getLines(s string) []string {
  88. var lines []string
  89. for _, line := range strings.Split(s, nl) {
  90. lines = append(lines, line)
  91. }
  92. return lines
  93. }