git_bonsai/
tui.rs

1/*
2 * Copyright 2020 Aurélien Gâteau <mail@agateau.com>
3 *
4 * This file is part of git-bonsai.
5 *
6 * Git-bonsai is free software: you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation, either version 3 of the License, or (at your option)
9 * any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
14 * more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/**
21 * This module contains "low-level" primitives to implement a text-based UI
22 */
23use console::style;
24
25use dialoguer::{MultiSelect, Select};
26
27pub fn log_warning(msg: &str) {
28    println!("{}", style(format!("Warning: {}", msg)).yellow());
29}
30
31pub fn log_error(msg: &str) {
32    println!("{}", style(format!("Error: {}", msg)).red());
33}
34
35pub fn log_info(msg: &str) {
36    println!("{}", style(format!("Info: {}", msg)).blue());
37}
38
39pub fn select(msg: &str, items: &[String]) -> Vec<usize> {
40    let checked_items: Vec<(String, bool)> = items.iter().map(|x| (x.clone(), true)).collect();
41
42    MultiSelect::new()
43        .with_prompt(msg)
44        .items_checked(&checked_items[..])
45        .interact()
46        .unwrap()
47}
48
49pub fn select_one(msg: &str, items: &[String]) -> Option<usize> {
50    Select::new()
51        .with_prompt(msg)
52        .items(items)
53        .default(0)
54        .interact_opt()
55        .unwrap()
56}