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
use crate::color::reset_display;
use crate::git::GitStatus;
use crate::Config;
use crate::TermBuffer;
use crossterm::{self as ct, style, InputEvent, KeyEvent};
use std::iter;

#[derive(Debug)]
pub struct FilesPrompt<'a> {
  config: &'a mut Config,
  checked: Vec<bool>,
  selected_index: u16,
  options: GitStatus,
}

pub enum FilesPromptResult {
  Files(Vec<String>),
  Escape,
  Terminate,
}

impl<'a> FilesPrompt<'a> {
  pub fn new(config: &'a mut Config, options: GitStatus) -> Self {
    FilesPrompt {
      config,
      checked: (0..options.len()).map(|_| false).collect(),
      selected_index: 0,
      options,
    }
  }

  pub fn run(mut self) -> FilesPromptResult {
    let mut buffer = TermBuffer::new();

    let input = crossterm::input();
    let mut sync_stdin = input.read_sync();

    let figlet = self
      .config
      .get_figlet()
      .expect("Ensure figlet_file points to a valid file, or remove it.");

    let mut first_iteration = true;
    loop {
      let event = if first_iteration {
        first_iteration = false;
        None
      } else {
        sync_stdin.next()
      };

      match event {
        Some(InputEvent::Keyboard(KeyEvent::Ctrl('c'))) => {
          return FilesPromptResult::Terminate;
        }
        Some(InputEvent::Keyboard(KeyEvent::Char(' '))) => {
          let index = self.selected_index as usize;
          if index == 0 {
            let set_to = !self.checked.iter().all(|&x| x);

            for item in self.checked.iter_mut() {
              *item = set_to;
            }
          } else {
            self.checked[index - 1] = !self.checked[index - 1];
          }
        }
        Some(InputEvent::Keyboard(KeyEvent::Char('\n'))) => {
          let selected = self
            .options
            .iter()
            .enumerate()
            .filter_map(|(i, file)| Some(file).filter(|_| self.checked[i]))
            .map(Into::into)
            .collect();
          return FilesPromptResult::Files(selected);
        }

        Some(InputEvent::Keyboard(KeyEvent::Esc)) => {
          return FilesPromptResult::Escape;
        }
        Some(InputEvent::Keyboard(KeyEvent::Up)) => {
          self.selected_index = match self.selected_index {
            0 => 0,
            x => x.saturating_sub(1),
          };
        }
        Some(InputEvent::Keyboard(KeyEvent::Down)) => {
          let total = self.options.len() as u16 + 1;

          self.selected_index += 1;
          if self.selected_index >= total {
            self.selected_index = total.saturating_sub(1);
          }
        }
        _ => {}
      };

      let mut header = figlet.create_vec();
      figlet.write_to_buf_color("<glint>", header.as_mut_slice(), |s| {
        ct::style(s).with(ct::Color::Magenta).to_string()
      });

      for line in header {
        buffer.push_line(line);
      }

      let prompt_pre = "Toggle files to commit (with <space>):";
      let underscores = "-".repeat(prompt_pre.len());
      buffer.push_line("");
      buffer.push_line(prompt_pre);
      buffer.push_line(format!("{}{}", underscores, reset_display()));

      let y_offset = buffer.lines() + self.selected_index;

      let selected_color = style("").with(ct::Color::Blue).to_string();

      // Padded limit (never overflows by 1 item)
      let total = self.options.len();
      let max = 15;
      let take = if total > max { max - 3 } else { total };

      for (i, label) in iter::once("<all>")
        .chain(self.options.iter().map(|item| item.file()))
        .enumerate()
        .take(take + 1)
      {
        let color = if i as u16 == self.selected_index {
          &selected_color as &str
        } else {
          ""
        };

        let checked = if i == 0 {
          self.checked.iter().all(|&x| x)
        } else {
          self.checked[i - 1]
        };
        let prefix = if checked { "☑" } else { "□" };

        let line = format!("{}{} {}{}", color, prefix, label, reset_display());
        buffer.push_line(line);
      }

      if take < total {
        let diff = total - take;
        buffer.push_line(format!("and {} more", diff));
      }

      buffer.set_next_cursor((0, y_offset));
      buffer.render_frame();
      buffer.flush();
    }
  }
}