Refactor to using kwargs for passing long lists of keyword arguments around

This commit is contained in:
Thomas A. Christensen II 2022-02-01 18:18:51 -06:00
parent 61f02e7ae7
commit bc04423125
Signed by: millironx
GPG key ID: 139C07724802BC5D

View file

@ -81,12 +81,12 @@ julia> cowthink("Have I mooed today?")
|| || || ||
``` ```
""" """
function cowsay(message::AbstractString; cow=default, eyes="oo", tongue=" ", wrap=40) function cowsay(message::AbstractString; kwargs...)
cowsay(stdout, message, cow=cow, eyes=eyes, tongue=tongue, wrap=wrap) cowsay(stdout, message; kwargs...)
end end
function cowsay(io::IO, message::AbstractString; cow=default, eyes="oo", tongue=" ", wrap=40) function cowsay(io::IO, message::AbstractString; kwargs...)
println(io, cowsaid(message, cow=cow, eyes=eyes, tongue=tongue, wrap=wrap)) println(io, cowsaid(message; kwargs...))
end end
""" """
@ -96,12 +96,12 @@ Print an ASCII picture of a cow thinking `message`
See [`cowsay`](@ref) See [`cowsay`](@ref)
""" """
function cowthink(message::AbstractString; cow=default, eyes="oo", tongue=" ", wrap=40) function cowthink(message::AbstractString; kwargs...)
cowthink(stdout, message, cow=cow, eyes=eyes, tongue=tongue, wrap=wrap) cowthink(stdout, message; kwargs...)
end end
function cowthink(io::IO, message::AbstractString; cow=default, eyes="oo", tongue=" ", wrap=40) function cowthink(io::IO, message::AbstractString; kwargs...)
println(io, cowthunk(message, cow=cow, eyes=eyes, tongue=tongue, wrap=wrap)) println(io, cowthunk(message; kwargs...))
end end
""" """
@ -123,9 +123,8 @@ julia> cowthunk("Have I mooed today?")
" _____________________\\n( Have I mooed today? )\\n ---------------------\\n o ^__^\\n o (oo)\\\\_______\\n (__)\\\\ )\\\\/\\\\\\n ||----w |\\n || ||\\n" " _____________________\\n( Have I mooed today? )\\n ---------------------\\n o ^__^\\n o (oo)\\\\_______\\n (__)\\\\ )\\\\/\\\\\\n ||----w |\\n || ||\\n"
``` ```
""" """
function cowsaid(message::AbstractString; cow=default, eyes="oo", tongue=" ", wrap=40) function cowsaid(message::AbstractString; kwargs...)
balloon = sayballoon(TextWrap.wrap(message, width=wrap)) return cowmoo(message, :say; kwargs...)
return string(balloon, cow(eyes=eyes, tongue=tongue))
end end
""" """
@ -135,9 +134,59 @@ Construct art of a cow thinking `message`.
See [`cowsaid`](@ref) and [`cowsay`](@ref) See [`cowsaid`](@ref) and [`cowsay`](@ref)
""" """
function cowthunk(message::AbstractString; cow=default, eyes="oo", tongue=" ", wrap=40) function cowthunk(message::AbstractString; kwargs...)
balloon = thinkballoon(TextWrap.wrap(message, width=wrap)) return cowmoo(message, :think; kwargs...)
return string(balloon, cow(eyes=eyes, tongue=tongue, thoughts="o")) end
"""
cowmoo(message::AbstractString, mode, kwargs...)
Converts `message` into either saying or thinking art based on the value of `mode`. All of
the default values for `kwargs` for pretty much every other function live here. See
[`cowsay`](@ref) for their descriptions.
"""
function cowmoo(message::AbstractString, mode; kwargs...)
cow = dict_or_default(kwargs, :cow, Cowsay.default)
eyes = dict_or_default(kwargs, :eyes, "oo")
tongue = dict_or_default(kwargs, :tongue, " ")
wrap = dict_or_default(kwargs, :wrap, 40)
# Default to 'say' mode
if mode ==:think
balloon = thinkballoon
thoughts = "o"
else
balloon = sayballoon
thoughts = "\\"
end
speechbubble = balloon(TextWrap.wrap(message, width=wrap))
return string(speechbubble, cow(eyes=eyes, tongue=tongue, thoughts=thoughts))
end
"""
dict_or_default(dict, key, value)
If `key` is present in `dict`, then return `dict[key]`, otherwise return `value`
# Example
```jldoctest
julia> dict = Dict(:holstein => "lotsa milk", :jersey => "butterfat", :shorthorn => "worthless");
julia> Cowsay.dict_or_default(dict, :holstein, "fluid")
"lotsa milk"
julia> Cowsay.dict_or_default(dict, :brown_swiss, "cheese")
"cheese"
```
"""
function dict_or_default(dict, key, value)
if haskey(dict, key)
return dict[key]
else
return value
end
end end
""" """