Skip to main content

Prompt Library (Prompt)

Linked to the prompt type — a tree of strings, each optionally tagged, that renders to an XML-like format. Std.prompt(text, tag) is how you build one to start with.

Building

Prompt.push(prompt: prompt, other: prompt | str) -> void

Adds a sub-prompt.

prompt-push.stof
#[main]
fn main() {
  const p = prompt('hello', 'greet');
  p.push(', world');
  pln(p as str);
}
Output

Prompt.insert(prompt: prompt, index: int, other: prompt) -> void

prompt-insert.stof
#[main]
fn main() {
  const p = prompt(tag = 'greet');
  p.push('hello');
  p.insert(1, ', world');
  pln(p as str);
}
Output

Prompt.replace(prompt: prompt, index: int, other: prompt) -> void

prompt-replace.stof
#[main]
fn main() {
  const p = prompt(tag = 'greet');
  p.push('hello');
  p.replace(0, 'yo');
  pln(p as str);
}
Output

Prompt.remove(prompt: prompt, index: int) -> prompt

prompt-remove.stof
#[main]
fn main() {
  const p = prompt(tag = 'greet');
  p.push('hello');
  p.push(', world');
  p.remove(1);
  pln(p as str);
}
Output

Prompt.pop(prompt: prompt) -> prompt

Removes from the end.

prompt-pop.stof
#[main]
fn main() {
  const p = prompt('hello', 'greet');
  p.push(', world');
  p.pop();
  pln(p as str);
}
Output

Prompt.clear(prompt: prompt) -> void

prompt-clear.stof
#[main]
fn main() {
  const p = prompt('hello', 'greet');
  p.push(', world');
  p.clear();
  pln(p as str);
}
Output

Prompt.reverse(prompt: prompt) -> void

prompt-reverse.stof
#[main]
fn main() {
  const p = prompt(tag = 'greet');
  p.push(', world');
  p.push('hello');
  p.reverse();
  pln(p as str);
}
Output

Reading

Prompt.str(prompt: prompt) -> str

Same conversion a plain as str cast would do.

prompt-str.stof
#[main]
fn main() {
  const p = prompt('hello', 'greet');
  pln(p.str());
}
Output

Prompt.text(prompt: prompt) -> str

Just this prompt's own text, ignoring its tag and any sub-prompts.

prompt-text.stof
#[main]
fn main() {
  const p = prompt('hello', 'greet');
  pln(p.text());
}
Output

Prompt.tag(prompt: prompt) -> str

prompt-tag.stof
#[main]
fn main() {
  const p = prompt('hello', 'greet');
  pln(p.tag());
}
Output

Prompt.set_text(prompt: prompt, text: str) -> void

prompt-set-text.stof
#[main]
fn main() {
  const p = prompt('hello', 'greet');
  p.set_text('hello, world');
  pln(p.str());
}
Output

Prompt.set_tag(prompt: prompt, tag: str) -> void

null clears the tag entirely.

prompt-set-tag.stof
#[main]
fn main() {
  const p = prompt('hello', 'greet');
  p.set_tag('msg');
  pln(p.str());
}
Output

Structure

Prompt.len(prompt: prompt) -> int

The number of sub-prompts — not the text length.

prompt-len.stof
#[main]
fn main() {
  const p = prompt('hello', 'greet', prompt('hello, world'));
  pln(p.len());
}
Output

Prompt.empty(prompt: prompt) -> bool

prompt-empty.stof
#[main]
fn main() {
  const p = prompt('hello', 'greet');
  pln(p.empty());
}
Output

Prompt.any(prompt: prompt) -> bool

prompt-any.stof
#[main]
fn main() {
  const p = prompt('hello', 'greet');
  pln(p.any());
}
Output

Prompt.at(prompt: prompt, index: int) -> prompt

p[0] works the same way.

prompt-at.stof
#[main]
fn main() {
  const p = prompt('hello', 'greet', prompt('hello there'));
  pln(p[0].str());
}
Output

Prompt.prompts(prompt: prompt) -> list

Every direct sub-prompt, as a list.

prompt-prompts.stof
#[main]
fn main() {
  const p = prompt('hello', 'greet', prompt('a thing', 'sub'));
  pln(p.str());
  pln(p.prompts().len());
}
Output