Utility to display long messages with nice formatting. This function cuts the message to fit the current screen width of the R console. Words are never cut in the middle.

fit_screen(msg, width = NULL, leading_ws = TRUE, leader = "")

Arguments

msg

Text message: character vector.

width

A number between 0 and 1, or an integer. The maximum width of the screen the message should take. Numbers between 0 and 1 represent a fraction of the screen. You can also refer to the screen width with the special variable .sw. Integers represent the number of characters and cannot be lower than 15. Default is min(120, 0.95*.sw) (the min between 120 characters and 90% of the screen width).

leading_ws

Logical, default is TRUE. Whether to keep the leading white spaces when the line is cut.

leader

Character scalar, default is the empty string. If provided, this value will be placed in front of every line.

Value

It returns a single character vector with line breaks at the appropriate width.

Details

This function does not handle tabulations.

Examples


# A long message of two lines with a few leading spaces
msg = enumerate_items(state.name, nmax = Inf)
msg = paste0("     ", gsub("Michigan, ", "\n", msg))

# by default the message takes 95% of the screen
cat(fit_screen(msg))
#>      Alabama, Alaska, Arizona, Arkansas, California, Colorado, Connecticut,
#>      Delaware, Florida, Georgia, Hawaii, Idaho, Illinois, Indiana, Iowa,
#>      Kansas, Kentucky, Louisiana, Maine, Maryland, Massachusetts,
#> Minnesota, Mississippi, Missouri, Montana, Nebraska, Nevada, New Hampshire,
#> New Jersey, New Mexico, New York, North Carolina, North Dakota, Ohio,
#> Oklahoma, Oregon, Pennsylvania, Rhode Island, South Carolina, South Dakota,
#> Tennessee, Texas, Utah, Vermont, Virginia, Washington, West Virginia,
#> Wisconsin and Wyoming

# Now we reduce it to 50%
cat(fit_screen(msg, 0.5))
#>      Alabama, Alaska, Arizona, Arkansas,
#>      California, Colorado, Connecticut,
#>      Delaware, Florida, Georgia, Hawaii,
#>      Idaho, Illinois, Indiana, Iowa,
#>      Kansas, Kentucky, Louisiana, Maine,
#>      Maryland, Massachusetts,
#> Minnesota, Mississippi, Missouri,
#> Montana, Nebraska, Nevada, New
#> Hampshire, New Jersey, New Mexico, New
#> York, North Carolina, North Dakota,
#> Ohio, Oklahoma, Oregon, Pennsylvania,
#> Rhode Island, South Carolina, South
#> Dakota, Tennessee, Texas, Utah, Vermont,
#> Virginia, Washington, West Virginia,
#> Wisconsin and Wyoming

# we add leading_ws = FALSE to avoid the continuation of leading WS
cat(fit_screen(msg, 0.5, FALSE))
#>      Alabama, Alaska, Arizona, Arkansas,
#> California, Colorado, Connecticut,
#> Delaware, Florida, Georgia, Hawaii,
#> Idaho, Illinois, Indiana, Iowa, Kansas,
#> Kentucky, Louisiana, Maine, Maryland,
#> Massachusetts,
#> Minnesota, Mississippi, Missouri,
#> Montana, Nebraska, Nevada, New
#> Hampshire, New Jersey, New Mexico, New
#> York, North Carolina, North Dakota,
#> Ohio, Oklahoma, Oregon, Pennsylvania,
#> Rhode Island, South Carolina, South
#> Dakota, Tennessee, Texas, Utah, Vermont,
#> Virginia, Washington, West Virginia,
#> Wisconsin and Wyoming

# We add "#> " in front of each line
cat(fit_screen(msg, 0.5, leader = "#> "))
#> #>      Alabama, Alaska, Arizona,
#> #>      Arkansas, California, Colorado,
#> #>      Connecticut, Delaware, Florida,
#> #>      Georgia, Hawaii, Idaho,
#> #>      Illinois, Indiana, Iowa, Kansas,
#> #>      Kentucky, Louisiana, Maine,
#> #>      Maryland, Massachusetts,
#> #> Minnesota, Mississippi, Missouri,
#> #> Montana, Nebraska, Nevada, New
#> #> Hampshire, New Jersey, New Mexico,
#> #> New York, North Carolina, North
#> #> Dakota, Ohio, Oklahoma, Oregon,
#> #> Pennsylvania, Rhode Island, South
#> #> Carolina, South Dakota, Tennessee,
#> #> Texas, Utah, Vermont, Virginia,
#> #> Washington, West Virginia, Wisconsin
#> #> and Wyoming