aboutsummaryrefslogtreecommitdiff
path: root/ticket/src/tui.rs
blob: 9898e71922bb08c7bec5f4552be7341518fa1493 (plain)
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
use crate::{
  actions::{
    get_closed_tickets,
    get_open_tickets,
    save_ticket,
    uuid_v1,
  },
  Comment,
  Name,
  Status,
  Ticket,
};
use anyhow::Result;
use configamajig::{
  get_user_config,
  UserConfig,
};
use crossterm::{
  event::{
    self,
    DisableMouseCapture,
    EnableMouseCapture,
    Event as CEvent,
    KeyCode,
    KeyEvent,
  },
  queue,
  terminal::*,
};
use std::{
  collections::BTreeMap,
  io::{
    self,
    BufWriter,
    Write,
  },
  sync::mpsc::{
    self,
    Receiver,
    Sender,
  },
  thread,
  time::Duration,
};
use tui::{
  backend::{
    Backend,
    CrosstermBackend,
  },
  layout::{
    Alignment,
    Constraint,
    Direction,
    Layout,
    Rect,
  },
  style::{
    Color,
    Modifier,
    Style,
  },
  widgets::{
    Block,
    Borders,
    Paragraph,
    Row,
    Table,
    Tabs,
    Text,
    Widget,
  },
  Frame,
  Terminal,
};

pub struct TabsState<'a> {
  pub titles: Vec<&'a str>,
  pub index: usize,
}

impl<'a> TabsState<'a> {
  pub fn new(titles: Vec<&'a str>) -> TabsState {
    TabsState { titles, index: 0 }
  }

  pub fn next(&mut self) {
    self.index = (self.index + 1) % self.titles.len()
  }

  pub fn previous(&mut self) {
    if self.index > 0 {
      self.index = (self.index - 1) % self.titles.len()
    }
  }
}
pub enum Event<I> {
  Input(I),
  Tick,
}

pub struct TicketState {
  pub tickets: BTreeMap<String, Vec<(Ticket, String)>>,
  pub index: usize,
  pub status: Status,
}

impl TicketState {
  pub fn new(tickets: BTreeMap<String, Vec<(Ticket, String)>>) -> Self {
    Self {
      tickets,
      index: 0,
      status: Status::Open,
    }
  }

  fn len(&self) -> usize {
    match self.status {
      Status::Open => self.tickets.get("Open").unwrap().len(),
      Status::Closed => self.tickets.get("Closed").unwrap().len(),
    }
  }

  pub fn next(&mut self) {
    self.index = (self.index + 1) % self.len()
  }

  pub fn previous(&mut self) {
    if self.index > 0 {
      self.index = (self.index - 1) % self.len()
    }
  }
}
struct App<'a> {
  tabs: TabsState<'a>,
  tickets: TicketState,
  should_quit: bool,
}
#[derive(Debug, Clone, Copy)]
pub struct Config {
  pub exit_key: KeyCode,
  pub tick_rate: Duration,
}

impl Default for Config {
  fn default() -> Self {
    Self {
      exit_key: KeyCode::Char('q'),
      tick_rate: Duration::from_millis(250),
    }
  }
}

#[allow(clippy::too_many_lines)]
pub fn run() -> Result<()> {
  let stdout = io::stdout();
  let mut lock = BufWriter::new(stdout.lock());
  // Terminal initialization
  enable_raw_mode()?;
  queue!(lock, EnterAlternateScreen, EnableMouseCapture)?;
  let mut terminal = Terminal::new(CrosstermBackend::new(lock))?;
  terminal.backend_mut().hide_cursor()?;
  terminal.clear()?;

  // App
  let mut app = App {
    tabs: TabsState::new(vec!["Open", "Closed"]),
    tickets: {
      let mut map = BTreeMap::new();
      let _ = map.insert(
        "Open".into(),
        get_open_tickets()?
          .into_iter()
          .map(|i| (i, String::new()))
          .collect(),
      );
      let _ = map.insert(
        "Closed".into(),
        get_closed_tickets()?
          .into_iter()
          .map(|i| (i, String::new()))
          .collect(),
      );
      TicketState::new(map)
    },
    should_quit: false,
  };

  // Spawn event sender thread
  let (tx, rx) = mpsc::channel();
  let (tx_close, rx_close) = mpsc::channel();
  let _ = thread::spawn(move || -> Result<()> {
    loop {
      // poll for tick rate duration, if no events, sent tick event.
      if event::poll(Duration::from_millis(250))? {
        if let CEvent::Key(key) = event::read()? {
          tx.send(Event::Input(key))?;
        }
      }

      if rx_close.try_recv().unwrap_or(false) {
        break;
      }

      tx.send(Event::Tick)?;
    }
    Ok(())
  });

  // Cached Values
  let user_config = get_user_config()?;

  // Main drawing and event receiving loop
  loop {
    let status = match app.tickets.status {
      Status::Open => "Open",
      Status::Closed => "Closed",
    };

    terminal.draw(|mut f| {
      let size = f.size();
      let vertical = Layout::default()
        .direction(Direction::Vertical)
        .constraints(
          [
            Constraint::Length(3),
            Constraint::Min(0),
            Constraint::Length(3),
            Constraint::Length(3),
          ]
          .as_ref(),
        )
        .split(size);
      let horizontal = Layout::default()
        .direction(Direction::Horizontal)
        .vertical_margin(3)
        .constraints(
          [Constraint::Percentage(30), Constraint::Percentage(70)].as_ref(),
        )
        .split(Rect {
          x: size.x,
          y: size.y,
          width: size.width,
          height: size.height - 3,
        });
      app.tabs(&mut f, vertical[0]);
      app.table(status, &mut f, horizontal[0]);
      app.description(status, &mut f, horizontal[1]);
      app.comment(status, &mut f, vertical[2]);
      App::instructions(&mut f, vertical[3]);
    })?;

    handle_event(&rx, &tx_close, &mut app, &user_config, &status)?;

    if app.should_quit {
      let open = app.tickets.tickets["Open"].iter();
      let closed = app.tickets.tickets["Closed"].iter();
      for t in open.chain(closed) {
        save_ticket(&t.0)?;
      }
      break;
    }
  }

  // Clean up terminal
  queue!(
    io::stdout().lock(),
    LeaveAlternateScreen,
    DisableMouseCapture
  )?;
  terminal.backend_mut().show_cursor()?;
  disable_raw_mode()?;

  Ok(())
}

fn handle_event(
  rx: &Receiver<Event<KeyEvent>>,
  tx: &Sender<bool>,
  app: &mut App,
  user_config: &UserConfig,
  status: &str,
) -> Result<()> {
  match rx.recv()? {
    Event::Input(event) => match event.code {
      KeyCode::Esc => {
        app.should_quit = true;
        tx.send(true)?;
      }
      KeyCode::Right => {
        if app.tabs.index == 0 {
          app.tickets.status = Status::Closed;
          app.tickets.index = 0;
        }
        app.tabs.next();
      }
      KeyCode::Left => {
        if app.tabs.index > 0 {
          app.tickets.status = Status::Open;
          app.tickets.index = 0;
        }
        app.tabs.previous();
      }
      KeyCode::Up => app.tickets.previous(),
      KeyCode::Down => app.tickets.next(),
      KeyCode::Backspace => {
        let _ = app.tickets.tickets.get_mut(status).unwrap()[app.tickets.index]
          .1
          .pop();
      }
      KeyCode::Char(c) => {
        app.tickets.tickets.get_mut(status).unwrap()[app.tickets.index]
          .1
          .push(c);
      }
      KeyCode::Enter => {
        let ticket =
          &mut app.tickets.tickets.get_mut(status).unwrap()[app.tickets.index];
        if !ticket.1.is_empty() {
          let _ = ticket.0.comments.insert(
            uuid_v1()?,
            (
              user_config.uuid,
              Name(user_config.name.clone()),
              Comment(ticket.1.clone()),
            ),
          );
          ticket.1.clear();
        }
      }
      _ => {}
    },
    Event::Tick => (),
  }
  Ok(())
}

impl<'a> App<'a> {
  #[inline]
  fn table(&self, tab: &'a str, f: &mut Frame<impl Backend>, rect: Rect) {
    Table::new(
      ["Id", "Title"].iter(),
      self
        .tickets
        .tickets
        .get(tab)
        .unwrap()
        .iter()
        .enumerate()
        .map(move |(idx, i)| {
          let data =
            vec![i.0.id.to_string(), i.0.title.to_string()].into_iter();
          let normal_style = Style::default().fg(Color::Yellow);
          let selected_style =
            Style::default().fg(Color::White).modifier(Modifier::BOLD);
          if idx == self.tickets.index {
            Row::StyledData(data, selected_style)
          } else {
            Row::StyledData(data, normal_style)
          }
        }),
    )
    .block(Block::default().title(tab).borders(Borders::ALL))
    .header_style(Style::default().fg(Color::Yellow))
    .widths(&[Constraint::Percentage(30), Constraint::Percentage(70)])
    .style(Style::default().fg(Color::White))
    .column_spacing(1)
    .render(f, rect)
  }

  #[inline]
  fn description(&self, tab: &'a str, f: &mut Frame<impl Backend>, rect: Rect) {
    let mut description = vec![];
    for (idx, i) in self.tickets.tickets.get(tab).unwrap().iter().enumerate() {
      if idx == self.tickets.index {
        description = {
          let header = Style::default().fg(Color::Red).modifier(Modifier::BOLD);
          let mut desc = vec![
            Text::styled("Description\n-------------\n", header),
            Text::raw(i.0.description.to_owned()),
          ];
          let name_style =
            Style::default().fg(Color::Cyan).modifier(Modifier::BOLD);
          if i.0.assignees.is_empty() {
            desc.push(Text::styled("\nAssignees\n---------\n", header));
          } else {
            desc.push(Text::styled("\nAssignees\n---------\n", header));
            if i.0.assignees.len() == 1 {
              let (_, name) = &i.0.assignees[0];
              desc.push(Text::styled(name.0.clone(), name_style));
            } else {
              for (idx, (_, name)) in i.0.assignees.iter().enumerate() {
                if idx < i.0.assignees.len() - 1 {
                  desc.push(Text::styled(format!("{}, ", name.0), name_style));
                } else {
                  desc.push(Text::styled(name.0.clone(), name_style));
                }
              }
            }
          }

          if i.0.comments.is_empty() {
            desc.push(Text::styled("\nComments\n--------\n", header));
          } else {
            desc.push(Text::styled("\nComments\n--------\n", header));
            for (_, name, comment) in i.0.comments.values() {
              desc.push(Text::styled(format!("{}\n", name.0), name_style));
              desc.push(Text::raw(format!("{}\n\n", comment.0)));
            }
          }
          desc
        };
        break;
      }
    }

    Paragraph::new(description.iter())
      .block(Block::default().borders(Borders::ALL))
      .alignment(Alignment::Left)
      .wrap(true)
      .render(f, rect);
  }

  #[inline]
  fn comment(&self, tab: &'a str, f: &mut Frame<impl Backend>, rect: Rect) {
    let (_, s) = &self.tickets.tickets.get(tab).unwrap()[self.tickets.index];
    let mut text = String::from("> ");
    text.push_str(&s);

    Paragraph::new([Text::raw(text)].iter())
      .block(Block::default().borders(Borders::ALL).title("Comment"))
      .alignment(Alignment::Left)
      .wrap(true)
      .render(f, rect);
  }

  #[inline]
  fn tabs(&self, f: &mut Frame<impl Backend>, rect: Rect) {
    Tabs::default()
      .block(Block::default().borders(Borders::ALL).title("Status"))
      .titles(&self.tabs.titles)
      .select(self.tabs.index)
      .style(Style::default().fg(Color::Cyan))
      .highlight_style(Style::default().fg(Color::Yellow))
      .render(f, rect);
  }

  #[inline]
  fn instructions(f: &mut Frame<impl Backend>, rect: Rect) {
    let blue = Style::default().fg(Color::Blue).modifier(Modifier::BOLD);
    Paragraph::new(
      [
        Text::Styled("[ESC] ".into(), blue),
        Text::Raw("- Exit ".into()),
        Text::Styled("[Enter] ".into(), blue),
        Text::Raw("- Comment ".into()),
        Text::Styled("[Char] ".into(), blue),
        Text::Raw("- Write a comment ".into()),
        Text::Styled("[Backspace] ".into(), blue),
        Text::Raw("- Delete a character".into()),
      ]
      .iter(),
    )
    .block(Block::default().borders(Borders::ALL).title("Instructions"))
    .alignment(Alignment::Left)
    .wrap(true)
    .render(f, rect);
  }
}