removed unwraps (#430)

This commit is contained in:
Fernando Herrera
2021-12-04 12:38:21 +00:00
committed by GitHub
parent eed22605ef
commit 8a06ea133b
24 changed files with 233 additions and 159 deletions

View File

@ -5,7 +5,8 @@ use nu_engine::CallExt;
use nu_protocol::{
ast::{Call, PathMember},
engine::{Command, EngineState, Stack},
Category, Config, IntoPipelineData, PipelineData, Signature, Span, SyntaxShape, Value,
Category, Config, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape,
Value,
};
use nu_term_grid::grid::{Alignment, Cell, Direction, Filling, Grid, GridOptions};
use terminal_size::{Height, Width};
@ -76,7 +77,7 @@ prints out the list properly."#
separator_param,
env_str,
use_grid_icons,
))
)?)
} else {
Ok(PipelineData::new(call.head))
}
@ -93,7 +94,7 @@ prints out the list properly."#
separator_param,
env_str,
use_grid_icons,
))
)?)
} else {
// dbg!(data);
Ok(PipelineData::new(call.head))
@ -115,7 +116,7 @@ prints out the list properly."#
separator_param,
env_str,
use_grid_icons,
))
)?)
}
x => {
// dbg!("other value");
@ -142,7 +143,7 @@ fn create_grid_output(
separator_param: Option<String>,
env_str: Option<String>,
use_grid_icons: bool,
) -> PipelineData {
) -> Result<PipelineData, ShellError> {
let ls_colors = match env_str {
Some(s) => LsColors::from_string(&s),
None => LsColors::default(),
@ -173,7 +174,7 @@ fn create_grid_output(
if use_grid_icons {
let no_ansi = strip_ansi(&value);
let path = std::path::Path::new(&no_ansi);
let icon = icon_for_file(path);
let icon = icon_for_file(path)?;
let ls_colors_style = ls_colors.style_for_path(path);
// eprintln!("ls_colors_style: {:?}", &ls_colors_style);
@ -212,18 +213,20 @@ fn create_grid_output(
}
}
if let Some(grid_display) = grid.fit_into_width(cols as usize) {
Value::String {
val: grid_display.to_string(),
span: call.head,
Ok(
if let Some(grid_display) = grid.fit_into_width(cols as usize) {
Value::String {
val: grid_display.to_string(),
span: call.head,
}
} else {
Value::String {
val: format!("Couldn't fit grid into {} columns!", cols),
span: call.head,
}
}
} else {
Value::String {
val: format!("Couldn't fit grid into {} columns!", cols),
span: call.head,
}
}
.into_pipeline_data()
.into_pipeline_data(),
)
}
fn convert_to_list(

View File

@ -1,4 +1,5 @@
use lazy_static::lazy_static;
use nu_protocol::{ShellError, Span};
use std::collections::HashMap;
use std::path::Path;
@ -129,23 +130,47 @@ lazy_static! {
};
}
pub fn icon_for_file(file_path: &Path) -> char {
pub fn icon_for_file(file_path: &Path) -> Result<char, ShellError> {
let extensions = Box::new(FileExtensions);
let fp = format!("{}", file_path.display());
if let Some(icon) = MAP_BY_NAME.get(&fp[..]) {
*icon
Ok(*icon)
} else if file_path.is_dir() {
match file_path.file_name().unwrap().to_str().unwrap() {
let str = file_path
.file_name()
.ok_or_else(|| {
ShellError::LabeledError(
"File name error".into(),
"Unable to get file name".into(),
Span::unknown(),
)
})?
.to_str()
.ok_or_else(|| {
ShellError::LabeledError(
"Unable to get str error".into(),
"Unable to convert to str file name".into(),
Span::unknown(),
)
})?;
Ok(match str {
"bin" => '\u{e5fc}', // 
".git" => '\u{f1d3}', // 
".idea" => '\u{e7b5}', // 
_ => '\u{f115}', // 
}
})
} else if let Some(icon) = extensions.icon_file(file_path) {
icon
Ok(icon)
} else if let Some(ext) = file_path.extension().as_ref() {
match ext.to_str().unwrap() {
let str = ext.to_str().ok_or_else(|| {
ShellError::LabeledError(
"Unable to get str error".into(),
"Unable to convert to str file name".into(),
Span::unknown(),
)
})?;
Ok(match str {
"ai" => '\u{e7b4}', // 
"android" => '\u{e70e}', // 
"apk" => '\u{e70e}', // 
@ -372,9 +397,9 @@ pub fn icon_for_file(file_path: &Path) -> char {
"zsh-theme" => '\u{f489}', // 
"zshrc" => '\u{f489}', // 
_ => '\u{f15b}', // 
}
})
} else {
'\u{f016}'
Ok('\u{f016}')
}
}