use super::footer::{Hint, Hints};
use super::{PopupScroll, Viewport, CURSOR_BAR, SELECTION_MARKER};
use crate::browse::external::ExternalDraft;
use crate::browse::hex::{hex_lines, HEX_BYTES_PER_LINE, HEX_OFFSET_WIDTH};
use crate::browse::new_field::FieldType;
use crate::browse::state::{Browser, EditorChoice, Popup};
use crate::browse::tree::{Listing, MaskPath};
use crate::browse::write::{
Change, Confirmation, Edit, Halt, Note, PartialDelete, Stage, ValueChange,
};
use crate::errors::FsqError;
use crate::field_path::FieldPath;
use crate::render::{counted, cut_middle, type_name, View};
use firestore::gcloud_sdk::google::firestore::v1::value::ValueType;
use firestore::gcloud_sdk::google::firestore::v1::Value;
use firestore::{FirestoreDb, FirestoreVector};
use ratatui::layout::{Constraint, Rect};
use ratatui::style::{Style, Stylize};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Clear, HighlightSpacing, List, ListState, Paragraph};
use ratatui::Frame;
use similar::{ChangeTag, TextDiff};
use std::borrow::Cow;
use std::time::Duration;
const CONFIRMATION_DIFF_TIMEOUT: Duration = Duration::from_millis(50);
const POPUP_MARGIN: usize = 2;
const POPUP_TAB: &str = " ";
const POPUP_NOTE_WIDTH: usize = 60;
impl Browser {
pub(super) fn draw_popup(
&self,
frame: &mut Frame,
popup: &Popup,
body: Rect,
viewport: &mut Viewport,
) {
let view = self.value_view();
let (title, text) = match popup {
Popup::Value { path, value } => {
let details = [Some(type_name(value).to_string()), size_detail(value)];
let title = PopupTitle {
name: FieldPath::from(path).to_string(),
details: details.into_iter().flatten().collect(),
};
(title, PopupText::of(value, &view))
}
Popup::Error(failure) => {
let error = self.error(*failure).map(ToString::to_string);
(
PopupTitle::named("error"),
PopupText::Text(error.unwrap_or_default()),
)
}
Popup::Help => (PopupTitle::named("keys"), PopupText::Help),
};
let mut hints = Vec::new();
if let Popup::Value { .. } = popup {
hints.push(Hint::new("y", "copy", 2));
}
if self.edits_at_cursor(EditorChoice::Fitting) {
hints.push(Hint::new("e", "edit", 1));
}
if self.new_field_parent().is_some() {
hints.push(Hint::new("a", "add", 1));
}
hints.push(Hint::new("Esc", "close", 3));
draw_box(frame, body, viewport, title, text, hints);
}
pub(super) fn draw_confirmation(
&self,
frame: &mut Frame,
edit: &Edit,
confirmation: &Confirmation,
body: Rect,
viewport: &mut Viewport,
) {
let change = confirmation.change();
let mut details = Vec::new();
let action = match change {
Change::Field { path, value } => {
details.push(FieldPath::from(path).to_string());
value.as_ref().map_or("delete", type_name)
}
Change::Document(None) => "delete",
Change::Document(Some(_)) if edit.last_read().is_some() => "replace",
Change::Document(Some(_)) | Change::NewDocument { .. } => "new",
};
details.push(action.to_string());
let title = PopupTitle {
name: self.edited_document_path(edit),
details,
};
let view = self.value_view();
let mut lines = edit
.value_change()
.map(|values| values.lines(&view))
.unwrap_or_default();
let deletes_document = matches!(change, Change::Document(None));
let recursive_delete_started = matches!(
confirmation.stage(),
Stage::DeletingRecursively(_) | Stage::DeletedPartly(_)
);
if deletes_document && !recursive_delete_started {
let listing = self.level().tree().listing();
lines.push(MarkedLine::blank());
lines.extend(listing.confirmation_lines(self.offers_recursive_delete()));
}
let stage_lines = match confirmation.stage() {
_ if edit.is_gone() => vec![MarkedLine::note(
"the document no longer exists",
Style::new().red(),
)],
Stage::Asking(None) => Vec::new(),
Stage::Asking(Some(Note::Changed)) => vec![MarkedLine::note(
"nothing was written: the document changed since it was read, and the diff is \
against what it holds now",
Style::new().yellow(),
)],
Stage::Asking(Some(Note::WriteStopped)) => vec![MarkedLine::note(
"stopped waiting: the write may have reached Firestore, and y tries again",
Style::new().yellow(),
)],
Stage::Asking(Some(Note::ReadStopped)) => vec![MarkedLine::note(
"stopped reading the document again, and y tries the write again",
Style::new().yellow(),
)],
Stage::Asking(Some(Note::Exists)) => vec![MarkedLine::note(
"nothing was written: a document of this name exists, and e gives the new one \
another",
Style::new().red(),
)],
Stage::Writing(_) => vec![MarkedLine::note("writing…", Style::new().yellow())],
Stage::DeletingRecursively(progress) => {
let deleted = counted(progress.deleted, "document", "documents");
let text = if progress.stopping {
format!("stopping after the request in flight… {deleted} deleted so far")
} else {
format!("deleting… {deleted} deleted so far")
};
vec![MarkedLine::note(&text, Style::new().yellow())]
}
Stage::DeletedPartly(partial) => partial.lines(),
Stage::Reading(_) => vec![MarkedLine::note(
"reading the document again…",
Style::new().yellow(),
)],
Stage::Failed(error) => vec![
MarkedLine::note(&format!("error: {error}"), Style::new().red()),
MarkedLine::note("y tries again", Style::new().dim()),
],
};
if !stage_lines.is_empty() {
lines.push(MarkedLine::blank());
lines.extend(stage_lines);
}
let mut hints = Vec::new();
if edit.is_waiting() {
let stopping = matches!(
confirmation.stage(),
Stage::DeletingRecursively(progress) if progress.stopping
);
let action = if stopping { "stop waiting" } else { "stop" };
hints.push(Hint::new("Ctrl-C", action, 4));
} else if edit.is_gone() || recursive_delete_started {
if confirmation.has_draft() {
hints.push(Hint::new("e", "edit", 2));
}
hints.push(Hint::new("n", "close", 4));
} else {
let deletes = matches!(
change,
Change::Field { value: None, .. } | Change::Document(None)
);
if !self.waits_for_listing() && !edit.exists_already() {
hints.push(Hint::new("y", if deletes { "delete" } else { "write" }, 4));
}
if self.offers_recursive_delete() {
hints.push(Hint::new("R", "delete all", 3));
}
if confirmation.has_draft() {
hints.push(Hint::new("e", "edit", 2));
}
hints.push(Hint::new("n", "discard", 3));
}
draw_box(
frame,
body,
viewport,
title,
PopupText::Marked(lines),
hints,
);
}
pub(super) fn draw_rejected_text(
&self,
frame: &mut Frame,
edit: &Edit,
draft: &ExternalDraft,
error: &FsqError,
body: Rect,
viewport: &mut Viewport,
) {
let title = PopupTitle {
name: self.edited_document_path(edit),
details: draft
.path()
.map(|path| FieldPath::from(path).to_string())
.into_iter()
.collect(),
};
let lines = vec![
MarkedLine::note(&format!("error: {error}"), Style::new().red()),
MarkedLine::blank(),
MarkedLine::note("nothing was written", Style::new().dim()),
];
let hints = vec![Hint::new("e", "edit", 2), Hint::new("n", "discard", 3)];
draw_box(
frame,
body,
viewport,
title,
PopupText::Marked(lines),
hints,
);
}
pub(super) fn draw_type_list(
&self,
frame: &mut Frame,
path: &MaskPath,
selected: FieldType,
body: Rect,
viewport: &mut Viewport,
) {
let title = PopupTitle {
name: FieldPath::from(path).to_string(),
details: vec!["type".to_string()],
};
let hints = vec![
Hint::new("↑↓", "move", 1),
Hint::new("Enter", "pick", 3),
Hint::new("Esc", "cancel", 2),
];
let names: Vec<String> = FieldType::ALL.iter().map(ToString::to_string).collect();
let marker = Span::from(SELECTION_MARKER).width();
let widest = names
.iter()
.map(|name| marker + Span::from(name.as_str()).width())
.max()
.unwrap_or_default();
let inner_width = box_width(body, widest, &title, &hints);
let (area, rows) = box_area(body, inner_width, names.len());
viewport.page_rows = rows;
let position = FieldType::ALL.iter().position(|listed| *listed == selected);
let list = List::new(names)
.block(box_block(&title, hints, inner_width))
.highlight_style(CURSOR_BAR)
.highlight_symbol(SELECTION_MARKER)
.highlight_spacing(HighlightSpacing::Always);
frame.render_widget(Clear, area);
frame.render_stateful_widget(
list,
area,
&mut ListState::default().with_selected(position),
);
}
fn edited_document_path(&self, edit: &Edit) -> String {
match edit.document() {
Some(name) => {
FirestoreDb::relative_document_name(self.documents_path(), name).to_string()
}
None => self.level().path().to_string(),
}
}
}
fn draw_box(
frame: &mut Frame,
body: Rect,
viewport: &mut Viewport,
title: PopupTitle,
text: PopupText,
mut hints: Vec<Hint>,
) {
let mut inner_width = box_width(body, text.natural_width(), &title, &hints);
let mut lines = text.lines(inner_width);
let mut rows = box_area(body, inner_width, lines.len()).1;
if rows < lines.len() {
hints.insert(0, Hint::new("↑↓", "scroll", 1));
inner_width = box_width(body, text.natural_width(), &title, &hints);
lines = text.lines(inner_width);
}
let area;
(area, rows) = box_area(body, inner_width, lines.len());
let last_top = lines.len() - rows;
let scroll = viewport.popup.get_or_insert_with(PopupScroll::default);
scroll.top = scroll.top.min(last_top);
scroll.last_top = last_top;
let top = scroll.top;
viewport.page_rows = rows;
let block = box_block(&title, hints, inner_width);
let shown: Vec<Line> = lines.into_iter().skip(top).take(rows).collect();
frame.render_widget(Clear, area);
frame.render_widget(Paragraph::new(shown).block(block), area);
}
fn box_width(body: Rect, natural_width: usize, title: &PopupTitle, hints: &[Hint]) -> usize {
let most = usize::from(body.width).saturating_sub(2 * POPUP_MARGIN + 2);
let hints_width = usize::from(Hints(hints.to_vec()).columns()) + 2;
natural_width.max(title.width()).max(hints_width).min(most)
}
fn box_area(body: Rect, inner_width: usize, lines: usize) -> (Rect, usize) {
let rows = lines.min(usize::from(body.height).saturating_sub(2));
let area = body.centered(
Constraint::Length(u16::try_from(inner_width + 2).unwrap_or(u16::MAX)),
Constraint::Length(u16::try_from(rows + 2).unwrap_or(u16::MAX)),
);
(area, rows)
}
fn box_block(title: &PopupTitle, hints: Vec<Hint>, inner_width: usize) -> Block<'static> {
let mut hints = Hints(hints).fitted(inner_width.saturating_sub(2)).line();
hints.spans.insert(0, Span::from(" "));
hints.spans.push(Span::from(" "));
Block::bordered()
.title(title.line(inner_width))
.title_bottom(hints)
.border_style(Style::new().cyan())
}
impl ValueChange {
fn lines(&self, view: &View<'_>) -> Vec<MarkedLine> {
if self.changes_nothing() {
return vec![MarkedLine::note("no change", Style::new().dim())];
}
let [before, after] = self.texts(view);
TextDiff::configure()
.timeout(CONFIRMATION_DIFF_TIMEOUT)
.diff_lines(&before, &after)
.iter_all_changes()
.map(|line| {
let (marker, style) = match line.tag() {
ChangeTag::Equal => (" ", Style::new().dim()),
ChangeTag::Delete => ("- ", Style::new().red()),
ChangeTag::Insert => ("+ ", Style::new().green()),
};
MarkedLine {
marker,
text: line.value().trim_end_matches(['\r', '\n']).to_string(),
style,
}
})
.collect()
}
fn texts(&self, view: &View<'_>) -> [String; 2] {
let values = [&self.before, &self.after];
let shown = values.map(|value| value.as_ref().map(|value| value_text(value, view)));
let retyped = matches!(
(&self.before, &self.after),
(Some(before), Some(after)) if type_name(before) != type_name(after)
);
let blank = shown.iter().flatten().any(String::is_empty);
let [before, after] = shown.map(Option::unwrap_or_default);
if retyped || blank || before.split_whitespace().eq(after.split_whitespace()) {
return values.map(|value| {
value
.as_ref()
.map(|value| format!("{:#}", view.tagged_json(value)))
.unwrap_or_default()
});
}
[before, after]
}
}
fn value_text(value: &Value, view: &View<'_>) -> String {
match &value.value_type {
Some(ValueType::BytesValue(bytes)) => hex_lines(bytes, HEX_BYTES_PER_LINE).join("\n"),
_ => view.pretty_value(value),
}
}
impl Listing {
fn confirmation_lines(&self, offers_recursive: bool) -> Vec<MarkedLine> {
match self {
Self::Listed(collection_ids) if collection_ids.is_empty() => {
vec![MarkedLine::note("no subcollections", Style::new().dim())]
}
Self::Listed(collection_ids) => {
let names: Vec<String> = collection_ids.iter().map(ToString::to_string).collect();
let names = names.join(", ");
if offers_recursive {
vec![
MarkedLine::note(
&format!("y leaves the subcollections: {names}"),
Style::new().yellow(),
),
MarkedLine::note(
"R deletes them too, with every document under them",
Style::new().yellow(),
),
]
} else {
vec![MarkedLine::note(
&format!("the subcollections stay: {names}"),
Style::new().yellow(),
)]
}
}
Self::Pending(_) => vec![MarkedLine::note(
"listing the subcollections…",
Style::new().dim(),
)],
Self::Failed(error) => vec![MarkedLine::note(
&format!("the subcollections could not be listed: {error}"),
Style::new().red(),
)],
Self::NotListed => vec![MarkedLine::note(
"the subcollections were not listed",
Style::new().dim(),
)],
}
}
}
impl PartialDelete {
fn lines(&self) -> Vec<MarkedLine> {
let deleted = counted(self.deleted, "document", "documents");
let stopped = format!(
"stopped after deleting {deleted} under it; the document stays, with the rest under it"
);
match &self.halt {
Halt::Stopped => vec![MarkedLine::note(&stopped, Style::new().yellow())],
Halt::Stale => vec![MarkedLine::note(
&format!(
"deleted {deleted} under it; the document stays, since it changed after it \
was read"
),
Style::new().yellow(),
)],
Halt::Failed(error) => vec![
MarkedLine::note(&format!("error: {error}"), Style::new().red()),
MarkedLine::note(
&format!("deleted {deleted} under it; the document stays"),
Style::new().dim(),
),
],
Halt::Abandoned => vec![MarkedLine::note(
&format!(
"stopped waiting after deleting {deleted} under it; the request in flight may \
still delete more, the document among them, and r shows what stays"
),
Style::new().yellow(),
)],
}
}
}
struct PopupTitle {
name: String,
details: Vec<String>,
}
impl PopupTitle {
fn named(name: &str) -> Self {
Self {
name: name.to_string(),
details: Vec::new(),
}
}
fn tail(&self) -> String {
self.details
.iter()
.map(|detail| format!(" · {detail}"))
.collect::<String>()
+ " "
}
fn width(&self) -> usize {
1 + Span::from(self.name.as_str()).width() + Span::from(self.tail()).width()
}
fn line(&self, width: usize) -> Line<'static> {
let tail = self.tail();
let room = width.saturating_sub(1 + Span::from(tail.as_str()).width());
Line::from(format!(" {}{tail}", cut_middle(&self.name, room)))
}
}
fn size_detail(value: &Value) -> Option<String> {
match &value.value_type {
Some(ValueType::StringValue(text)) => Some(counted(text.chars().count(), "char", "chars")),
Some(ValueType::BytesValue(bytes)) => Some(counted(bytes.len(), "byte", "bytes")),
Some(ValueType::ArrayValue(array)) => Some(counted(array.values.len(), "item", "items")),
Some(ValueType::MapValue(map)) => Some(match FirestoreVector::from_value(value) {
Ok(vector) => counted(vector.as_vec().len(), "dimension", "dimensions"),
Err(_) => counted(map.fields.len(), "field", "fields"),
}),
_ => None,
}
}
enum PopupText<'a> {
Text(String),
Hex(&'a [u8]),
Help,
Marked(Vec<MarkedLine>),
}
struct MarkedLine {
marker: &'static str,
text: String,
style: Style,
}
impl MarkedLine {
fn note(text: &str, style: Style) -> Self {
Self {
marker: "",
text: text.to_string(),
style,
}
}
fn blank() -> Self {
Self::note("", Style::new())
}
}
impl<'a> PopupText<'a> {
fn of(value: &'a Value, view: &View<'_>) -> Self {
match &value.value_type {
Some(ValueType::BytesValue(bytes)) => Self::Hex(bytes),
_ => Self::Text(view.pretty_value(value)),
}
}
fn natural_width(&self) -> usize {
match self {
Self::Text(text) => text_lines(text)
.map(|line| Span::from(line.as_ref()).width())
.max()
.unwrap_or_default(),
Self::Hex(bytes) => hex_line_width(bytes.len().min(HEX_BYTES_PER_LINE)),
Self::Help => {
let action_column = help_action_column();
help_keys()
.map(|key| action_column + Span::from(key.action).width())
.max()
.unwrap_or_default()
.min(HELP_MAX_WIDTH)
}
Self::Marked(lines) => lines
.iter()
.flat_map(|line| {
let marker = Span::from(line.marker).width();
let most = if line.marker.is_empty() {
POPUP_NOTE_WIDTH
} else {
usize::MAX
};
text_lines(&line.text)
.map(move |text| (marker + Span::from(text.as_ref()).width()).min(most))
})
.max()
.unwrap_or_default(),
}
}
fn lines(&self, width: usize) -> Vec<Line<'static>> {
let width = width.max(1);
match self {
Self::Text(text) => text_lines(text)
.flat_map(|line| {
textwrap::wrap(&line, width)
.into_iter()
.map(|wrapped| Line::from(wrapped.into_owned()))
.collect::<Vec<_>>()
})
.collect(),
Self::Hex(bytes) => {
let per_line = [HEX_BYTES_PER_LINE, 8, 4]
.into_iter()
.find(|&count| hex_line_width(count) <= width)
.unwrap_or(4);
hex_lines(bytes, per_line)
.into_iter()
.map(Line::from)
.collect()
}
Self::Marked(lines) => lines
.iter()
.flat_map(|line| {
let indent = " ".repeat(Span::from(line.marker).width());
let room = width.saturating_sub(indent.len()).max(1);
let mut wrapped: Vec<Line<'static>> = text_lines(&line.text)
.flat_map(|text| {
textwrap::wrap(&text, room)
.into_iter()
.map(Cow::into_owned)
.collect::<Vec<_>>()
})
.enumerate()
.map(|(position, text)| {
let lead = if position == 0 { line.marker } else { &indent };
Line::from(format!("{lead}{text}")).style(line.style)
})
.collect();
if wrapped.is_empty() {
wrapped.push(Line::from(line.marker).style(line.style));
}
wrapped
})
.collect(),
Self::Help => {
let action_column = help_action_column();
let indent = " ".repeat(action_column);
let options = textwrap::Options::new(width).subsequent_indent(&indent);
let mut lines = Vec::new();
for (position, section) in HELP_SECTIONS.iter().enumerate() {
if position > 0 {
lines.push(Line::default());
}
lines.push(Line::from(section.title).bold());
for key in section.keys {
let padding = action_column - HELP_INDENT - Span::from(key.keys).width();
let line = format!(
"{}{}{}{}",
" ".repeat(HELP_INDENT),
key.keys,
" ".repeat(padding),
key.action
);
lines.extend(
textwrap::wrap(&line, &options)
.into_iter()
.map(|wrapped| Line::from(wrapped.into_owned())),
);
}
}
lines
}
}
}
}
fn text_lines(text: &str) -> impl Iterator<Item = Cow<'_, str>> {
text.lines().map(|line| {
if line.contains(char::is_control) {
Cow::Owned(line.replace('\t', POPUP_TAB).replace(char::is_control, " "))
} else {
Cow::Borrowed(line)
}
})
}
fn hex_line_width(count: usize) -> usize {
HEX_OFFSET_WIDTH + 2 + (3 * count).saturating_sub(1)
}
struct HelpSection {
title: &'static str,
keys: &'static [HelpKey],
}
struct HelpKey {
keys: &'static str,
action: &'static str,
}
const HELP_INDENT: usize = 2;
const HELP_MAX_WIDTH: usize = 80;
const HELP_SECTIONS: &[HelpSection] = &[
HelpSection {
title: "Documents and tree",
keys: &[
HelpKey {
keys: "↑ ↓ k j",
action: "move the cursor",
},
HelpKey {
keys: "PgUp PgDn",
action: "move a page",
},
HelpKey {
keys: "Ctrl-U Ctrl-D",
action: "move half a page",
},
HelpKey {
keys: "g G Home End",
action: "go to the first or the last row",
},
HelpKey {
keys: "→ l",
action: "open the map, array or subcollection; from the documents, go to the tree",
},
HelpKey {
keys: "Enter",
action: "show the value whole, or open what → opens",
},
HelpKey {
keys: "Space",
action: "open or close the map, array or subcollections",
},
HelpKey {
keys: "← h",
action: "close the map or array, or go to the one it is in; from the top of the tree, go to the documents; from the documents, go up a level",
},
HelpKey {
keys: "Tab",
action: "switch between the documents and the tree",
},
HelpKey {
keys: "/",
action: "edit the filter",
},
HelpKey {
keys: "f",
action: "open the editor with a filter on the value under the cursor, WHERE <path> = <value>",
},
HelpKey {
keys: "y Y",
action: "copy the value under the cursor, or its path, to the clipboard",
},
HelpKey {
keys: "e",
action: "edit the value under the cursor on one line, or in $VISUAL or $EDITOR when it does not fit, then confirm the change; from the documents, the document",
},
HelpKey {
keys: "E",
action: "edit the value in $VISUAL or $EDITOR as tagged JSON",
},
HelpKey {
keys: "d",
action: "delete the field under the cursor, or from the documents, the document, once confirmed, and with R every document under it too; an element of an array is not deleted alone",
},
HelpKey {
keys: "a",
action: "add a field beside the one under the cursor, or inside the open map under it: its name, its type, then its value; not in an array",
},
HelpKey {
keys: "n",
action: "from the documents, create a document in $VISUAL or $EDITOR",
},
HelpKey {
keys: "D",
action: "from the documents, copy the document under a new ID",
},
HelpKey {
keys: "r",
action: "read the documents again with their filter",
},
HelpKey {
keys: "Esc Backspace",
action: "go back up from a subcollection",
},
HelpKey {
keys: "Ctrl-G",
action: "show the whole error of a failed filter or listing",
},
HelpKey {
keys: "?",
action: "show these keys",
},
HelpKey {
keys: "Ctrl-C",
action: "stop waiting for Firestore, else close the popup, the change or the editor, else quit",
},
HelpKey {
keys: "q",
action: "quit",
},
],
},
HelpSection {
title: "Filter editor",
keys: &[
HelpKey {
keys: "← → Home End",
action: "move the cursor",
},
HelpKey {
keys: "↑ ↓",
action: "recall an earlier filter that ran, or a later one",
},
HelpKey {
keys: "Backspace Delete",
action: "delete the character before or after the cursor",
},
HelpKey {
keys: "Ctrl-W Ctrl-U",
action: "delete the word before the cursor, or everything before it",
},
HelpKey {
keys: "Enter",
action: "run the filter",
},
HelpKey {
keys: "Esc",
action: "close the editor and keep the filter before",
},
HelpKey {
keys: "Ctrl-G",
action: "show the whole error of the filter",
},
],
},
HelpSection {
title: "Value editor",
keys: &[
HelpKey {
keys: "Enter",
action: "show the change, to confirm it, or the error of a text that does not read as a value of the field's type",
},
HelpKey {
keys: "Esc",
action: "close the editor and change nothing",
},
HelpKey {
keys: "other keys",
action: "edit the line as in the filter editor",
},
],
},
HelpSection {
title: "New field",
keys: &[
HelpKey {
keys: "Enter",
action: "go on from the name to the list of types, and from the type to the value",
},
HelpKey {
keys: "↑ ↓ k j",
action: "choose the type",
},
HelpKey {
keys: "Esc",
action: "drop the field and write nothing",
},
HelpKey {
keys: "other keys",
action: "edit the name as in the filter editor",
},
],
},
HelpSection {
title: "Confirmation",
keys: &[
HelpKey {
keys: "y",
action: "write the change, if the document is still as it was read; else read it again and show the change over it; a new document, if none has its name",
},
HelpKey {
keys: "R",
action: "delete the document and every document under it, at any depth: in batches of 500, each after the documents under it, and the document last; only for a document with subcollections",
},
HelpKey {
keys: "e",
action: "go back to the editor with the text as it was left",
},
HelpKey {
keys: "n Esc",
action: "discard the change",
},
HelpKey {
keys: "Ctrl-C",
action: "stop waiting for the write, or stop a delete with R once its batch in flight is answered and, a second time, stop waiting for that batch, else discard the change",
},
],
},
HelpSection {
title: "Text from $VISUAL or $EDITOR that does not read",
keys: &[
HelpKey {
keys: "e",
action: "open the editor again on the text as it was saved",
},
HelpKey {
keys: "n Esc",
action: "discard the change",
},
],
},
HelpSection {
title: "Popup",
keys: &[
HelpKey {
keys: "↑ ↓ k j",
action: "scroll a line",
},
HelpKey {
keys: "PgUp PgDn",
action: "scroll a page",
},
HelpKey {
keys: "Ctrl-U Ctrl-D",
action: "scroll half a page",
},
HelpKey {
keys: "g G Home End",
action: "go to the start or the end",
},
HelpKey {
keys: "y Y",
action: "copy the value, or its path, to the clipboard",
},
HelpKey {
keys: "e E",
action: "edit the value, as e and E do in the tree",
},
HelpKey {
keys: "d",
action: "delete the field",
},
HelpKey {
keys: "a",
action: "add a field to the map, or beside any other value",
},
HelpKey {
keys: "Esc",
action: "close the popup",
},
],
},
];
fn help_keys() -> impl Iterator<Item = &'static HelpKey> {
HELP_SECTIONS.iter().flat_map(|section| section.keys)
}
fn help_action_column() -> usize {
let keys = help_keys()
.map(|key| Span::from(key.keys).width())
.max()
.unwrap_or_default();
HELP_INDENT + keys + 2
}