Add nowrap keyword argument to disable text wrapping

This commit is contained in:
Thomas A. Christensen II 2022-02-01 18:31:17 -06:00
parent bc04423125
commit 3d2f86d275
Signed by: millironx
GPG key ID: 139C07724802BC5D
2 changed files with 18 additions and 2 deletions

View file

@ -56,7 +56,8 @@ Print an ASCII picture of a cow saying/thinking `message`
cowfiles support this, though.
- `tongue::AbstractString=" "`: A two-character string to be drawn in for the tongue. Not
all cowfiles support this.
- `wrap::Int`=40: The number of characters at which to wrap `message` to a new line
- `wrap::Int=40`: The number of characters at which to wrap `message` to a new line
- `nowrap::Bool=false`: Don't perform text wrapping on `message`
# Example
```jldoctest
@ -150,6 +151,7 @@ function cowmoo(message::AbstractString, mode; kwargs...)
eyes = dict_or_default(kwargs, :eyes, "oo")
tongue = dict_or_default(kwargs, :tongue, " ")
wrap = dict_or_default(kwargs, :wrap, 40)
nowrap = dict_or_default(kwargs, :nowrap, false)
# Default to 'say' mode
if mode ==:think
@ -160,7 +162,8 @@ function cowmoo(message::AbstractString, mode; kwargs...)
thoughts = "\\"
end
speechbubble = balloon(TextWrap.wrap(message, width=wrap))
wrapped_message = !nowrap ? TextWrap.wrap(message, width=wrap) : message
speechbubble = balloon(wrapped_message)
return string(speechbubble, cow(eyes=eyes, tongue=tongue, thoughts=thoughts))
end

View file

@ -25,4 +25,17 @@ DocMeta.setdocmeta!(Cowsay, :DocTestSetup, :(using Cowsay); recursive=true)
# Cowthink with io redirection
@test_warn cowthunk("Moo") cowthink(stderr, "Moo")
end
@testset "Word Wrapping" begin
# Long text, default wrap
@test cowsaid("Rollin' down a long highway out through New Mexico driftin' down to Santa Fe to ride a bull in a rodeo") == " _________________________________________\n/ Rollin' down a long highway out through \\\n| New Mexico driftin' down to Santa Fe to |\n\\ ride a bull in a rodeo /\n -----------------------------------------\n \\ ^__^\n \\ (oo)\\_______\n (__)\\ )\\/\\\n ||----w |\n || ||\n"
# Long text, no wrap
@test cowsaid("Rollin' down a long highway out through New Mexico driftin' down to Santa Fe to ride a bull in a rodeo", nowrap=true) == " ________________________________________________________________________________________________________\n< Rollin' down a long highway out through New Mexico driftin' down to Santa Fe to ride a bull in a rodeo >\n --------------------------------------------------------------------------------------------------------\n \\ ^__^\n \\ (oo)\\_______\n (__)\\ )\\/\\\n ||----w |\n || ||\n"
# Long text, conflicting wrap instructions (nowrap should win)
@test cowsaid("Rollin' down a long highway out through New Mexico driftin' down to Santa Fe to ride a bull in a rodeo", wrap=80, nowrap=true) == " ________________________________________________________________________________________________________\n< Rollin' down a long highway out through New Mexico driftin' down to Santa Fe to ride a bull in a rodeo >\n --------------------------------------------------------------------------------------------------------\n \\ ^__^\n \\ (oo)\\_______\n (__)\\ )\\/\\\n ||----w |\n || ||\n"
# Long text, different wrap amount
@test cowsaid("Rollin' down a long highway out through New Mexico driftin' down to Santa Fe to ride a bull in a rodeo", wrap=80) == " _________________________________________________________________________________\n/ Rollin' down a long highway out through New Mexico driftin' down to Santa Fe to \\\n\\ ride a bull in a rodeo /\n ---------------------------------------------------------------------------------\n \\ ^__^\n \\ (oo)\\_______\n (__)\\ )\\/\\\n ||----w |\n || ||\n"
end
end