use crate::host::{type_error, PyHost, PyObj};
use fusevm::Value;
pub fn entries(h: &mut PyHost) -> Vec<(String, Value)> {
["wrap", "fill", "shorten", "indent", "dedent"]
.iter()
.map(|name| {
(
(*name).to_string(),
h.alloc(PyObj::Builtin(format!("textwrap.{name}"))),
)
})
.collect()
}
pub fn call(h: &mut PyHost, fname: &str, args: &[Value]) -> Option<Result<Value, String>> {
match fname {
"wrap" => Some(wrap(h, args)),
"fill" => Some(fill(h, args)),
"shorten" => Some(shorten(h, args)),
"indent" => Some(indent(h, args)),
"dedent" => Some(dedent(h, args)),
_ => None,
}
}
fn text_arg(h: &PyHost, args: &[Value], fname: &str) -> Result<String, String> {
let v = args
.first()
.ok_or_else(|| type_error(&format!("{fname}() missing required argument: 'text'")))?;
h.as_str(v)
.ok_or_else(|| type_error(&format!("{fname}() requires a str, not '{}'", h.type_name(v))))
}
fn width_arg(h: &PyHost, args: &[Value], idx: usize) -> usize {
args.get(idx)
.and_then(|v| h.as_int(v))
.map(|n| n.max(1) as usize)
.unwrap_or(70)
}
fn split_words(text: &str) -> Vec<String> {
text.split_whitespace().map(|w| w.to_string()).collect()
}
fn break_long_word(word: &str, width: usize, out: &mut Vec<String>) {
let chars: Vec<char> = word.chars().collect();
let mut i = 0;
while chars.len() - i > width {
out.push(chars[i..i + width].iter().collect());
i += width;
}
out.push(chars[i..].iter().collect());
}
fn wrap_lines(text: &str, width: usize) -> Vec<String> {
let words = split_words(text);
let mut lines: Vec<String> = Vec::new();
let mut cur = String::new();
let mut cur_len = 0usize;
for word in words {
let wlen = word.chars().count();
if wlen > width {
if !cur.is_empty() {
lines.push(std::mem::take(&mut cur));
cur_len = 0;
}
let mut pieces = Vec::new();
break_long_word(&word, width, &mut pieces);
let last = pieces.pop();
for p in pieces {
lines.push(p);
}
if let Some(last) = last {
cur_len = last.chars().count();
cur = last;
}
continue;
}
if cur.is_empty() {
cur = word;
cur_len = wlen;
} else if cur_len + 1 + wlen <= width {
cur.push(' ');
cur.push_str(&word);
cur_len += 1 + wlen;
} else {
lines.push(std::mem::take(&mut cur));
cur = word;
cur_len = wlen;
}
}
if !cur.is_empty() {
lines.push(cur);
}
lines
}
fn wrap(h: &mut PyHost, args: &[Value]) -> Result<Value, String> {
let text = text_arg(h, args, "wrap")?;
let width = width_arg(h, args, 1);
let lines: Vec<Value> = wrap_lines(&text, width)
.into_iter()
.map(|l| h.new_str(l))
.collect();
Ok(h.new_list(lines))
}
fn fill(h: &mut PyHost, args: &[Value]) -> Result<Value, String> {
let text = text_arg(h, args, "fill")?;
let width = width_arg(h, args, 1);
Ok(h.new_str(wrap_lines(&text, width).join("\n")))
}
fn shorten(h: &mut PyHost, args: &[Value]) -> Result<Value, String> {
let text = text_arg(h, args, "shorten")?;
let width = width_arg(h, args, 1);
let placeholder = args
.get(2)
.and_then(|v| h.as_str(v))
.unwrap_or_else(|| " [...]".to_string());
let collapsed = split_words(&text).join(" ");
if collapsed.chars().count() <= width {
return Ok(h.new_str(collapsed));
}
let ph_stripped = placeholder.trim_start();
if ph_stripped.chars().count() > width {
return Err("ValueError: placeholder too large for max width".into());
}
let ph_len = placeholder.chars().count();
let mut cur = String::new();
let mut cur_len = 0usize;
for word in split_words(&text) {
let wlen = word.chars().count();
let added = if cur.is_empty() { wlen } else { 1 + wlen };
if cur_len + added + ph_len <= width {
if !cur.is_empty() {
cur.push(' ');
}
cur.push_str(&word);
cur_len += added;
} else {
break;
}
}
let result = if cur.is_empty() {
ph_stripped.to_string()
} else {
format!("{cur}{placeholder}")
};
Ok(h.new_str(result))
}
fn indent(h: &mut PyHost, args: &[Value]) -> Result<Value, String> {
let text = text_arg(h, args, "indent")?;
let prefix = args
.get(1)
.and_then(|v| h.as_str(v))
.ok_or_else(|| type_error("indent() missing required argument: 'prefix'"))?;
let mut out = String::new();
for line in text.split_inclusive('\n') {
if line.trim().is_empty() {
out.push_str(line);
} else {
out.push_str(&prefix);
out.push_str(line);
}
}
Ok(h.new_str(out))
}
fn dedent(h: &mut PyHost, args: &[Value]) -> Result<Value, String> {
let text = text_arg(h, args, "dedent")?;
let raw_lines: Vec<&str> = text.split('\n').collect();
let mut margin: Option<String> = None;
for line in &raw_lines {
if line.trim().is_empty() {
continue;
}
let indent: String = line
.chars()
.take_while(|c| *c == ' ' || *c == '\t')
.collect();
margin = Some(match margin {
None => indent,
Some(m) => common_prefix(&m, &indent),
});
}
let margin = margin.unwrap_or_default();
let out: Vec<String> = raw_lines
.iter()
.map(|line| {
if line.trim().is_empty() {
String::new()
} else {
line.strip_prefix(&margin).unwrap_or(line).to_string()
}
})
.collect();
Ok(h.new_str(out.join("\n")))
}
fn common_prefix(a: &str, b: &str) -> String {
a.chars()
.zip(b.chars())
.take_while(|(x, y)| x == y)
.map(|(x, _)| x)
.collect()
}