Skip to content

Commit

Permalink
Add support for channels more broadly
Browse files Browse the repository at this point in the history
  • Loading branch information
Brendan Molloy committed Feb 5, 2019
1 parent f1602b4 commit c8815cb
Show file tree
Hide file tree
Showing 5 changed files with 256 additions and 193 deletions.
15 changes: 9 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,22 @@
name = "pahkat"
version = "0.6.0"
authors = ["Brendan Molloy <[email protected]>"]
edition = "2018"

[[bin]]
name = "pahkat"
path = "src/main.rs"
required-features = ["binaries"]

[dependencies]
clap = { version = "*", optional = true }
termcolor = { version = "0.3", optional = true }
clap = { version = "2.32.0", optional = true }
dialoguer = { version = "0.3.0", optional = true }
termcolor = { version = "1.0.4", optional = true }
pathdiff = { version = "0.1", optional = true }
serde = "*"
serde_derive = "*"
serde_json = "*"
url = { version = "1.7.2", optional = true}
serde = "1.0.87"
serde_derive = "1.0.87"
serde_json = "1.0.38"

[features]
binaries = ["clap", "termcolor", "pathdiff"]
binaries = ["clap", "termcolor", "pathdiff", "dialoguer", "url"]
100 changes: 38 additions & 62 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,79 +2,55 @@ use std::io;
use std::io::Write;
use std::collections::BTreeMap;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

#[cfg(target_os = "windows")]
pub const INPUT_DEFAULT_LEN: usize = 2;
#[cfg(not(target_os = "windows"))]
pub const INPUT_DEFAULT_LEN: usize = 1;
use dialoguer::{Confirmation, Input, Checkboxes, Select, theme::ColorfulTheme};

pub fn progress(color: Color, first: &str, rest: &str) -> Result<(), io::Error> {
let mut stdout = StandardStream::stdout(ColorChoice::Always);
try!(stdout.set_color(ColorSpec::new().set_fg(Some(color))
let mut stderr = StandardStream::stderr(ColorChoice::Always);
stderr.set_color(ColorSpec::new().set_fg(Some(color))
.set_intense(true)
.set_bold(true)));
try!(write!(&mut stdout, "{:>12}", first));
stdout.reset()?;
writeln!(&mut stdout, " {}", rest)?;
.set_bold(true))?;
write!(&mut stderr, "{:>12}", first)?;
stderr.reset()?;
writeln!(&mut stderr, " {}", rest)?;
Ok(())
}

pub fn prompt_question(prompt: &str, default: bool) -> bool {
let mut stdout = StandardStream::stdout(ColorChoice::Always);
stdout.set_color(ColorSpec::new().set_fg(Some(Color::Cyan))).unwrap();
write!(&mut stdout, "{}? ", prompt).unwrap();
stdout.reset().unwrap();

print!("({}) ", if default { "yes" } else { "no" });

let _ = io::stdout().flush();
let mut input = String::new();

fn parse(it: &str) -> bool {
let lower = it.to_lowercase();

if lower == "y" || lower == "yes" {
return true;
}

false
}

match io::stdin().read_line(&mut input) {
Ok(n) => {
match n {
0 => false,
INPUT_DEFAULT_LEN => default,
_ => parse(input.trim())
}
}
Err(error) => panic!(error)
}
Confirmation::with_theme(&ColorfulTheme::default())
.with_text(prompt)
.default(default)
.interact()
.unwrap_or(default)
}

pub fn prompt_line(prompt: &str, default: &str) -> Option<String> {
let mut stdout = StandardStream::stdout(ColorChoice::Always);
stdout.set_color(ColorSpec::new().set_fg(Some(Color::Cyan))).unwrap();
write!(&mut stdout, "{}: ", prompt).unwrap();
stdout.reset().unwrap();

if default != "" {
print!("({}) ", default);
}

let _ = io::stdout().flush();
let mut input = String::new();
Some(Input::with_theme(&ColorfulTheme::default())
.with_prompt(prompt)
.default(default.to_string())
.interact()
.unwrap_or(default.to_string())
.to_string())
}

match io::stdin().read_line(&mut input) {
Ok(n) => {
match n {
0 => None,
INPUT_DEFAULT_LEN => Some(default.to_owned()),
_ => Some(input.trim().to_owned())
}
}
Err(error) => panic!(error)
}
pub fn prompt_multi_select(prompt: &str, options: &[&str]) -> Vec<String> {
Checkboxes::with_theme(&ColorfulTheme::default())
.with_prompt(prompt)
.items(options)
.interact()
.unwrap_or(vec![])
.into_iter()
.map(|i| options[i].to_string())
.collect()
}

pub fn prompt_select(prompt: &str, options: &[String], default: usize) -> String {
Select::with_theme(&ColorfulTheme::default())
.with_prompt(prompt)
.items(options)
.default(default)
.interact()
.map(|i| options[i].to_string())
.unwrap_or_else(|_| options[default].to_string())
}

pub fn parse_platform_list(vec: &[String]) -> BTreeMap<String, String> {
Expand Down
Loading

0 comments on commit c8815cb

Please sign in to comment.