delta-gitu 0.1.14

An experimental fork of gitu (a git client inspired by Magit), published as a library for delta to embed
Documentation
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
use crate::Res;
use crate::error::Error;
use crate::text_input::TextInput;
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyModifiers};
use std::borrow::Cow;
use std::fs::{self, OpenOptions};
use std::io::Write;
use std::ops::Range;
use std::path::{Path, PathBuf};

pub(crate) struct PromptData {
    pub(crate) prompt_text: Cow<'static, str>,
    /// Drawn alone in front of the line typed on, as less and vim draw `/` for
    /// a search, rather than as a question.
    pub(crate) bare: bool,
}

#[derive(Clone, Copy)]
pub(crate) enum HistoryKind {
    GitCommand,
    FilePatterns,
    Search,
}

pub(crate) struct Prompt {
    pub(crate) data: Option<PromptData>,
    pub(crate) state: TextInput,
    /// What the last kill took, for `ctrl+y` to put back.
    killed: String,
    history_dir: Option<PathBuf>,
    /// Whether history is read but not added to.
    history_read_only: bool,
    active_history: Option<ActiveHistory>,
}

struct ActiveHistory {
    entries: Vec<String>,
    position: usize,
    draft: String,
}

impl Prompt {
    pub(crate) fn new() -> Self {
        Prompt {
            data: None,
            state: TextInput::default(),
            killed: String::new(),
            history_dir: None,
            history_read_only: false,
            active_history: None,
        }
    }

    pub(crate) fn with_history(history_dir: PathBuf, read_only: bool) -> Self {
        Self {
            history_dir: Some(history_dir),
            history_read_only: read_only,
            ..Self::new()
        }
    }

    pub(crate) fn set(&mut self, data: PromptData) {
        self.data = Some(data);
        self.state.focused = true;
    }

    pub(crate) fn reset(&mut self) {
        self.data = None;
        self.state = TextInput::default();
        self.active_history = None;
    }

    pub(crate) fn start_history(&mut self, kind: Option<HistoryKind>) -> Res<()> {
        let (Some(kind), Some(dir)) = (kind, &self.history_dir) else {
            return Ok(());
        };
        let draft = self.state.value.as_str().to_owned();
        let entries = read_history(dir, kind)?
            .into_iter()
            .filter(|entry| entry != &draft)
            .collect::<Vec<_>>();
        self.active_history = Some(ActiveHistory {
            position: entries.len(),
            entries,
            draft,
        });
        Ok(())
    }

    pub(crate) fn remember(&self, kind: HistoryKind, value: &str) -> Res<()> {
        let Some(dir) = &self.history_dir else {
            return Ok(());
        };
        if self.history_read_only || value.is_empty() {
            return Ok(());
        }

        fs::create_dir_all(dir).map_err(Error::WritePromptHistory)?;
        let mut file = OpenOptions::new()
            .create(true)
            .append(true)
            .open(history_path(dir, kind))
            .map_err(Error::WritePromptHistory)?;
        file.write_all(format!("{value}\n").as_bytes())
            .map_err(Error::WritePromptHistory)
    }

    /// A line being edited is a line to edit as any other: the readline keys,
    /// over and above the single-character motions and deletions
    /// [`TextInput`] already binds.
    ///
    /// Words come in the two kinds readline has, and the distinction earns its
    /// keep on the commands gitu puts here: `alt+b`/`alt+f`/`alt+d` step over
    /// runs of letters and digits, so they move within a path, while `ctrl+w`
    /// takes back a whole whitespace-delimited token, which is what a pathspec
    /// is.
    pub(crate) fn handle_key(&mut self, key: KeyEvent) {
        if key.kind == KeyEventKind::Release {
            return;
        }

        const ALT: KeyModifiers = KeyModifiers::ALT;
        const CTRL: KeyModifiers = KeyModifiers::CONTROL;
        let cursor = self.cursor();

        match (key.code, key.modifiers) {
            (KeyCode::Up, KeyModifiers::NONE) => self.history_previous(),
            (KeyCode::Down, KeyModifiers::NONE) => self.history_next(),
            (KeyCode::Char('b'), ALT) | (KeyCode::Left, ALT | CTRL) => {
                self.move_to(self.previous_word_start());
            }
            (KeyCode::Char('f'), ALT) | (KeyCode::Right, ALT | CTRL) => {
                self.move_to(self.next_word_end());
            }
            (KeyCode::Char('w'), CTRL) => self.kill(self.previous_token_start()..cursor),
            (KeyCode::Backspace, ALT) => self.kill(self.previous_word_start()..cursor),
            (KeyCode::Char('d'), ALT) => self.kill(cursor..self.next_word_end()),
            (KeyCode::Char('k'), CTRL) => self.kill(cursor..self.len()),
            (KeyCode::Char('u'), CTRL) => self.kill(0..self.len()),
            (KeyCode::Char('y'), CTRL) => self.yank(),
            (KeyCode::Char('t'), CTRL) => self.transpose(),
            _ => self.state.handle_key_event(key),
        }
    }

    fn cursor(&self) -> usize {
        self.state.position().min(self.len())
    }

    fn len(&self) -> usize {
        self.state.value.as_str().chars().count()
    }

    fn chars(&self) -> Vec<char> {
        self.state.value.as_str().chars().collect()
    }

    fn move_to(&mut self, cursor: usize) {
        self.state.move_to(cursor);
    }

    fn history_previous(&mut self) {
        let Some(history) = &mut self.active_history else {
            return;
        };
        save_history_edit(history, self.state.value.as_str());
        if history.position > 0 {
            history.position -= 1;
        }
        self.show_history_value();
    }

    fn history_next(&mut self) {
        let Some(history) = &mut self.active_history else {
            return;
        };
        save_history_edit(history, self.state.value.as_str());
        if history.position < history.entries.len() {
            history.position += 1;
        }
        self.show_history_value();
    }

    fn show_history_value(&mut self) {
        let Some(history) = &self.active_history else {
            return;
        };
        let value = history
            .entries
            .get(history.position)
            .unwrap_or(&history.draft);
        self.state.value = value.clone();
        self.state.move_to(usize::MAX);
    }

    /// Take `range` out of the line, keeping it for [`Self::yank`]. A kill of
    /// nothing is not a kill: it leaves what was killed before to be put back.
    fn kill(&mut self, range: Range<usize>) {
        if range.is_empty() {
            return;
        }

        let chars = self.chars();
        self.killed = chars[range.clone()].iter().collect();
        self.state.value = chars[..range.start]
            .iter()
            .chain(&chars[range.end..])
            .collect();
        self.move_to(range.start);
    }

    fn yank(&mut self) {
        let chars = self.chars();
        let cursor = self.cursor();

        self.state.value = chars[..cursor]
            .iter()
            .chain(self.killed.chars().collect::<Vec<_>>().iter())
            .chain(&chars[cursor..])
            .collect();
        self.move_to(cursor + self.killed.chars().count());
    }

    /// Swap the two characters the cursor sits between, and step over them. At
    /// the end of the line there is nothing to the right to swap with, so the
    /// last two change places instead.
    fn transpose(&mut self) {
        let mut chars = self.chars();
        let cursor = self.cursor().min(chars.len().saturating_sub(1)).max(1);
        if chars.len() < 2 {
            return;
        }

        chars.swap(cursor - 1, cursor);
        self.state.value = chars.iter().collect();
        self.move_to((cursor + 1).min(self.len()));
    }

    /// Where the run of letters and digits before the cursor begins.
    fn previous_word_start(&self) -> usize {
        let chars = self.chars();
        let end = skipping_back(&chars, self.cursor(), |c| !c.is_alphanumeric());
        skipping_back(&chars, end, |c| c.is_alphanumeric())
    }

    /// Where the run of letters and digits after the cursor ends.
    fn next_word_end(&self) -> usize {
        let chars = self.chars();
        let start = skipping_on(&chars, self.cursor(), |c| !c.is_alphanumeric());
        skipping_on(&chars, start, |c| c.is_alphanumeric())
    }

    /// Where the whitespace-delimited token before the cursor begins.
    fn previous_token_start(&self) -> usize {
        let chars = self.chars();
        let end = skipping_back(&chars, self.cursor(), |c| c.is_whitespace());
        skipping_back(&chars, end, |c| !c.is_whitespace())
    }
}

fn save_history_edit(history: &mut ActiveHistory, value: &str) {
    if let Some(entry) = history.entries.get_mut(history.position) {
        *entry = value.to_owned();
    } else {
        history.draft = value.to_owned();
    }
}

fn read_history(dir: &Path, kind: HistoryKind) -> Res<Vec<String>> {
    match fs::read_to_string(history_path(dir, kind)) {
        Ok(contents) => Ok(contents.lines().map(str::to_owned).collect()),
        Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(Vec::new()),
        Err(err) => Err(Error::ReadPromptHistory(err)),
    }
}

fn history_path(dir: &Path, kind: HistoryKind) -> PathBuf {
    dir.join(match kind {
        HistoryKind::GitCommand => "git-command-history",
        HistoryKind::FilePatterns => "file-pattern-history",
        HistoryKind::Search => "search-history",
    })
}

fn skipping_back(chars: &[char], from: usize, matching: impl Fn(&char) -> bool) -> usize {
    let kept = chars[..from]
        .iter()
        .rev()
        .take_while(|c| matching(c))
        .count();
    from - kept
}

fn skipping_on(chars: &[char], from: usize, matching: impl Fn(&char) -> bool) -> usize {
    from + chars[from..].iter().take_while(|c| matching(c)).count()
}

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

    /// The line and cursor after typing `keys`, both written with `|` where
    /// the cursor is.
    fn edited(before: &str, keys: &str) -> String {
        let (value, rest) = before.split_once('|').expect("no cursor in the line");
        let mut prompt = Prompt::new();
        prompt.state.value = format!("{value}{rest}");
        prompt.move_to(value.chars().count());

        let ("", keys) = parse_test_keys(keys).unwrap() else {
            unreachable!()
        };
        for (modifiers, code) in keys {
            prompt.handle_key(KeyEvent::new(code, modifiers));
        }

        let chars = prompt.chars();
        let cursor = prompt.cursor();
        format!(
            "{}|{}",
            chars[..cursor].iter().collect::<String>(),
            chars[cursor..].iter().collect::<String>()
        )
    }

    /// A word is a run of letters and digits, so a motion stops inside a path
    /// rather than stepping over the whole of it.
    #[test]
    fn a_word_motion_steps_over_letters_and_digits() {
        assert_eq!(edited("git diff main|", "<alt+b>"), "git diff |main");
        assert_eq!(edited("git diff main|", "<alt+b><alt+b>"), "git |diff main");
        assert_eq!(edited("|src/lib.rs", "<alt+f>"), "src|/lib.rs");
        assert_eq!(edited("|src/lib.rs", "<alt+f><alt+f>"), "src/lib|.rs");
        assert_eq!(edited("|a", "<alt+b>"), "|a");
        assert_eq!(edited("a|", "<alt+f>"), "a|");
        assert_eq!(edited("a b|", "<ctrl+left>"), "a |b");
        assert_eq!(edited("|a b", "<ctrl+right>"), "a| b");
    }

    /// `ctrl+w` takes back a whole token, which is what a pathspec is: nobody
    /// wants it in the pieces its punctuation makes.
    #[test]
    fn ctrl_w_takes_back_a_whole_token() {
        assert_eq!(
            edited("git diff -- ':(top,exclude)*_test.go'|", "<ctrl+w>"),
            "git diff -- |"
        );
        assert_eq!(edited("git diff  |", "<ctrl+w>"), "git |");
        assert_eq!(edited("|", "<ctrl+w>"), "|");
    }

    #[test]
    fn a_word_can_be_killed_either_side_of_the_cursor() {
        assert_eq!(edited("src/lib.rs|", "<alt+backspace>"), "src/lib.|");
        assert_eq!(edited("|src/lib.rs", "<alt+d>"), "|/lib.rs");
        assert_eq!(edited("a |b c", "<alt+d>"), "a | c");
    }

    /// What a kill took, `ctrl+y` puts back — which is how a token is moved,
    /// and how a mistaken kill is undone.
    #[test]
    fn what_was_killed_can_be_put_back() {
        assert_eq!(
            edited("git diff main|", "<ctrl+w><ctrl+y>"),
            "git diff main|"
        );
        assert_eq!(
            edited("git diff main|", "<ctrl+w><ctrl+a><ctrl+y>"),
            "main|git diff "
        );
        // A kill of nothing leaves the last kill to be put back.
        assert_eq!(edited("a b|", "<ctrl+w><ctrl+a><ctrl+w><ctrl+y>"), "b|a ");
    }

    #[test]
    fn a_line_can_be_killed_to_the_end_or_entirely() {
        assert_eq!(edited("git |diff main", "<ctrl+k>"), "git |");
        assert_eq!(
            edited("git |diff main", "<ctrl+k><ctrl+y>"),
            "git diff main|"
        );
        assert_eq!(edited("git |diff", "<ctrl+u>"), "|");
        assert_eq!(edited("git |diff", "<ctrl+u><ctrl+y>"), "git diff|");
    }

    #[test]
    fn two_characters_change_places() {
        assert_eq!(edited("ab|cd", "<ctrl+t>"), "acb|d");
        assert_eq!(edited("ab|", "<ctrl+t>"), "ba|");
        assert_eq!(edited("|ab", "<ctrl+t>"), "ba|");
        assert_eq!(edited("a|", "<ctrl+t>"), "a|");
    }

    /// The keys `TextInput` binds still do what they did.
    #[test]
    fn the_single_character_keys_are_untouched() {
        assert_eq!(edited("git dif|", "f"), "git diff|");
        assert_eq!(edited("git diff|", "<backspace>"), "git dif|");
        assert_eq!(edited("git |diff", "<ctrl+e>"), "git diff|");
        assert_eq!(edited("git diff|", "<ctrl+a>"), "|git diff");
        assert_eq!(edited("git diff|", "<left>"), "git dif|f");
    }

    /// Characters are characters, not bytes: a motion over them lands between
    /// them.
    #[test]
    fn a_multibyte_line_edits_by_character() {
        assert_eq!(edited("héllo wörld|", "<ctrl+w>"), "héllo |");
        assert_eq!(edited("héllo wörld|", "<alt+b>"), "héllo |wörld");
        assert_eq!(edited("héllo|", "<backspace>"), "héll|");
    }
}