firestore-cli 0.2.0

fsq, a command line client for Google Firestore: a SQL REPL with tables, query plans and index statements, and a scripting mode with JSON output
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
use super::footer::{Hint, Hints};
use super::{PopupScroll, Viewport};
use crate::browse::state::{Browser, Popup};
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::FirestoreVector;
use ratatui::layout::{Constraint, Rect};
use ratatui::style::{Style, Stylize};
use ratatui::text::{Line, Span};
use ratatui::widgets::{Block, Clear, Paragraph};
use ratatui::Frame;
use std::borrow::Cow;

/// The columns a popup leaves free of the panes on either side of it.
const POPUP_MARGIN: usize = 2;

/// The columns of the offset that starts each line of bytes in hexadecimal.
const HEX_OFFSET_WIDTH: usize = 8;

/// The most bytes a line of hexadecimal holds, when the popup has room for them.
const HEX_BYTES_PER_LINE: usize = 16;

/// The columns a tab in a value takes in a popup.
const POPUP_TAB: &str = "    ";

impl Browser {
    /// Draws `popup` over `body`, the area of the panes: a box as wide as its text and its
    /// title, inside the panes, with its text wrapped to the box and scrolled to where
    /// `viewport` keeps it.
    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 most = usize::from(body.width).saturating_sub(2 * POPUP_MARGIN + 2);
        let inner_width = text.natural_width().max(title.width()).min(most);
        let lines = text.lines(inner_width);
        let rows = lines.len().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)),
        );
        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 mut hints = vec![Hint::new("Esc", "close", 3)];
        if let Popup::Value { .. } = popup {
            hints.insert(0, Hint::new("y", "copy", 2));
        }
        if last_top > 0 {
            hints.insert(0, Hint::new("↑↓", "scroll", 1));
        }
        let mut hints = Hints(hints).fitted(inner_width.saturating_sub(2)).line();
        hints.spans.insert(0, Span::from(" "));
        hints.spans.push(Span::from(" "));
        let block = Block::bordered()
            .title(title.line(inner_width))
            .title_bottom(hints)
            .border_style(Style::new().cyan());
        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);
    }
}

/// The title of a popup: its name, cut in the middle when the title is wider than the popup, and
/// the details after it, a `·` before each, with a space at either end.
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()
    }

    /// This title in `width` columns.
    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)))
    }
}

/// How much `value` holds, for the types whose value does not say it at a glance.
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,
    }
}

/// The text of a popup, which it lays out to the width of the popup.
enum PopupText<'a> {
    /// Text wrapped at words, each of its lines on a line of its own.
    Text(String),
    /// Bytes in hexadecimal, after the offset of the first byte of each line.
    Hex(&'a [u8]),
    /// Every key of the browser, by where it works.
    Help,
}

impl<'a> PopupText<'a> {
    /// A value whole: bytes in hexadecimal, and any other value as [`View::pretty_value`] writes
    /// it, a map or an array as indented JSON-like text.
    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)),
        }
    }

    /// The columns the widest line takes, unwrapped.
    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)
            }
        }
    }

    /// The lines of the text in `width` columns.
    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);
                bytes
                    .chunks(per_line)
                    .enumerate()
                    .map(|(line, chunk)| {
                        let hex: Vec<String> =
                            chunk.iter().map(|byte| format!("{byte:02x}")).collect();
                        Line::from(format!(
                            "{:0width$x}  {}",
                            line * per_line,
                            hex.join(" "),
                            width = HEX_OFFSET_WIDTH
                        ))
                    })
                    .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
            }
        }
    }
}

/// The lines of `text` as a popup shows them: a tab as spaces and any other control character
/// as one space, since the terminal would act on it.
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)
        }
    })
}

/// The columns of a line of `count` bytes in hexadecimal, after its offset.
fn hex_line_width(count: usize) -> usize {
    HEX_OFFSET_WIDTH + 2 + (3 * count).saturating_sub(1)
}

/// The keys of one place of the browser, and what each does there.
struct HelpSection {
    title: &'static str,
    keys: &'static [HelpKey],
}

struct HelpKey {
    keys: &'static str,
    action: &'static str,
}

const HELP_INDENT: usize = 2;

/// The widest the help gets, so that its longer lines wrap rather than run across a wide
/// terminal.
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: "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 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: "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: "Esc",
                action: "close the popup",
            },
        ],
    },
];

fn help_keys() -> impl Iterator<Item = &'static HelpKey> {
    HELP_SECTIONS.iter().flat_map(|section| section.keys)
}

/// The column the actions of the help start at, two after the longest 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
}