## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE, message = FALSE, warning = FALSE, comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(tidycjk)

## ----basic--------------------------------------------------------------------
labels <- c("中文", "abcd", "日本語")

nchar(labels)      # characters
cjk_width(labels)  # terminal columns

## ----ragged-------------------------------------------------------------------
pad_by_char <- function(x, n) paste0(x, strrep(" ", n - nchar(x)))

cat(paste0("|", pad_by_char(labels, 8), "|"), sep = "\n")

## ----aligned------------------------------------------------------------------
cat(paste0("|", cjk_pad(labels, 8), "|"), sep = "\n")

## ----pad----------------------------------------------------------------------
cjk_pad("中文", 10)
cjk_pad("中文", 10, side = "left")
cjk_pad("中文", 10, side = "both")
cjk_pad("中文", 10, pad = ".")

## ----pad-over-----------------------------------------------------------------
cjk_width(cjk_pad("日本語テキスト", 4))

## ----truncate-----------------------------------------------------------------
cjk_truncate("我今天很開心", 8)
cjk_width(cjk_truncate("我今天很開心", 8))

## ----truncate-odd-------------------------------------------------------------
cjk_truncate("中文中文", 5, ellipsis = "")
cjk_width(cjk_truncate("中文中文", 5, ellipsis = ""))

## ----wrap---------------------------------------------------------------------
x <- "我今天很開心，因為天氣非常好而且朋友來看我"
cat(cjk_wrap(x, 14))

## ----wrap-check---------------------------------------------------------------
cjk_width(strsplit(cjk_wrap(x, 14), "\n", fixed = TRUE)[[1]])

## ----wrap-punct---------------------------------------------------------------
cat(cjk_wrap("他說（今天天氣很好）。我們去公園散步。", 12))

## ----wrap-style---------------------------------------------------------------
kana <- "きょうはとてもいいてんきですっしゃちょ"
cat(cjk_wrap(kana, 6, locale = "ja@lb=strict"))

## ----wrap-mixed---------------------------------------------------------------
cat(cjk_wrap("tidycjk 讓中日韓文字在終端機裡對齊 and it handles Latin too", 20))

## ----wrap-indent--------------------------------------------------------------
cat(cjk_wrap(x, 14, exdent = 2))

## ----contract-----------------------------------------------------------------
cjk_pad(c("中", NA), 6)
cjk_truncate(character(0), 6)
cjk_wrap(NA, 6)

## ----contract-error, error = TRUE---------------------------------------------
try({
cjk_pad("中文", Inf)
})

## ----contract-recycle, error = TRUE-------------------------------------------
try({
cjk_pad(c("a", "b", "c"), c(2, 3))
})

## ----ambiguous----------------------------------------------------------------
cjk_width(c("α", "→", "±"))

