use std::collections::BTreeMap;
use std::ops::Range;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use crate::inbox::form::{self, Form, FormError, Question, QuestionKind};
use crate::inbox::{Answer, FormRecord, ItemKind, Kind, Op, State, Thread};
use crate::tui::textarea::TextArea;
use crate::tui::ui;
use super::inbox::{edit_text, InboxFocus, PaneLine, Tone};
use super::App;
pub struct FormEdit {
pub thread: u64,
pub message: String,
pub question: usize,
pub option: usize,
pub editing: Option<TextArea>,
pub confirming: bool,
}
pub struct Pinned<'a> {
pub message: &'a str,
pub form: &'a Form,
pub record: &'a FormRecord,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct FormSpot {
pub form: usize,
pub question: Option<usize>,
}
pub fn pinned_forms(t: &Thread) -> Vec<Pinned<'_>> {
if t.kind != Kind::Thread {
return Vec::new();
}
t.feed
.iter()
.rev()
.filter_map(|i| match &i.kind {
ItemKind::Message { id, blocks, withdrawn: false, form: Some(record), .. } if record.is_open() => {
form::form_in(blocks).map(|form| Pinned { message: id, form, record })
}
_ => None,
})
.collect()
}
pub fn value_of(q: &Question, record: &FormRecord) -> Option<Answer> {
record.draft.get(&q.id).cloned().or_else(|| form::default_of(q))
}
fn answered(a: Option<&Answer>) -> bool {
match a {
None => false,
Some(Answer::Text(t)) => !t.trim().is_empty(),
Some(Answer::Choices(os)) => !os.is_empty(),
Some(_) => true,
}
}
pub fn shown(form: &Form, q: &Question, record: &FormRecord) -> bool {
form::applies(form, q, |p| value_of(p, record))
}
fn missing(form: &Form, q: &Question, record: &FormRecord) -> bool {
q.required && shown(form, q, record) && !answered(value_of(q, record).as_ref())
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Confirm {
Ready(BTreeMap<String, Answer>),
Missing(Vec<String>),
Closed,
Invalid(String),
}
pub fn confirm(form: &Form, record: &FormRecord) -> Confirm {
match form::resolve_submission(form, record, &BTreeMap::new()) {
Ok(answers) => Confirm::Ready(answers),
Err(FormError::Closed(_)) => Confirm::Closed,
Err(FormError::Invalid(why)) => {
let labels: Vec<String> =
form.questions.iter().filter(|q| missing(form, q, record)).map(|q| q.label.clone()).collect();
if labels.is_empty() { Confirm::Invalid(why) } else { Confirm::Missing(labels) }
}
}
}
pub(super) fn answer_count(answers: &BTreeMap<String, Answer>) -> usize {
answers.values().filter(|a| answered(Some(a))).count()
}
fn option_count(q: &Question) -> usize {
match &q.kind {
QuestionKind::Choice { options, .. } => options.len(),
QuestionKind::Confirm { .. } => 2,
QuestionKind::Text { .. } => 0,
}
}
fn option_cursor(q: &Question, value: Option<&Answer>) -> usize {
match (&q.kind, value) {
(QuestionKind::Choice { options, .. }, Some(Answer::Choice(id))) => {
options.iter().position(|o| &o.id == id).unwrap_or(0)
}
(QuestionKind::Confirm { .. }, Some(Answer::Confirm(false))) => 1,
_ => 0,
}
}
pub fn pick(q: &Question, value: Option<&Answer>, option: usize) -> Option<Answer> {
match &q.kind {
QuestionKind::Choice { options, multiple: false, .. } => options.get(option).map(|o| Answer::Choice(o.id.clone())),
QuestionKind::Choice { options, multiple: true, .. } => {
let o = options.get(option)?;
let mut picked = match value {
Some(Answer::Choices(ids)) => ids.clone(),
_ => Vec::new(),
};
match picked.iter().position(|x| *x == o.id) {
Some(i) => {
picked.remove(i);
}
None => picked.push(o.id.clone()),
}
Some(Answer::Choices(options.iter().filter(|x| picked.contains(&x.id)).map(|x| x.id.clone()).collect()))
}
QuestionKind::Confirm { .. } => Some(Answer::Confirm(option == 0)),
QuestionKind::Text { .. } => None,
}
}
fn yes_no(q: &Question) -> (&str, &str) {
match &q.kind {
QuestionKind::Confirm { yes, no, .. } => (yes.as_deref().unwrap_or("Yes"), no.as_deref().unwrap_or("No")),
_ => ("Yes", "No"),
}
}
pub fn answer_text(q: &Question, a: Option<&Answer>) -> String {
let label = |id: &str| match &q.kind {
QuestionKind::Choice { options, .. } => {
options.iter().find(|o| o.id == id).map_or(id.to_string(), |o| o.label.clone())
}
_ => id.to_string(),
};
match a {
None => "–".into(),
Some(Answer::Choice(id)) => label(id),
Some(Answer::Choices(ids)) if ids.is_empty() => "none".into(),
Some(Answer::Choices(ids)) => ids.iter().map(|id| label(id)).collect::<Vec<_>>().join(", "),
Some(Answer::Text(t)) if t.trim().is_empty() => "–".into(),
Some(Answer::Text(t)) => t.clone(),
Some(Answer::Confirm(b)) => {
let (yes, no) = yes_no(q);
if *b { yes.into() } else { no.into() }
}
}
}
pub fn answer_rows(form: &Form, answers: &BTreeMap<String, Answer>) -> Vec<PaneLine> {
let mut out = Vec::new();
for q in &form.questions {
if !form::applies(form, q, |p| answers.get(&p.id).cloned()) {
continue;
}
let a = answers.get(&q.id);
let multiline = matches!(q.kind, QuestionKind::Text { multiline: true, .. });
let mut head = vec![(Tone::Text, q.label.clone())];
if q.under.is_some() {
head.insert(0, (Tone::Dim, "↳ ".into()));
}
match a {
Some(Answer::Text(t)) if multiline && !t.trim().is_empty() => {
head.push((Tone::Dim, ":".into()));
out.push(head);
out.push(vec![(Tone::Markdown, t.clone())]);
}
_ => {
head.extend([(Tone::Dim, ": ".into()), (Tone::Plain, answer_text(q, a))]);
out.push(head);
}
}
}
out
}
pub struct FormCursor<'a> {
pub question: usize,
pub option: usize,
pub editing: Option<&'a str>,
pub confirming: bool,
}
pub struct FormLines {
pub lines: Vec<PaneLine>,
pub questions: Vec<Range<usize>>,
}
const TEXT_LINES: usize = 3;
const INDENT: &str = " ";
const SUB: &str = " ";
pub fn form_lines(form: &Form, record: &FormRecord, cursor: Option<&FormCursor>, width: usize) -> FormLines {
let mut lines: Vec<PaneLine> = Vec::new();
let mut questions = Vec::new();
let n = form.questions.iter().filter(|q| q.under.is_none()).count();
let mut number = 0;
for (i, q) in form.questions.iter().enumerate() {
let start = lines.len();
let sub = q.under.is_some();
if !sub {
number += 1;
}
if !shown(form, q, record) {
questions.push(start..start);
continue;
}
let room = width.saturating_sub(INDENT.len() + if sub { SUB.len() } else { 0 });
let at = cursor.filter(|c| c.question == i);
let value = value_of(q, record);
let mut head = vec![
if at.is_some() { (Tone::Bold, "› ".to_string()) } else { (Tone::Plain, " ".to_string()) },
(Tone::Dim, if sub { format!("{SUB}↳ ") } else { format!("{number}/{n} ") }),
(Tone::Text, q.label.clone()),
];
if missing(form, q, record) {
head.push((Tone::State(State::NeedsYou), " *".into()));
}
lines.push(head);
if let Some(context) = q.context.as_deref().filter(|c| at.is_some() && !c.trim().is_empty()) {
lines.push(vec![(Tone::Markdown, context.to_string())]);
}
let option = at.map(|c| c.option);
match &q.kind {
QuestionKind::Choice { options, multiple, .. } => {
let picked = |id: &str| match &value {
Some(Answer::Choice(o)) => o == id,
Some(Answer::Choices(os)) => os.iter().any(|o| o == id),
_ => false,
};
let marks = |on: bool| match (multiple, on) {
(false, true) => "(•) ",
(false, false) => "( ) ",
(true, true) => "[x] ",
(true, false) => "[ ] ",
};
let labels: Vec<(bool, &str)> = options.iter().map(|o| (picked(&o.id), o.label.as_str())).collect();
lines.push(widget(&labels, option, marks));
if let Some(desc) = option.and_then(|o| options.get(o)).and_then(|o| o.description.as_deref()) {
lines.push(vec![(Tone::Dim, format!("{INDENT}{desc}"))]);
}
}
QuestionKind::Confirm { .. } => {
let (yes, no) = yes_no(q);
let v = match &value {
Some(Answer::Confirm(b)) => Some(*b),
_ => None,
};
let labels = [(v == Some(true), yes), (v == Some(false), no)];
lines.push(widget(&labels, option, |on| if on { "(•) " } else { "( ) " }));
}
QuestionKind::Text { placeholder, .. } => {
let editing = at.and_then(|c| c.editing);
let text = match (editing, &value) {
(Some(t), _) => t.to_string(),
(None, Some(Answer::Text(t))) => t.clone(),
_ => String::new(),
};
let mark = if editing.is_some() { "✎ " } else { "" };
if text.is_empty() {
let hint = placeholder.as_deref().unwrap_or("(empty)");
lines.push(vec![(Tone::Plain, format!("{INDENT}{mark}")), (Tone::Dim, ui::truncate(hint, room))]);
} else {
let all: Vec<&str> = text.lines().collect();
for (j, l) in all.iter().take(TEXT_LINES).enumerate() {
let mark = if j == 0 { mark } else { "" };
lines.push(vec![(Tone::Plain, format!("{INDENT}{}", ui::truncate(&format!("{mark}{l}"), room)))]);
}
if all.len() > TEXT_LINES {
lines.push(vec![(Tone::Dim, format!("{INDENT}… {} more lines", all.len() - TEXT_LINES))]);
}
}
}
}
if sub {
for l in lines[start + 1..].iter_mut().filter(|l| !l.iter().any(|(t, _)| *t == Tone::Markdown)) {
l.insert(0, (Tone::Plain, SUB.to_string()));
}
}
questions.push(start..lines.len());
}
let hint = match cursor {
Some(c) if c.editing.is_some() => "[Enter] keep [Esc] keep [Alt-Enter] newline",
Some(c) if c.confirming => match confirm(form, record) {
Confirm::Ready(answers) => {
let n = answer_count(&answers);
let s = if n == 1 { "" } else { "s" };
lines.push(vec![(Tone::Bold, format!("{}: submit {n} answer{s}?", form.submit))]);
"[Enter] yes [Esc] no"
}
Confirm::Missing(labels) => {
lines.push(vec![(Tone::State(State::NeedsYou), format!("missing: {}", labels.join(", ")))]);
"[Esc] back"
}
Confirm::Closed => {
lines.push(vec![(Tone::Dim, "form is no longer open".to_string())]);
"[Esc] back"
}
Confirm::Invalid(why) => {
lines.push(vec![(Tone::State(State::NeedsYou), why)]);
"[Esc] back"
}
},
_ => "[Tab] next [Space] pick [e] edit [Enter] Confirm",
};
lines.push(vec![(Tone::Dim, hint.to_string())]);
FormLines { lines, questions }
}
fn widget(labels: &[(bool, &str)], cursor: Option<usize>, mark: impl Fn(bool) -> &'static str) -> PaneLine {
let mut out = vec![(Tone::Plain, INDENT.to_string())];
for (j, (on, label)) in labels.iter().enumerate() {
if j > 0 {
out.push((Tone::Plain, " ".into()));
}
if cursor == Some(j) {
out.push((Tone::Cursor, format!("{}{label}", mark(*on))));
} else {
out.push((Tone::Plain, mark(*on).to_string()));
out.push((Tone::Text, label.to_string()));
}
}
out
}
pub fn feed_form_lines(f: &Form, record: Option<&FormRecord>, answered_above: bool, utc_offset: i64) -> Vec<PaneLine> {
let title = f.title.as_deref().unwrap_or("Form");
match record.map(|r| &r.state) {
None | Some(crate::inbox::FormState::Open) => {
vec![vec![(Tone::Dim, format!("form: {title} · open, pinned above"))]]
}
Some(crate::inbox::FormState::Submitted { at, answers, .. }) => {
let when = super::inbox::stamp(*at, utc_offset);
if answered_above {
return vec![vec![(Tone::Dim, format!("form: {title} · submitted {when}, answers above"))]];
}
let mut out = vec![vec![(Tone::Dim, format!("form: {title} · submitted {when}"))]];
out.extend(answer_rows(f, answers));
out
}
Some(crate::inbox::FormState::Withdrawn) => vec![vec![(Tone::Dim, "form withdrawn".to_string())]],
}
}
pub fn submission_lines(form: Option<&Form>, answers: &BTreeMap<String, Answer>, at: &str) -> Vec<PaneLine> {
let n = answer_count(answers);
let s = if n == 1 { "" } else { "s" };
let title = form.map_or("Form", form_title);
let mut out = vec![vec![(Tone::Dim, format!("{at} you answered \"{title}\" ({n} question{s})"))]];
match form {
Some(f) => out.extend(answer_rows(f, answers)),
None => out.extend(answers.iter().flat_map(|(id, a)| {
if let Answer::Text(t) = a {
if t.contains('\n') {
return vec![vec![(Tone::Text, id.clone()), (Tone::Dim, ":".into())], vec![(Tone::Markdown, t.clone())]];
}
}
let value = match a {
Answer::Choice(o) => o.clone(),
Answer::Choices(os) if os.is_empty() => "none".into(),
Answer::Choices(os) => os.join(", "),
Answer::Text(t) if t.trim().is_empty() => "–".into(),
Answer::Text(t) => t.clone(),
Answer::Confirm(b) => if *b { "yes" } else { "no" }.into(),
};
vec![vec![(Tone::Text, id.clone()), (Tone::Dim, ": ".into()), (Tone::Plain, value)]]
})),
}
out
}
impl App {
fn form_target(&self) -> Option<(Thread, usize)> {
let e = self.inbox.form.as_ref()?;
let t = self.inbox.threads().iter().find(|t| t.id == e.thread)?;
let i = pinned_forms(t).iter().position(|p| p.message == e.message)?;
Some((t.clone(), i))
}
pub(super) fn focus_inbox_form(&mut self, spot: Option<FormSpot>) -> bool {
let Some(t) = self.selected_inbox_owned() else { return false };
let pinned = pinned_forms(&t);
if pinned.is_empty() {
return false;
}
if self.refuse_user_op(&t) {
return true;
}
let spot = spot.unwrap_or(FormSpot { form: 0, question: Some(0) });
let fi = spot.form.min(pinned.len() - 1);
let current = self
.inbox
.form
.as_ref()
.filter(|e| e.thread == t.id && e.message == pinned[fi].message)
.map(|e| (e.question, e.editing.is_some()));
let qi = spot.question.or(current.map(|(q, _)| q)).unwrap_or(0);
if current == Some((qi, true)) {
self.inbox.focus = InboxFocus::Form;
return true;
}
self.close_form_edit();
self.set_form_cursor(&t, fi, qi);
self.inbox.focus = InboxFocus::Form;
true
}
fn set_form_cursor(&mut self, t: &Thread, fi: usize, qi: usize) {
let pinned = pinned_forms(t);
let Some(p) = pinned.get(fi) else { return };
let qi = qi.min(p.form.questions.len().saturating_sub(1));
let option = p.form.questions.get(qi).map_or(0, |q| option_cursor(q, value_of(q, p.record).as_ref()));
self.inbox.form = Some(FormEdit {
thread: t.id,
message: p.message.to_string(),
question: qi,
option,
editing: None,
confirming: false,
});
self.inbox.forms_follow.set(true);
}
fn leave_form(&mut self) {
self.close_form_edit();
self.inbox.focus = InboxFocus::Thread;
}
pub(super) fn close_form_edit(&mut self) {
if self.inbox.form.as_ref().is_some_and(|e| e.editing.is_some()) {
let _ = self.commit_text_edit();
}
self.inbox.form = None;
}
pub(super) fn check_form_edit(&mut self) {
if self.inbox.form.is_some() && self.form_target().is_none() {
self.inbox.form = None;
if self.inbox.focus == InboxFocus::Form {
self.inbox.focus = InboxFocus::Thread;
self.status = Some("form is no longer open".into());
}
}
}
fn save_answer(&mut self, t: &Thread, fi: usize, q: &Question, answer: Answer) {
let pinned = pinned_forms(t);
let Some(p) = pinned.get(fi) else { return };
if value_of(q, p.record).as_ref() == Some(&answer) {
return;
}
let op = Op::SaveDraft { thread: t.id, message: p.message.to_string(), answers: [(q.id.clone(), answer)].into() };
self.request_inbox(op);
}
fn commit_text_edit(&mut self) -> Result<(), String> {
let Some((t, fi)) = self.form_target() else {
self.inbox.form = None;
return Ok(());
};
let Some(e) = self.inbox.form.as_ref() else { return Ok(()) };
let Some(text) = e.editing.as_ref().map(|a| a.input().to_string()) else { return Ok(()) };
let pinned = pinned_forms(&t);
let Some(q) = pinned[fi].form.questions.get(e.question).cloned() else { return Ok(()) };
let answer = Answer::Text(text);
form::check_answer(&q, &answer)?;
if let Some(e) = self.inbox.form.as_mut() {
e.editing = None;
}
self.save_answer(&t, fi, &q, answer);
Ok(())
}
pub(super) fn on_key_form(&mut self, key: KeyEvent) {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
let Some((t, fi)) = self.form_target() else {
self.inbox.form = None;
self.inbox.focus = InboxFocus::Thread;
self.status = Some("form is no longer open".into());
return;
};
if self.inbox.form.as_ref().is_some_and(|e| e.editing.is_some()) {
self.on_key_form_text(key);
return;
}
self.status = None;
let pinned = pinned_forms(&t);
let p = &pinned[fi];
let Some(e) = self.inbox.form.as_ref() else { return };
let Some(q) = p.form.questions.get(e.question).cloned() else { return };
let (option, confirming) = (e.option, e.confirming);
let value = value_of(&q, p.record);
let (form, record) = (p.form.clone(), p.record.clone());
drop(pinned);
match key.code {
KeyCode::Esc if confirming => self.form_edit().confirming = false,
KeyCode::Esc => self.leave_form(),
KeyCode::Char('q') => self.should_quit = true,
KeyCode::Char('c') if ctrl => self.should_quit = true,
KeyCode::Char(':') => self.open_prompt(),
KeyCode::Char('?') => self.open_help(),
KeyCode::Tab => self.step_question(&t, 1),
KeyCode::BackTab => self.step_question(&t, -1),
KeyCode::Up | KeyCode::Left => {
let e = self.form_edit();
e.option = option.saturating_sub(1);
}
KeyCode::Down | KeyCode::Right => {
let max = option_count(&q).saturating_sub(1);
self.form_edit().option = (option + 1).min(max);
}
KeyCode::Char(' ') | KeyCode::Char('e') if matches!(q.kind, QuestionKind::Text { .. }) => {
let text = match &value {
Some(Answer::Text(t)) => t.clone(),
_ => String::new(),
};
let e = self.form_edit();
e.editing = Some(TextArea::with_text(&text));
e.confirming = false;
}
KeyCode::Char('e') => self.status = Some("e edits a text question".into()),
KeyCode::Char(' ') => {
if let Some(answer) = pick(&q, value.as_ref(), option) {
self.form_edit().confirming = false;
self.save_answer(&t, fi, &q, answer);
}
}
KeyCode::Enter if !confirming => self.form_edit().confirming = true,
KeyCode::Enter => match confirm(&form, &record) {
Confirm::Ready(answers) => {
let message = self.form_edit().message.clone();
self.request_inbox(Op::Submit { thread: t.id, message, answers });
self.inbox.form = None;
self.inbox.focus = InboxFocus::Thread;
self.status = Some(format!("submitted to {}", t.owner_name));
}
Confirm::Closed => {
self.inbox.form = None;
self.inbox.focus = InboxFocus::Thread;
self.status = Some("form is no longer open".into());
}
Confirm::Missing(_) | Confirm::Invalid(_) => {}
},
KeyCode::Char('r' | 'i') => {
self.close_form_edit();
self.inbox.focus = InboxFocus::Thread;
self.open_reply(&t);
}
_ => {}
}
}
fn form_edit(&mut self) -> &mut FormEdit {
self.inbox.form.as_mut().expect("the form zone has a cursor")
}
fn step_question(&mut self, t: &Thread, step: isize) {
let pinned = pinned_forms(t);
let Some(e) = self.inbox.form.as_ref() else { return };
let Some(fi) = pinned.iter().position(|p| p.message == e.message) else { return };
let here = (fi, e.question);
let all: Vec<(usize, usize)> = pinned
.iter()
.enumerate()
.flat_map(|(f, p)| {
p.form.questions.iter().enumerate().filter_map(move |(q, question)| {
(shown(p.form, question, p.record) || (f, q) == here).then_some((f, q))
})
})
.collect();
let Some(pos) = all.iter().position(|&x| x == here) else { return };
let (f, q) = all[(pos as isize + step).rem_euclid(all.len() as isize) as usize];
self.set_form_cursor(t, f, q);
}
fn on_key_form_text(&mut self, key: KeyEvent) {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
let newline = key.modifiers.intersects(KeyModifiers::ALT | KeyModifiers::SHIFT);
let multiline = self.editing_question().is_some_and(|q| matches!(q.kind, QuestionKind::Text { multiline: true, .. }));
match key.code {
KeyCode::Enter if newline => {
if multiline {
if let Some(a) = self.form_edit().editing.as_mut() {
a.newline();
}
}
}
KeyCode::Enter | KeyCode::Esc => self.finish_text_edit(),
KeyCode::Char('c') if ctrl => self.finish_text_edit(),
_ => {
if let Some(a) = self.form_edit().editing.as_mut() {
edit_text(a, key);
}
}
}
}
fn finish_text_edit(&mut self) {
match self.commit_text_edit() {
Ok(()) => self.status = None,
Err(why) => self.status = Some(why),
}
}
pub fn editing_question(&self) -> Option<Question> {
let e = self.inbox.form.as_ref().filter(|e| e.editing.is_some())?;
let (t, fi) = self.form_target()?;
pinned_forms(&t)[fi].form.questions.get(e.question).cloned()
}
pub fn form_text_edit(&self) -> Option<&TextArea> {
self.inbox.form.as_ref().filter(|_| self.inbox.focus == InboxFocus::Form)?.editing.as_ref()
}
pub fn form_cursor(&self, thread: u64, message: &str) -> Option<FormCursor<'_>> {
let e = self.inbox.form.as_ref().filter(|e| {
self.inbox.focus == InboxFocus::Form && e.thread == thread && e.message == message
})?;
Some(FormCursor {
question: e.question,
option: e.option,
editing: e.editing.as_ref().map(TextArea::input),
confirming: e.confirming,
})
}
}
pub fn forms_offset(prev: usize, focus: Option<Range<usize>>, total: usize, height: usize, follow: bool) -> usize {
let mut off = prev.min(total.saturating_sub(height));
if let (true, Some(r)) = (follow, focus) {
if r.start < off {
off = r.start;
} else if r.end > off + height {
off = (r.end - height).min(r.start);
}
}
off
}
pub fn form_title(f: &Form) -> &str {
f.title.as_deref().filter(|t| !t.trim().is_empty()).unwrap_or("Form")
}
#[cfg(test)]
mod tests {
use super::super::test_support::*;
use super::*;
use crate::inbox::form::{ChoiceDefault, ChoiceOption};
use crate::inbox::{feed, Block, Compose, FeedItem, FormState, Inbox};
fn form_of<'a>(t: &'a Thread, id: &str) -> Option<(&'a Form, &'a FormRecord)> {
feed::form_of(&t.feed, id)
}
fn q(id: &str, label: &str, required: bool, kind: QuestionKind) -> Question {
Question { id: id.into(), label: label.into(), context: None, required, under: None, when: None, kind }
}
fn opt(id: &str, label: &str) -> ChoiceOption {
ChoiceOption { id: id.into(), label: label.into(), description: None }
}
fn choice(multiple: bool, default: Option<ChoiceDefault>) -> QuestionKind {
QuestionKind::Choice { options: vec![opt("post", "Post the reply"), opt("skip", "Don't reply")], multiple, default }
}
fn text(default: Option<&str>, multiline: bool) -> QuestionKind {
QuestionKind::Text { placeholder: Some("Reply to post".into()), default: default.map(str::to_string), multiline, max: 20 }
}
fn drafts() -> Form {
let mut first = q("c-1", "greptile on `src/cost.ts:42`", true, choice(false, Some(ChoiceDefault::One("post".into()))));
first.context = Some("> Consider batching these writes.".into());
Form {
id: "drafts".into(),
title: Some("Replies to post".into()),
submit: "Post replies".into(),
questions: vec![
first,
Question { under: Some("c-1".into()), when: Some("post".into()), ..q("c-1-text", "Reply", false, text(Some("Already batched"), true)) },
q("notes", "Anything else?", false, text(None, true)),
q("merge", "Merge after?", true, QuestionKind::Confirm { yes: Some("Merge".into()), no: None, default: None }),
],
}
}
fn texts(lines: &[PaneLine]) -> Vec<String> {
lines.iter().map(|l| l.iter().map(|(_, s)| s.as_str()).collect()).collect()
}
fn cursor(question: usize, option: usize) -> FormCursor<'static> {
FormCursor { question, option, editing: None, confirming: false }
}
#[test]
fn form_lines_render_each_widget_and_the_required_marker() {
let form = drafts();
let record = FormRecord::open("drafts");
let out = form_lines(&form, &record, None, 40);
assert_eq!(texts(&out.lines), [
" 1/3 greptile on `src/cost.ts:42`",
" (•) Post the reply ( ) Don't reply",
" ↳ Reply",
" Already batched",
" 2/3 Anything else?",
" Reply to post",
" 3/3 Merge after? *",
" ( ) Merge ( ) No",
"[Tab] next [Space] pick [e] edit [Enter] Confirm",
]);
assert_eq!(out.questions, [0..2, 2..4, 4..6, 6..8]);
assert_eq!(out.lines[5][1], (Tone::Dim, "Reply to post".into()));
assert_eq!(out.lines[6][3], (Tone::State(State::NeedsYou), " *".into()));
assert_eq!(out.lines[0][2].0, Tone::Text);
let mut multi = drafts();
multi.questions[0].kind = choice(true, None);
let mut record = FormRecord::open("drafts");
record.draft.insert("c-1".into(), Answer::Choices(vec!["skip".into()]));
record.draft.insert("merge".into(), Answer::Confirm(false));
let out = form_lines(&multi, &record, None, 40);
assert_eq!(texts(&out.lines)[1], " [ ] Post the reply [x] Don't reply");
assert_eq!(texts(&out.lines)[2], " 2/3 Anything else?");
assert_eq!(out.questions, [0..2, 2..2, 2..4, 4..6]);
assert_eq!(texts(&out.lines)[4], " 3/3 Merge after?", "answered: no marker");
assert_eq!(texts(&out.lines)[5], " ( ) Merge (•) No");
}
#[test]
fn the_focused_question_shows_its_context_and_highlights_the_option() {
let form = drafts();
let record = FormRecord::open("drafts");
let out = form_lines(&form, &record, Some(&cursor(0, 1)), 40);
let t = texts(&out.lines);
assert_eq!(t[0], "› 1/3 greptile on `src/cost.ts:42`");
assert_eq!(out.lines[1], [(Tone::Markdown, "> Consider batching these writes.".to_string())]);
assert_eq!(t[2], " (•) Post the reply ( ) Don't reply");
assert_eq!(out.lines[2].last(), Some(&(Tone::Cursor, "( ) Don't reply".to_string())));
assert_eq!(out.questions[0], 0..3);
let out = form_lines(&form, &record, Some(&cursor(1, 0)), 40);
assert!(!texts(&out.lines).iter().any(|l| l.contains("Consider")));
}
#[test]
fn text_answers_are_cut_and_multi_line_ones_show_their_first_lines() {
let form = drafts();
let mut record = FormRecord::open("drafts");
record.draft.insert("c-1-text".into(), Answer::Text("a much longer reply than fits here".into()));
record.draft.insert("notes".into(), Answer::Text("1\n2\n3\n4\n5".into()));
let t = texts(&form_lines(&form, &record, None, 20).lines);
assert_eq!(t[3], " a much longer…", "a follow-up: indented, cut to what's left");
assert_eq!(t[5..9], [" 1", " 2", " 3", " … 2 more lines"]);
let c = FormCursor { editing: Some("typed"), ..cursor(1, 0) };
let t = texts(&form_lines(&form, &record, Some(&c), 20).lines);
assert_eq!(t[3], " ✎ typed");
assert_eq!(t.last().unwrap(), "[Enter] keep [Esc] keep [Alt-Enter] newline");
}
#[test]
fn confirming_shows_the_summary_or_what_is_missing() {
let form = drafts();
let mut record = FormRecord::open("drafts");
let c = FormCursor { confirming: true, ..cursor(0, 0) };
let t = texts(&form_lines(&form, &record, Some(&c), 40).lines);
assert_eq!(t[t.len() - 2..], ["missing: Merge after?", "[Esc] back"]);
record.draft.insert("merge".into(), Answer::Confirm(true));
let t = texts(&form_lines(&form, &record, Some(&c), 40).lines);
assert_eq!(t[t.len() - 2..], ["Post replies: submit 3 answers?", "[Enter] yes [Esc] no"]);
record.state = FormState::Withdrawn;
assert_eq!(confirm(&form, &record), Confirm::Closed);
}
#[test]
fn submitted_answers_render_read_only() {
let form = drafts();
let answers: BTreeMap<String, Answer> = [
("c-1".to_string(), Answer::Choice("post".into())),
("c-1-text".to_string(), Answer::Text("line one\n\nline two".into())),
("notes".to_string(), Answer::Text(String::new())),
("merge".to_string(), Answer::Confirm(true)),
]
.into();
let rows = answer_rows(&form, &answers);
assert_eq!(texts(&rows), [
"greptile on `src/cost.ts:42`: Post the reply",
"↳ Reply:",
"line one\n\nline two",
"Anything else?: –",
"Merge after?: Merge",
]);
assert_eq!(rows[2], [(Tone::Markdown, "line one\n\nline two".to_string())]);
let mut skipped = answers.clone();
skipped.insert("c-1".into(), Answer::Choice("skip".into()));
skipped.remove("c-1-text");
let rows = texts(&answer_rows(&form, &skipped));
assert_eq!(rows, ["greptile on `src/cost.ts:42`: Don't reply", "Anything else?: –", "Merge after?: Merge"]);
let mut multi = q("m", "Which", true, choice(true, None));
assert_eq!(answer_text(&multi, Some(&Answer::Choices(vec!["post".into(), "skip".into()]))), "Post the reply, Don't reply");
assert_eq!(answer_text(&multi, Some(&Answer::Choices(vec![]))), "none");
multi.kind = QuestionKind::Confirm { yes: None, no: None, default: None };
assert_eq!(answer_text(&multi, Some(&Answer::Confirm(false))), "No");
}
#[test]
fn a_submission_shows_its_answers_even_without_its_message() {
let answers: BTreeMap<String, Answer> =
[("c-1".to_string(), Answer::Choice("post".into())), ("merge".to_string(), Answer::Confirm(true))].into();
let rows = texts(&submission_lines(Some(&drafts()), &answers, "10:52"));
assert_eq!(rows[0], r#"10:52 you answered "Replies to post" (2 questions)"#);
assert_eq!(rows[1], "greptile on `src/cost.ts:42`: Post the reply");
let mut bare = answers.clone();
bare.insert("notes".into(), Answer::Text("a\nb".into()));
let rows = texts(&submission_lines(None, &bare, "10:52"));
assert_eq!(rows, [r#"10:52 you answered "Form" (3 questions)"#, "c-1: post", "merge: yes", "notes:", "a\nb"]);
}
#[test]
fn pick_picks_toggles_and_confirms() {
let single = q("s", "S", true, choice(false, None));
assert_eq!(pick(&single, None, 1), Some(Answer::Choice("skip".into())));
let multi = q("m", "M", true, choice(true, None));
let a = pick(&multi, None, 1).unwrap();
assert_eq!(a, Answer::Choices(vec!["skip".into()]));
let b = pick(&multi, Some(&a), 0).unwrap();
assert_eq!(b, Answer::Choices(vec!["post".into(), "skip".into()]), "options' order");
assert_eq!(pick(&multi, Some(&b), 1), Some(Answer::Choices(vec!["post".into()])));
let yn = q("c", "C", true, QuestionKind::Confirm { yes: None, no: None, default: None });
assert_eq!(pick(&yn, None, 1), Some(Answer::Confirm(false)));
assert_eq!(pick(&q("t", "T", false, text(None, false)), None, 0), None);
}
#[test]
fn forms_offset_follows_the_focus_only_when_asked() {
assert_eq!(forms_offset(0, Some(8..10), 20, 5, true), 5);
assert_eq!(forms_offset(9, Some(2..4), 20, 5, true), 2);
assert_eq!(forms_offset(0, Some(8..16), 20, 5, true), 8);
assert_eq!(forms_offset(3, Some(8..10), 20, 5, false), 3);
assert_eq!(forms_offset(30, None, 20, 5, false), 15);
}
fn form_app() -> (App, u64) {
let mut app = new_app();
let mut inbox = Inbox::default();
let put = crate::inbox::ThreadPut {
key: "asks".into(),
title: "asks".into(),
state: State::NeedsYou,
compose: Some(Compose::default()),
..Default::default()
};
inbox.put("d-id", "d", 10, put);
let t = &mut inbox.threads[0];
let id = t.id;
let small = Form {
id: "ok".into(),
title: None,
submit: "Submit".into(),
questions: vec![q("go", "Go?", true, QuestionKind::Confirm { yes: None, no: None, default: Some(true) })],
};
for (seq, (msg, form)) in [("run-1", drafts()), ("run-2", small)].into_iter().enumerate() {
let record = FormRecord::open(&form.id);
let blocks = vec![Block::Markdown { text: format!("{msg} text") }, Block::Form(form)];
let kind = ItemKind::Message { id: msg.into(), blocks, edited: false, withdrawn: false, form: Some(record) };
t.feed.push(FeedItem { seq: 100 + seq as u64, at: 20 + seq as u64, kind });
}
app.set_inbox(inbox);
app.on_key(key(KeyCode::Char('4')));
app.on_key(key(KeyCode::Enter));
flush(&mut app);
(app, id)
}
fn flush(app: &mut App) -> Vec<Op> {
let ops = app.take_pending_inbox();
let mut inbox = app.inbox.content_clone();
ops.iter().for_each(|op| inbox.apply(op, 50, "tui"));
app.set_inbox(inbox);
ops
}
fn at(app: &App) -> (String, usize, usize) {
let e = app.inbox.form.as_ref().expect("a form cursor");
(e.message.clone(), e.question, e.option)
}
fn record<'a>(app: &'a App, msg: &str) -> &'a FormRecord {
form_of(app.selected_inbox_thread().unwrap(), msg).unwrap().1
}
#[test]
fn pinned_forms_are_the_open_ones_newest_first() {
let (mut app, _) = form_app();
let t = app.selected_inbox_thread().unwrap();
assert_eq!(pinned_forms(t).iter().map(|p| p.message).collect::<Vec<_>>(), ["run-2", "run-1"]);
let mut inbox = app.inbox.content_clone();
feed::form_record_mut(&mut inbox.threads[0].feed, "run-2").unwrap().withdraw();
app.set_inbox(inbox);
let t = app.selected_inbox_thread().unwrap();
assert_eq!(pinned_forms(t).iter().map(|p| p.message).collect::<Vec<_>>(), ["run-1"]);
}
#[test]
fn tab_in_the_thread_enters_the_form_and_tab_cycles_its_questions() {
let (mut app, _) = form_app();
assert_eq!(app.inbox.focus, InboxFocus::Thread);
app.on_key(key(KeyCode::Tab));
assert_eq!(app.inbox.focus, InboxFocus::Form);
assert_eq!(at(&app), ("run-2".into(), 0, 0));
app.on_key(key(KeyCode::Tab));
assert_eq!(at(&app), ("run-1".into(), 0, 0), "on to the older form");
for want in [1, 2, 3] {
app.on_key(key(KeyCode::Tab));
assert_eq!(at(&app).1, want);
}
app.on_key(key(KeyCode::Tab));
assert_eq!(at(&app), ("run-2".into(), 0, 0), "wraps");
app.on_key(key(KeyCode::BackTab));
assert_eq!(at(&app), ("run-1".into(), 3, 0));
app.on_key(key(KeyCode::Esc));
assert_eq!((app.inbox.focus, app.inbox.form.is_none()), (InboxFocus::Thread, true));
app.on_key(key(KeyCode::Tab));
app.on_key(key(KeyCode::Char('r')));
assert_eq!(app.inbox.focus, InboxFocus::Input);
assert_eq!(app.take_pending_inbox(), []);
}
#[test]
fn without_an_open_form_tab_goes_to_the_composer() {
let (mut app, _) = form_app();
let mut inbox = app.inbox.content_clone();
for m in ["run-1", "run-2"] {
feed::form_record_mut(&mut inbox.threads[0].feed, m).unwrap().withdraw();
}
app.set_inbox(inbox);
app.on_key(key(KeyCode::Tab));
assert_eq!(app.inbox.focus, InboxFocus::Input);
}
#[test]
fn space_picks_and_saves_the_draft() {
let (mut app, id) = form_app();
app.on_key(key(KeyCode::Tab));
app.on_key(key(KeyCode::Tab));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Down));
assert_eq!(at(&app).2, 1, "clamped to the last option");
app.on_key(key(KeyCode::Char(' ')));
let save = |q: &str, a: Answer| Op::SaveDraft { thread: id, message: "run-1".into(), answers: [(q.to_string(), a)].into() };
assert_eq!(record(&app, "run-1").draft.get("c-1"), Some(&Answer::Choice("skip".into())));
assert_eq!(flush(&mut app), [save("c-1", Answer::Choice("skip".into()))]);
app.on_key(key(KeyCode::Char(' ')));
assert_eq!(flush(&mut app), []);
for _ in 0..2 {
app.on_key(key(KeyCode::Tab));
}
app.on_key(key(KeyCode::Right));
app.on_key(key(KeyCode::Char(' ')));
assert_eq!(flush(&mut app), [save("merge", Answer::Confirm(false))]);
app.on_key(key(KeyCode::Tab));
app.on_key(key(KeyCode::Tab));
app.on_key(key(KeyCode::Char('e')));
assert_eq!(app.status.as_deref(), Some("e edits a text question"));
}
#[test]
fn space_toggles_a_multiple_choice() {
let (mut app, id) = form_app();
let mut inbox = app.inbox.content_clone();
for item in &mut inbox.threads[0].feed {
if let ItemKind::Message { id, blocks, .. } = &mut item.kind {
if let ("run-1", Block::Form(f)) = (id.as_str(), &mut blocks[1]) {
f.questions[0].kind = choice(true, None);
}
}
}
app.set_inbox(inbox);
app.on_key(key(KeyCode::Tab));
app.on_key(key(KeyCode::Tab));
app.on_key(key(KeyCode::Char(' ')));
app.on_key(key(KeyCode::Down));
app.on_key(key(KeyCode::Char(' ')));
app.on_key(key(KeyCode::Up));
app.on_key(key(KeyCode::Char(' ')));
let ops = flush(&mut app);
let picks: Vec<&Answer> = ops
.iter()
.map(|op| match op {
Op::SaveDraft { thread, answers, .. } if *thread == id => &answers["c-1"],
op => panic!("{op:?}"),
})
.collect();
assert_eq!(picks, [
&Answer::Choices(vec!["post".into()]),
&Answer::Choices(vec!["post".into(), "skip".into()]),
&Answer::Choices(vec!["skip".into()]),
]);
}
#[test]
fn e_edits_a_text_question_and_enter_or_esc_keeps_it() {
let (mut app, id) = form_app();
app.on_key(key(KeyCode::Tab));
app.on_key(key(KeyCode::Tab));
app.on_key(key(KeyCode::Tab));
assert_eq!(at(&app), ("run-1".into(), 1, 0));
app.on_key(key(KeyCode::Char('e')));
assert_eq!(app.form_text_edit().map(TextArea::input), Some("Already batched"), "opened on the default");
for c in " q".chars() {
app.on_key(key(KeyCode::Char(c)));
}
app.on_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::ALT));
app.on_key(key(KeyCode::Char('x')));
assert!(!app.should_quit);
assert_eq!(flush(&mut app), [], "no save per keystroke");
app.on_key(key(KeyCode::Enter));
assert!(app.form_text_edit().is_none());
assert_eq!(app.inbox.focus, InboxFocus::Form, "still in the form");
let text = Answer::Text("Already batched q\nx".into());
assert_eq!(flush(&mut app), [Op::SaveDraft {
thread: id,
message: "run-1".into(),
answers: [("c-1-text".to_string(), text.clone())].into()
}]);
assert_eq!(record(&app, "run-1").draft["c-1-text"], text);
app.on_key(key(KeyCode::Char('e')));
assert_eq!(app.form_text_edit().map(TextArea::input), Some("Already batched q\nx"));
app.on_key(key(KeyCode::Backspace));
app.on_key(key(KeyCode::Esc));
assert_eq!(app.inbox.focus, InboxFocus::Form);
assert_eq!(record(&app, "run-1").draft["c-1-text"], Answer::Text("Already batched q\n".into()));
flush(&mut app);
app.on_key(key(KeyCode::Char('e')));
for _ in 0..5 {
app.on_key(key(KeyCode::Char('y')));
}
app.on_key(key(KeyCode::Enter));
assert!(app.form_text_edit().is_some());
assert_eq!(app.status.as_deref(), Some("`c-1-text`: longer than 20 bytes"));
app.on_key(key(KeyCode::Backspace));
app.on_key(key(KeyCode::Backspace));
app.on_key(key(KeyCode::Backspace));
app.close_form_edit();
assert_eq!(record(&app, "run-1").draft["c-1-text"], Answer::Text("Already batched q\nyy".into()));
}
#[test]
fn enter_confirms_then_submits_with_the_defaults_resolved() {
let (mut app, id) = form_app();
app.on_key(key(KeyCode::Tab));
app.on_key(key(KeyCode::Tab));
app.on_key(key(KeyCode::Enter));
assert!(app.inbox.form.as_ref().unwrap().confirming);
app.on_key(key(KeyCode::Enter));
assert_eq!((app.inbox.focus, app.take_pending_inbox()), (InboxFocus::Form, vec![]));
let t = app.selected_inbox_thread().unwrap();
let (form, rec) = form_of(t, "run-1").unwrap();
assert_eq!(confirm(form, rec), Confirm::Missing(vec!["Merge after?".into()]));
app.on_key(key(KeyCode::Esc));
assert!(!app.inbox.form.as_ref().unwrap().confirming);
assert_eq!(app.inbox.focus, InboxFocus::Form);
app.on_key(key(KeyCode::BackTab));
app.on_key(key(KeyCode::BackTab));
assert_eq!(at(&app).1, 3);
app.on_key(key(KeyCode::Char(' ')));
app.on_key(key(KeyCode::Enter));
app.on_key(key(KeyCode::Enter));
let ops = app.take_pending_inbox();
let answers: BTreeMap<String, Answer> = [
("c-1".to_string(), Answer::Choice("post".into())),
("c-1-text".to_string(), Answer::Text("Already batched".into())),
("notes".to_string(), Answer::Text(String::new())),
("merge".to_string(), Answer::Confirm(true)),
]
.into();
assert_eq!(ops.last(), Some(&Op::Submit { thread: id, message: "run-1".into(), answers }));
assert_eq!(app.inbox.focus, InboxFocus::Thread);
assert_eq!(app.status.as_deref(), Some("submitted to d"));
let mut inbox = app.inbox.content_clone();
ops.iter().for_each(|op| inbox.apply(op, 60, "tui"));
app.set_inbox(inbox);
let t = app.selected_inbox_thread().unwrap();
assert_eq!(pinned_forms(t).len(), 1);
let feed: Vec<String> = texts(&super::super::pane_feed(t, 0));
let head = feed.iter().position(|l| l.ends_with(r#" you answered "Replies to post" (3 questions)"#)).expect("submission");
assert_eq!(feed[head + 1..head + 5], ["greptile on `src/cost.ts:42`: Post the reply", "↳ Reply:", "Already batched", "Anything else?: –"]);
assert_eq!(feed[head + 5], "Merge after?: Merge");
let form_row = feed.iter().position(|l| l.starts_with("form: Replies to post · submitted ")).unwrap();
assert!(form_row > head && feed[form_row].ends_with(", answers above"), "{feed:#?}");
assert_eq!(feed.iter().filter(|l| *l == "Merge after?: Merge").count(), 1, "answers once, under the submission");
assert!(feed.iter().any(|l| l == "form: Form · open, pinned above"), "{feed:#?}");
}
#[test]
fn a_form_closed_elsewhere_leaves_the_zone() {
let (mut app, _) = form_app();
app.on_key(key(KeyCode::Tab));
let mut inbox = app.inbox.content_clone();
feed::form_record_mut(&mut inbox.threads[0].feed, "run-2").unwrap().withdraw();
app.set_inbox(inbox);
assert_eq!(app.inbox.focus, InboxFocus::Thread);
assert_eq!(app.status.as_deref(), Some("form is no longer open"));
}
#[test]
fn archived_threads_refuse_the_form() {
let (mut app, _) = form_app();
let mut inbox = app.inbox.content_clone();
inbox.threads[0].archived = true;
app.set_inbox(inbox);
app.on_key(key(KeyCode::Tab));
assert_eq!(app.inbox.focus, InboxFocus::Thread);
assert!(app.status.as_deref().is_some_and(|s| s.starts_with("archived")));
}
}