def wrap_words
cli@0.2.0.0 · cli.bend:1019
import cli@0.2.0.0/cli.bend as Cli
import Base
1019def wrap_words(words: List<&2, String>, state: WrapState) -> String:
1020 match words state:
1021 case Nil{} WrapState{indent, cols, line, started, done}:
1022 Bool.pick(String, started,
1023 String.join(List.reverse(&2, String, line <> done), "\n"), "")
1024 case +word <> rest WrapState{+indent, +cols, +line, +started, +done}:
1025 +candidate = Bool.pick(String, started,
1026 String.append(line, String.append(" ", word)),
1027 String.append(indent, word))
1028 fits = Bool.or(Bool.not(started), Nat.is_le(width(strip(candidate)), cols))
1029 next = Bool.pick(WrapState, fits,
1030 WrapState{indent, cols, candidate, True{}, done},
1031 WrapState{indent, cols, String.append(indent, word), True{}, line <> done})
1032 wrap_words(rest, next)