wrap.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. package ansi
  2. import (
  3. "bytes"
  4. "unicode"
  5. "unicode/utf8"
  6. "github.com/charmbracelet/x/ansi/parser"
  7. "github.com/rivo/uniseg"
  8. )
  9. // nbsp is a non-breaking space
  10. const nbsp = 0xA0
  11. // Hardwrap wraps a string or a block of text to a given line length, breaking
  12. // word boundaries. This will preserve ANSI escape codes and will account for
  13. // wide-characters in the string.
  14. // When preserveSpace is true, spaces at the beginning of a line will be
  15. // preserved.
  16. func Hardwrap(s string, limit int, preserveSpace bool) string {
  17. if limit < 1 {
  18. return s
  19. }
  20. var (
  21. cluster []byte
  22. buf bytes.Buffer
  23. curWidth int
  24. forceNewline bool
  25. pstate = parser.GroundState // initial state
  26. b = []byte(s)
  27. )
  28. addNewline := func() {
  29. buf.WriteByte('\n')
  30. curWidth = 0
  31. }
  32. i := 0
  33. for i < len(b) {
  34. state, action := parser.Table.Transition(pstate, b[i])
  35. if state == parser.Utf8State {
  36. var width int
  37. cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1)
  38. i += len(cluster)
  39. if curWidth+width > limit {
  40. addNewline()
  41. }
  42. if !preserveSpace && curWidth == 0 && len(cluster) <= 4 {
  43. // Skip spaces at the beginning of a line
  44. if r, _ := utf8.DecodeRune(cluster); r != utf8.RuneError && unicode.IsSpace(r) {
  45. pstate = parser.GroundState
  46. continue
  47. }
  48. }
  49. buf.Write(cluster)
  50. curWidth += width
  51. pstate = parser.GroundState
  52. continue
  53. }
  54. switch action {
  55. case parser.PrintAction, parser.ExecuteAction:
  56. if b[i] == '\n' {
  57. addNewline()
  58. forceNewline = false
  59. break
  60. }
  61. if curWidth+1 > limit {
  62. addNewline()
  63. forceNewline = true
  64. }
  65. // Skip spaces at the beginning of a line
  66. if curWidth == 0 {
  67. if !preserveSpace && forceNewline && unicode.IsSpace(rune(b[i])) {
  68. break
  69. }
  70. forceNewline = false
  71. }
  72. buf.WriteByte(b[i])
  73. if action == parser.PrintAction {
  74. curWidth++
  75. }
  76. default:
  77. buf.WriteByte(b[i])
  78. }
  79. // We manage the UTF8 state separately manually above.
  80. if pstate != parser.Utf8State {
  81. pstate = state
  82. }
  83. i++
  84. }
  85. return buf.String()
  86. }
  87. // Wordwrap wraps a string or a block of text to a given line length, not
  88. // breaking word boundaries. This will preserve ANSI escape codes and will
  89. // account for wide-characters in the string.
  90. // The breakpoints string is a list of characters that are considered
  91. // breakpoints for word wrapping. A hyphen (-) is always considered a
  92. // breakpoint.
  93. //
  94. // Note: breakpoints must be a string of 1-cell wide rune characters.
  95. func Wordwrap(s string, limit int, breakpoints string) string {
  96. if limit < 1 {
  97. return s
  98. }
  99. var (
  100. cluster []byte
  101. buf bytes.Buffer
  102. word bytes.Buffer
  103. space bytes.Buffer
  104. curWidth int
  105. wordLen int
  106. pstate = parser.GroundState // initial state
  107. b = []byte(s)
  108. )
  109. addSpace := func() {
  110. curWidth += space.Len()
  111. buf.Write(space.Bytes())
  112. space.Reset()
  113. }
  114. addWord := func() {
  115. if word.Len() == 0 {
  116. return
  117. }
  118. addSpace()
  119. curWidth += wordLen
  120. buf.Write(word.Bytes())
  121. word.Reset()
  122. wordLen = 0
  123. }
  124. addNewline := func() {
  125. buf.WriteByte('\n')
  126. curWidth = 0
  127. space.Reset()
  128. }
  129. i := 0
  130. for i < len(b) {
  131. state, action := parser.Table.Transition(pstate, b[i])
  132. if state == parser.Utf8State {
  133. var width int
  134. cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1)
  135. i += len(cluster)
  136. r, _ := utf8.DecodeRune(cluster)
  137. if r != utf8.RuneError && unicode.IsSpace(r) && r != nbsp {
  138. addWord()
  139. space.WriteRune(r)
  140. } else if bytes.ContainsAny(cluster, breakpoints) {
  141. addSpace()
  142. addWord()
  143. buf.Write(cluster)
  144. curWidth++
  145. } else {
  146. word.Write(cluster)
  147. wordLen += width
  148. if curWidth+space.Len()+wordLen > limit &&
  149. wordLen < limit {
  150. addNewline()
  151. }
  152. }
  153. pstate = parser.GroundState
  154. continue
  155. }
  156. switch action {
  157. case parser.PrintAction, parser.ExecuteAction:
  158. r := rune(b[i])
  159. switch {
  160. case r == '\n':
  161. if wordLen == 0 {
  162. if curWidth+space.Len() > limit {
  163. curWidth = 0
  164. } else {
  165. buf.Write(space.Bytes())
  166. }
  167. space.Reset()
  168. }
  169. addWord()
  170. addNewline()
  171. case unicode.IsSpace(r):
  172. addWord()
  173. space.WriteByte(b[i])
  174. case r == '-':
  175. fallthrough
  176. case runeContainsAny(r, breakpoints):
  177. addSpace()
  178. addWord()
  179. buf.WriteByte(b[i])
  180. curWidth++
  181. default:
  182. word.WriteByte(b[i])
  183. wordLen++
  184. if curWidth+space.Len()+wordLen > limit &&
  185. wordLen < limit {
  186. addNewline()
  187. }
  188. }
  189. default:
  190. word.WriteByte(b[i])
  191. }
  192. // We manage the UTF8 state separately manually above.
  193. if pstate != parser.Utf8State {
  194. pstate = state
  195. }
  196. i++
  197. }
  198. addWord()
  199. return buf.String()
  200. }
  201. // Wrap wraps a string or a block of text to a given line length, breaking word
  202. // boundaries if necessary. This will preserve ANSI escape codes and will
  203. // account for wide-characters in the string. The breakpoints string is a list
  204. // of characters that are considered breakpoints for word wrapping. A hyphen
  205. // (-) is always considered a breakpoint.
  206. //
  207. // Note: breakpoints must be a string of 1-cell wide rune characters.
  208. func Wrap(s string, limit int, breakpoints string) string {
  209. if limit < 1 {
  210. return s
  211. }
  212. var (
  213. cluster []byte
  214. buf bytes.Buffer
  215. word bytes.Buffer
  216. space bytes.Buffer
  217. curWidth int // written width of the line
  218. wordLen int // word buffer len without ANSI escape codes
  219. pstate = parser.GroundState // initial state
  220. b = []byte(s)
  221. )
  222. addSpace := func() {
  223. curWidth += space.Len()
  224. buf.Write(space.Bytes())
  225. space.Reset()
  226. }
  227. addWord := func() {
  228. if word.Len() == 0 {
  229. return
  230. }
  231. addSpace()
  232. curWidth += wordLen
  233. buf.Write(word.Bytes())
  234. word.Reset()
  235. wordLen = 0
  236. }
  237. addNewline := func() {
  238. buf.WriteByte('\n')
  239. curWidth = 0
  240. space.Reset()
  241. }
  242. i := 0
  243. for i < len(b) {
  244. state, action := parser.Table.Transition(pstate, b[i])
  245. if state == parser.Utf8State {
  246. var width int
  247. cluster, _, width, _ = uniseg.FirstGraphemeCluster(b[i:], -1)
  248. i += len(cluster)
  249. r, _ := utf8.DecodeRune(cluster)
  250. switch {
  251. case r != utf8.RuneError && unicode.IsSpace(r) && r != nbsp: // nbsp is a non-breaking space
  252. addWord()
  253. space.WriteRune(r)
  254. case bytes.ContainsAny(cluster, breakpoints):
  255. addSpace()
  256. if curWidth+wordLen+width > limit {
  257. word.Write(cluster)
  258. wordLen += width
  259. } else {
  260. addWord()
  261. buf.Write(cluster)
  262. curWidth += width
  263. }
  264. default:
  265. if wordLen+width > limit {
  266. // Hardwrap the word if it's too long
  267. addWord()
  268. }
  269. word.Write(cluster)
  270. wordLen += width
  271. if curWidth+wordLen+space.Len() > limit {
  272. addNewline()
  273. }
  274. }
  275. pstate = parser.GroundState
  276. continue
  277. }
  278. switch action {
  279. case parser.PrintAction, parser.ExecuteAction:
  280. switch r := rune(b[i]); {
  281. case r == '\n':
  282. if wordLen == 0 {
  283. if curWidth+space.Len() > limit {
  284. curWidth = 0
  285. } else {
  286. // preserve whitespaces
  287. buf.Write(space.Bytes())
  288. }
  289. space.Reset()
  290. }
  291. addWord()
  292. addNewline()
  293. case unicode.IsSpace(r):
  294. addWord()
  295. space.WriteRune(r)
  296. case r == '-':
  297. fallthrough
  298. case runeContainsAny(r, breakpoints):
  299. addSpace()
  300. if curWidth+wordLen >= limit {
  301. // We can't fit the breakpoint in the current line, treat
  302. // it as part of the word.
  303. word.WriteRune(r)
  304. wordLen++
  305. } else {
  306. addWord()
  307. buf.WriteRune(r)
  308. curWidth++
  309. }
  310. default:
  311. if curWidth == limit {
  312. addNewline()
  313. }
  314. word.WriteRune(r)
  315. wordLen++
  316. if wordLen == limit {
  317. // Hardwrap the word if it's too long
  318. addWord()
  319. }
  320. if curWidth+wordLen+space.Len() > limit {
  321. addNewline()
  322. }
  323. }
  324. default:
  325. word.WriteByte(b[i])
  326. }
  327. // We manage the UTF8 state separately manually above.
  328. if pstate != parser.Utf8State {
  329. pstate = state
  330. }
  331. i++
  332. }
  333. if wordLen == 0 {
  334. if curWidth+space.Len() > limit {
  335. curWidth = 0
  336. } else {
  337. // preserve whitespaces
  338. buf.Write(space.Bytes())
  339. }
  340. space.Reset()
  341. }
  342. addWord()
  343. return buf.String()
  344. }
  345. func runeContainsAny(r rune, s string) bool {
  346. for _, c := range s {
  347. if c == r {
  348. return true
  349. }
  350. }
  351. return false
  352. }