2020-06-27 21:46:30 +00:00
|
|
|
|
use crate::prelude::*;
|
2021-01-10 02:50:49 +00:00
|
|
|
|
use nu_engine::WholeStreamCommand;
|
2020-06-27 21:46:30 +00:00
|
|
|
|
use nu_errors::ShellError;
|
|
|
|
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
|
|
|
|
use nu_source::Tagged;
|
|
|
|
|
|
|
|
|
|
pub struct Char;
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
struct CharArgs {
|
|
|
|
|
name: Tagged<String>,
|
2020-10-28 14:08:09 +00:00
|
|
|
|
unicode: bool,
|
2020-06-27 21:46:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[async_trait]
|
|
|
|
|
impl WholeStreamCommand for Char {
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
|
"char"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2021-01-22 21:48:31 +00:00
|
|
|
|
Signature::build("char")
|
2020-10-28 14:08:09 +00:00
|
|
|
|
.required(
|
|
|
|
|
"character",
|
|
|
|
|
SyntaxShape::Any,
|
|
|
|
|
"the name of the character to output",
|
|
|
|
|
)
|
|
|
|
|
.switch("unicode", "unicode string i.e. 1f378", Some('u'))
|
2020-06-27 21:46:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
|
"Output special characters (eg. 'newline')"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
2020-07-20 19:25:38 +00:00
|
|
|
|
vec![
|
|
|
|
|
Example {
|
|
|
|
|
description: "Output newline",
|
|
|
|
|
example: r#"char newline"#,
|
|
|
|
|
result: Some(vec![Value::from("\n")]),
|
|
|
|
|
},
|
|
|
|
|
Example {
|
|
|
|
|
description: "Output prompt character, newline and a hamburger character",
|
|
|
|
|
example: r#"echo $(char prompt) $(char newline) $(char hamburger)"#,
|
|
|
|
|
result: Some(vec![
|
|
|
|
|
UntaggedValue::string("\u{25b6}").into(),
|
|
|
|
|
UntaggedValue::string("\n").into(),
|
|
|
|
|
UntaggedValue::string("\u{2261}").into(),
|
|
|
|
|
]),
|
|
|
|
|
},
|
2020-10-28 14:08:09 +00:00
|
|
|
|
Example {
|
|
|
|
|
description: "Output unicode character",
|
|
|
|
|
example: r#"char -u 1f378"#,
|
|
|
|
|
result: Some(vec![Value::from("\u{1f378}")]),
|
|
|
|
|
},
|
2020-07-20 19:25:38 +00:00
|
|
|
|
]
|
2020-06-27 21:46:30 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-12-18 07:53:49 +00:00
|
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
|
let (CharArgs { name, unicode }, _) = args.process().await?;
|
2020-06-27 21:46:30 +00:00
|
|
|
|
|
2020-10-28 14:08:09 +00:00
|
|
|
|
if unicode {
|
|
|
|
|
let decoded_char = string_to_unicode_char(&name.item);
|
|
|
|
|
if let Some(output) = decoded_char {
|
|
|
|
|
Ok(OutputStream::one(ReturnSuccess::value(
|
|
|
|
|
UntaggedValue::string(output).into_value(name.tag()),
|
|
|
|
|
)))
|
|
|
|
|
} else {
|
|
|
|
|
Err(ShellError::labeled_error(
|
|
|
|
|
"error decoding unicode character",
|
|
|
|
|
"error decoding unicode character",
|
|
|
|
|
name.tag(),
|
|
|
|
|
))
|
|
|
|
|
}
|
2020-06-27 21:46:30 +00:00
|
|
|
|
} else {
|
2020-10-28 14:08:09 +00:00
|
|
|
|
let special_character = str_to_character(&name.item);
|
|
|
|
|
if let Some(output) = special_character {
|
|
|
|
|
Ok(OutputStream::one(ReturnSuccess::value(
|
|
|
|
|
UntaggedValue::string(output).into_value(name.tag()),
|
|
|
|
|
)))
|
|
|
|
|
} else {
|
|
|
|
|
Err(ShellError::labeled_error(
|
|
|
|
|
"error finding named character",
|
|
|
|
|
"error finding named character",
|
|
|
|
|
name.tag(),
|
|
|
|
|
))
|
|
|
|
|
}
|
2020-06-27 21:46:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-28 14:08:09 +00:00
|
|
|
|
fn string_to_unicode_char(s: &str) -> Option<char> {
|
|
|
|
|
u32::from_str_radix(s, 16)
|
|
|
|
|
.ok()
|
|
|
|
|
.and_then(std::char::from_u32)
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-27 21:46:30 +00:00
|
|
|
|
fn str_to_character(s: &str) -> Option<String> {
|
|
|
|
|
match s {
|
|
|
|
|
"newline" | "enter" | "nl" => Some("\n".into()),
|
|
|
|
|
"tab" => Some("\t".into()),
|
2020-07-06 15:30:47 +00:00
|
|
|
|
"sp" | "space" => Some(" ".into()),
|
2020-07-01 22:34:11 +00:00
|
|
|
|
// Unicode names came from https://www.compart.com/en/unicode
|
|
|
|
|
// Private Use Area (U+E000-U+F8FF)
|
2020-10-22 01:36:27 +00:00
|
|
|
|
// Unicode can't be mixed with Ansi or it will break width calculation
|
2020-07-01 22:34:11 +00:00
|
|
|
|
"branch" => Some('\u{e0a0}'.to_string()), //
|
|
|
|
|
"segment" => Some('\u{e0b0}'.to_string()), //
|
|
|
|
|
|
|
|
|
|
"identical_to" | "hamburger" => Some('\u{2261}'.to_string()), // ≡
|
|
|
|
|
"not_identical_to" | "branch_untracked" => Some('\u{2262}'.to_string()), // ≢
|
|
|
|
|
"strictly_equivalent_to" | "branch_identical" => Some('\u{2263}'.to_string()), // ≣
|
|
|
|
|
|
|
|
|
|
"upwards_arrow" | "branch_ahead" => Some('\u{2191}'.to_string()), // ↑
|
|
|
|
|
"downwards_arrow" | "branch_behind" => Some('\u{2193}'.to_string()), // ↓
|
|
|
|
|
"up_down_arrow" | "branch_ahead_behind" => Some('\u{2195}'.to_string()), // ↕
|
|
|
|
|
|
|
|
|
|
"black_right_pointing_triangle" | "prompt" => Some('\u{25b6}'.to_string()), // ▶
|
|
|
|
|
"vector_or_cross_product" | "failed" => Some('\u{2a2f}'.to_string()), // ⨯
|
|
|
|
|
"high_voltage_sign" | "elevated" => Some('\u{26a1}'.to_string()), // ⚡
|
|
|
|
|
"tilde" | "twiddle" | "squiggly" | "home" => Some("~".into()), // ~
|
|
|
|
|
"hash" | "hashtag" | "pound_sign" | "sharp" | "root" => Some("#".into()), // #
|
2020-09-05 18:13:07 +00:00
|
|
|
|
|
|
|
|
|
// Weather symbols
|
2020-10-22 20:10:19 +00:00
|
|
|
|
"sun" | "sunny" | "sunrise" => Some("☀️".to_string()),
|
|
|
|
|
"moon" => Some("🌛".to_string()),
|
|
|
|
|
"cloudy" | "cloud" | "clouds" => Some("☁️".to_string()),
|
|
|
|
|
"rainy" | "rain" => Some("🌦️".to_string()),
|
|
|
|
|
"foggy" | "fog" => Some("🌫️".to_string()),
|
|
|
|
|
"mist" | "haze" => Some("\u{2591}".to_string()),
|
|
|
|
|
"snowy" | "snow" => Some("❄️".to_string()),
|
|
|
|
|
"thunderstorm" | "thunder" => Some("🌩️".to_string()),
|
2020-09-15 21:01:57 +00:00
|
|
|
|
|
2020-06-27 21:46:30 +00:00
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::Char;
|
2020-10-03 14:06:02 +00:00
|
|
|
|
use super::ShellError;
|
2020-06-27 21:46:30 +00:00
|
|
|
|
|
|
|
|
|
#[test]
|
2020-10-03 14:06:02 +00:00
|
|
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
2020-06-27 21:46:30 +00:00
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
|
2021-02-12 10:13:14 +00:00
|
|
|
|
test_examples(Char {})
|
2020-06-27 21:46:30 +00:00
|
|
|
|
}
|
|
|
|
|
}
|