ghtui 0.2.0

A keyboard-driven terminal client for GitHub, with a pull request diff viewer that goes further than GitHub's
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//! Just enough YAML for issue templates: their front matter, issue forms
//! and `config.yml`. Block mappings and sequences by indentation, flow
//! lists (`[a, b]`) and maps, quoted and plain scalars, `|` and `>` block
//! scalars, and comments. Anchors, tags and multi-line plain scalars
//! aren't templates' YAML; what isn't understood reads as text or nothing.

/// A YAML value. Scalars stay text: a template's `true` is read as it's
/// used.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Value {
    Null,
    Text(String),
    List(Vec<Value>),
    Map(Vec<(String, Value)>),
}

impl Value {
    /// The value at `key` of a map.
    pub fn get(&self, key: &str) -> Option<&Value> {
        match self {
            Value::Map(entries) => entries.iter().find(|(k, _)| k == key).map(|(_, v)| v),
            _ => None,
        }
    }

    pub fn text(&self) -> Option<&str> {
        match self {
            Value::Text(text) => Some(text),
            _ => None,
        }
    }

    pub fn list(&self) -> &[Value] {
        match self {
            Value::List(items) => items,
            _ => &[],
        }
    }

    /// Names given either way GitHub takes them: a list, or one string of
    /// names between commas.
    pub fn names(&self) -> Vec<String> {
        let names: Vec<String> = match self {
            Value::Text(text) => text.split(',').map(str::to_owned).collect(),
            Value::List(items) => items
                .iter()
                .filter_map(Value::text)
                .map(str::to_owned)
                .collect(),
            _ => Vec::new(),
        };
        (names.into_iter())
            .map(|n| n.trim().to_owned())
            .filter(|n| !n.is_empty())
            .collect()
    }

    /// `false` (or `no`) said in so many words.
    pub fn is_false(&self) -> bool {
        matches!(self.text(), Some("false" | "False" | "FALSE" | "no" | "No"))
    }
}

/// Parses a YAML document.
pub fn parse(text: &str) -> Value {
    let mut lines: Vec<String> = text
        .lines()
        .map(|l| l.trim_end_matches('\r').to_owned())
        .collect();
    // A document start marker opens it; an end marker closes it.
    if lines.first().is_some_and(|l| l.trim_end() == "---") {
        lines.remove(0);
    }
    if let Some(end) = lines.iter().position(|l| l.trim_end() == "...") {
        lines.truncate(end);
    }
    let mut parser = Parser { lines, at: 0 };
    parser.block(0)
}

struct Parser {
    lines: Vec<String>,
    at: usize,
}

/// Columns of indentation.
fn indent(line: &str) -> usize {
    line.len() - line.trim_start_matches(' ').len()
}

/// Whether a line holds nothing: blank, or only a comment.
fn empty(line: &str) -> bool {
    let t = line.trim();
    t.is_empty() || t.starts_with('#')
}

/// The line without its comment (a `#` after a space, outside quotes).
fn uncommented(line: &str) -> &str {
    let mut quote = None;
    let mut previous = ' ';
    for (i, c) in line.char_indices() {
        match (quote, c) {
            (None, '"' | '\'') if previous == ' ' || previous == '[' || previous == ',' => {
                quote = Some(c);
            }
            (Some(q), c) if c == q => quote = None,
            (None, '#') if previous == ' ' || previous == '\t' => {
                return line.get(..i).unwrap_or(line).trim_end();
            }
            _ => {}
        }
        previous = c;
    }
    line.trim_end()
}

/// A sequence item's dash: `-` alone or before a space.
fn dash(content: &str) -> Option<&str> {
    match content.strip_prefix('-')? {
        "" => Some(""),
        rest if rest.starts_with(' ') => Some(rest),
        _ => None,
    }
}

/// `key: value` split at its colon (outside quotes): the key, unquoted,
/// and what follows.
fn key_value(content: &str) -> Option<(String, &str)> {
    let first = content.chars().next()?;
    if first == '"' || first == '\'' {
        let close = content.get(1..)?.find(first)? + 1;
        let key = content.get(1..close)?;
        let rest = content.get(close + 1..)?.trim_start();
        let rest = rest.strip_prefix(':')?;
        return (rest.is_empty() || rest.starts_with(' ')).then(|| (key.to_owned(), rest.trim()));
    }
    let mut from = 0;
    loop {
        let colon = from + content.get(from..)?.find(':')?;
        let rest = content.get(colon + 1..)?;
        if rest.is_empty() || rest.starts_with(' ') {
            let key = content.get(..colon)?.trim();
            return (!key.is_empty()).then(|| (key.to_owned(), rest.trim()));
        }
        from = colon + 1;
    }
}

impl Parser {
    /// The next line with something on it, and its indentation.
    fn peek(&mut self) -> Option<(usize, String)> {
        while let Some(line) = self.lines.get(self.at) {
            if empty(line) {
                self.at += 1;
                continue;
            }
            return Some((indent(line), uncommented(line).trim_start().to_owned()));
        }
        None
    }

    /// The node starting at the next line, if it's at least `min` in.
    fn block(&mut self, min: usize) -> Value {
        let Some((at, content)) = self.peek() else {
            return Value::Null;
        };
        if at < min {
            return Value::Null;
        }
        if dash(&content).is_some() {
            self.list(at)
        } else if key_value(&content).is_some() {
            self.map(at)
        } else {
            self.at += 1;
            scalar(&content)
        }
    }

    fn list(&mut self, at: usize) -> Value {
        let mut items = Vec::new();
        while let Some((here, content)) = self.peek() {
            let Some(rest) = dash(&content).filter(|_| here == at) else {
                break;
            };
            let item = rest.trim_start();
            if item.is_empty() {
                self.at += 1;
                items.push(self.block(at + 1));
            } else if key_value(item).is_some() {
                // `- key: value` starts a map where `key` is: that line,
                // re-read without its dash.
                let column = at + content.len() - item.len();
                if let Some(line) = self.lines.get_mut(self.at) {
                    *line = format!("{}{item}", " ".repeat(column));
                }
                items.push(self.map(column));
            } else {
                self.at += 1;
                items.push(self.inline(item, at));
            }
        }
        Value::List(items)
    }

    fn map(&mut self, at: usize) -> Value {
        let mut entries = Vec::new();
        while let Some((here, content)) = self.peek() {
            if here != at || dash(&content).is_some() {
                break;
            }
            let Some((key, value)) = key_value(&content) else {
                break;
            };
            self.at += 1;
            let value = if value.is_empty() {
                // A list may sit at the key's own indentation.
                match self.peek() {
                    Some((next, c)) if next == at && dash(&c).is_some() => self.list(at),
                    _ => self.block(at + 1),
                }
            } else {
                self.inline(value, at)
            };
            entries.push((key, value));
        }
        Value::Map(entries)
    }

    /// A value on its key's or dash's line: a block scalar's header, a
    /// flow list or map, or a scalar.
    fn inline(&mut self, value: &str, at: usize) -> Value {
        if let Some(style) = value.strip_prefix(['|', '>']) {
            let folded = value.starts_with('>');
            return Value::Text(self.block_scalar(at, folded, style));
        }
        flow(value)
    }

    /// The lines of a `|` or `>` scalar: those indented past `at`.
    fn block_scalar(&mut self, at: usize, folded: bool, style: &str) -> String {
        let mut raw = Vec::new();
        while let Some(line) = self.lines.get(self.at) {
            if !line.trim().is_empty() && indent(line) <= at {
                break;
            }
            raw.push(line.as_str());
            self.at += 1;
        }
        // Indented as its first line with something on it.
        let column = (raw.iter())
            .find(|l| !l.trim().is_empty())
            .map_or(0, |l| indent(l));
        let mut lines: Vec<String> = (raw.iter())
            .map(|l| l.get(column.min(l.len())..).unwrap_or("").to_owned())
            .collect();
        // Blank lines at the end belong to what follows, unless kept.
        let mut trailing = 0;
        while lines.last().is_some_and(|l| l.trim().is_empty()) {
            lines.pop();
            trailing += 1;
        }
        let mut text = if folded {
            let mut out = String::new();
            for line in &lines {
                if line.is_empty() {
                    out.push('\n');
                } else {
                    if !out.is_empty() && !out.ends_with('\n') {
                        out.push(' ');
                    }
                    out.push_str(line);
                }
            }
            out
        } else {
            lines.join("\n")
        };
        if style.contains('-') || text.is_empty() {
            return text;
        }
        text.push('\n');
        if style.contains('+') {
            text.push_str(&"\n".repeat(trailing));
        }
        text
    }
}

/// A flow list (`[a, "b"]`), a flow map (`{a: 1}`), or a scalar.
fn flow(value: &str) -> Value {
    let inner = |open: char, close: char| {
        let inner = value.strip_prefix(open)?.strip_suffix(close)?;
        Some(split_commas(inner))
    };
    if let Some(items) = inner('[', ']') {
        return Value::List(items.iter().map(|i| scalar(i)).collect());
    }
    if let Some(items) = inner('{', '}') {
        let entries = items.iter().filter_map(|i| {
            let (key, value) = key_value(i)?;
            Some((key, scalar(value)))
        });
        return Value::Map(entries.collect());
    }
    scalar(value)
}

/// Items between commas, outside quotes; empty ones left out.
fn split_commas(inner: &str) -> Vec<String> {
    let mut items = Vec::new();
    let mut item = String::new();
    let mut quote = None;
    for c in inner.chars() {
        match (quote, c) {
            (None, '"' | '\'') => quote = Some(c),
            (Some(q), c) if c == q => quote = None,
            (None, ',') => {
                items.push(std::mem::take(&mut item));
                continue;
            }
            _ => {}
        }
        item.push(c);
    }
    items.push(item);
    (items.into_iter())
        .map(|i| i.trim().to_owned())
        .filter(|i| !i.is_empty())
        .collect()
}

/// A scalar: quoted (with its escapes), `~`/`null`, or plain text.
fn scalar(value: &str) -> Value {
    let value = value.trim();
    if let Some(inner) = value.strip_prefix('"').and_then(|v| v.strip_suffix('"')) {
        let mut out = String::new();
        let mut chars = inner.chars();
        while let Some(c) = chars.next() {
            if c != '\\' {
                out.push(c);
                continue;
            }
            match chars.next() {
                Some('n') => out.push('\n'),
                Some('t') => out.push('\t'),
                Some(other) => out.push(other),
                None => {}
            }
        }
        return Value::Text(out);
    }
    if let Some(inner) = value.strip_prefix('\'').and_then(|v| v.strip_suffix('\'')) {
        return Value::Text(inner.replace("''", "'"));
    }
    match value {
        "" | "~" | "null" | "Null" | "NULL" => Value::Null,
        _ => Value::Text(value.to_owned()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn text(s: &str) -> Value {
        Value::Text(s.to_owned())
    }

    /// An issue form as GitHub documents them: nested maps in a list,
    /// flow lists, quoted strings, block scalars and comments.
    #[test]
    fn reads_an_issue_form() {
        let form = parse(
            r#"name: Bug Report   # shown in the chooser
description: "File a bug: it's broken"
title: '[Bug]: '
labels: ["bug", triage]
assignees:
  - octocat
body:
  - type: markdown
    attributes:
      value: |
        Thanks for taking the time!

        Really.
  - type: textarea
    id: what
    attributes:
      label: What happened?
      placeholder: Tell us # not a comment inside? it is
      render: shell
    validations:
      required: true
  - type: dropdown
    attributes:
      label: Version
      options:
      - "1.0"
      - 2.0
"#,
        );
        assert_eq!(form.get("name"), Some(&text("Bug Report")));
        assert_eq!(
            form.get("description"),
            Some(&text("File a bug: it's broken"))
        );
        assert_eq!(form.get("title"), Some(&text("[Bug]: ")));
        assert_eq!(
            form.get("labels").map(Value::names),
            Some(vec!["bug".into(), "triage".into()])
        );
        assert_eq!(
            form.get("assignees").map(Value::names),
            Some(vec!["octocat".into()])
        );
        let body = form.get("body").map(Value::list).unwrap_or_default();
        assert_eq!(body.len(), 3);
        let value = body[0].get("attributes").and_then(|a| a.get("value"));
        assert_eq!(
            value,
            Some(&text("Thanks for taking the time!\n\nReally.\n"))
        );
        let attributes = body[1].get("attributes").unwrap();
        assert_eq!(attributes.get("label"), Some(&text("What happened?")));
        assert_eq!(attributes.get("render"), Some(&text("shell")));
        let required = body[1].get("validations").and_then(|v| v.get("required"));
        assert_eq!(required, Some(&text("true")));
        let options = body[2].get("attributes").and_then(|a| a.get("options"));
        assert_eq!(
            options.map(Value::names),
            Some(vec!["1.0".into(), "2.0".into()])
        );
    }

    #[test]
    fn reads_front_matter_and_config() {
        let front = parse(
            "name: Feature\nabout: Suggest an idea\nlabels: enhancement, help wanted\nassignees: ''\n",
        );
        assert_eq!(
            front.get("labels").map(Value::names),
            Some(vec!["enhancement".into(), "help wanted".into()])
        );
        assert_eq!(front.get("assignees").map(Value::names), Some(Vec::new()));
        let config = parse(
            "blank_issues_enabled: false\ncontact_links:\n  - name: Forum\n    url: https://example.com\n    about: Ask here\n",
        );
        assert!(
            config
                .get("blank_issues_enabled")
                .is_some_and(Value::is_false)
        );
        let links = config
            .get("contact_links")
            .map(Value::list)
            .unwrap_or_default();
        assert_eq!(links[0].get("url"), Some(&text("https://example.com")));
        assert_eq!(links[0].get("about"), Some(&text("Ask here")));
    }

    #[test]
    fn folds_and_chomps_block_scalars() {
        let v = parse("a: >-\n  one\n  two\n\n  three\nb: |+\n  kept\n\nc: x\n");
        assert_eq!(v.get("a"), Some(&text("one two\nthree")));
        assert_eq!(v.get("b"), Some(&text("kept\n\n")));
        assert_eq!(v.get("c"), Some(&text("x")));
    }
}