Merge pull request #231 from jonathandturner/open_improvements

Various open improvements
This commit is contained in:
Jonathan Turner 2019-07-30 16:03:05 +12:00 committed by GitHub
commit df8b195aac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 46 deletions

View file

@ -45,9 +45,9 @@ pub fn from_ini(args: CommandArgs) -> Result<OutputStream, ShellError> {
.map(move |a| match a.item { .map(move |a| match a.item {
Value::Primitive(Primitive::String(s)) => match from_ini_string_to_value(s, span) { Value::Primitive(Primitive::String(s)) => match from_ini_string_to_value(s, span) {
Ok(x) => ReturnSuccess::value(x.spanned(a.span)), Ok(x) => ReturnSuccess::value(x.spanned(a.span)),
Err(e) => Err(ShellError::maybe_labeled_error( Err(_) => Err(ShellError::maybe_labeled_error(
"Could not parse as INI", "Could not parse as INI",
format!("{:#?}", e), "piped data failed INI parse",
span, span,
)), )),
}, },

View file

@ -148,7 +148,10 @@ pub fn fetch(
} }
(ty, sub_ty) => Ok(( (ty, sub_ty) => Ok((
None, None,
Value::string(format!("Not yet support MIME type: {} {}", ty, sub_ty)), Value::string(format!(
"Not yet supported MIME type: {} {}",
ty, sub_ty
)),
Span::unknown_with_uuid(Uuid::new_v4()), Span::unknown_with_uuid(Uuid::new_v4()),
SpanSource::Url(r.url().to_string()), SpanSource::Url(r.url().to_string()),
)), )),

View file

@ -59,7 +59,7 @@ fn paint_textview(
let mut pos = 0; let mut pos = 0;
let width = size.0 as usize; let width = size.0 as usize;
let height = size.1 as usize - 1; let height = size.1 as usize - 1;
let mut frame_buffer = vec![]; //(' ', 0, 0, 0); max_pos]; let mut frame_buffer = vec![];
for command in draw_commands { for command in draw_commands {
match command { match command {
@ -137,10 +137,12 @@ fn scroll_view_lines_if_needed(draw_commands: Vec<DrawCommand>, use_color_buffer
let terminal = terminal(); let terminal = terminal();
let mut size = terminal.terminal_size(); let mut size = terminal.terminal_size();
let height = size.1 as usize - 1;
let mut max_bottom_line = paint_textview(&draw_commands, starting_row, use_color_buffer); let mut max_bottom_line = paint_textview(&draw_commands, starting_row, use_color_buffer);
// Only scroll if needed // Only scroll if needed
if max_bottom_line > size.1 as usize { if max_bottom_line > height as usize {
loop { loop {
if rawkey.is_pressed(rawkey::KeyCode::Escape) { if rawkey.is_pressed(rawkey::KeyCode::Escape) {
break; break;
@ -153,23 +155,23 @@ fn scroll_view_lines_if_needed(draw_commands: Vec<DrawCommand>, use_color_buffer
} }
} }
if rawkey.is_pressed(rawkey::KeyCode::DownArrow) { if rawkey.is_pressed(rawkey::KeyCode::DownArrow) {
if starting_row < (max_bottom_line - size.1 as usize) { if starting_row < (max_bottom_line - height) {
starting_row += 1; starting_row += 1;
} }
max_bottom_line = max_bottom_line =
paint_textview(&draw_commands, starting_row, use_color_buffer); paint_textview(&draw_commands, starting_row, use_color_buffer);
} }
if rawkey.is_pressed(rawkey::KeyCode::PageUp) { if rawkey.is_pressed(rawkey::KeyCode::PageUp) {
starting_row -= std::cmp::min(size.1 as usize, starting_row); starting_row -= std::cmp::min(height, starting_row);
max_bottom_line = max_bottom_line =
paint_textview(&draw_commands, starting_row, use_color_buffer); paint_textview(&draw_commands, starting_row, use_color_buffer);
} }
if rawkey.is_pressed(rawkey::KeyCode::PageDown) { if rawkey.is_pressed(rawkey::KeyCode::PageDown) {
if starting_row < (max_bottom_line - size.1 as usize) { if starting_row < (max_bottom_line - height) {
starting_row += size.1 as usize; starting_row += height;
if starting_row > (max_bottom_line - size.1 as usize) { if starting_row > (max_bottom_line - height) {
starting_row = max_bottom_line - size.1 as usize; starting_row = max_bottom_line - height;
} }
} }
max_bottom_line = max_bottom_line =
@ -198,7 +200,6 @@ fn scroll_view_lines_if_needed(draw_commands: Vec<DrawCommand>, use_color_buffer
let screen = RawScreen::disable_raw_mode(); let screen = RawScreen::disable_raw_mode();
println!(""); println!("");
//thread::sleep(Duration::from_millis(50));
} }
fn scroll_view(s: &str) { fn scroll_view(s: &str) {
@ -219,46 +220,57 @@ fn view_text_value(value: &Spanned<Value>, source_map: &SourceMap) {
let source = span.source.map(|x| source_map.get(&x)).flatten(); let source = span.source.map(|x| source_map.get(&x)).flatten();
if let Some(source) = source { if let Some(source) = source {
match source { let extension: Option<String> = match source {
SpanSource::File(file) => { SpanSource::File(file) => {
let path = Path::new(file); let path = Path::new(file);
match path.extension() { path.extension().map(|x| x.to_string_lossy().to_string())
Some(extension) => { }
// Load these once at the start of your program SpanSource::Url(url) => {
let ps: SyntaxSet = syntect::dumps::from_binary(include_bytes!( let url = reqwest::Url::parse(url);
"../../assets/syntaxes.bin" if let Ok(url) = url {
)); let url = url.clone();
if let Some(mut segments) = url.path_segments() {
if let Some(syntax) = if let Some(file) = segments.next_back() {
ps.find_syntax_by_extension(extension.to_str().unwrap()) let path = Path::new(file);
{ path.extension().map(|x| x.to_string_lossy().to_string())
let ts: ThemeSet = syntect::dumps::from_binary(include_bytes!(
"../../assets/themes.bin"
));
let mut h =
HighlightLines::new(syntax, &ts.themes["OneHalfDark"]);
let mut v = vec![];
for line in s.lines() {
let ranges: Vec<(Style, &str)> = h.highlight(line, &ps);
for range in ranges {
v.push(DrawCommand::DrawString(
range.0,
range.1.to_string(),
));
}
v.push(DrawCommand::NextLine);
}
scroll_view_lines_if_needed(v, true);
} else { } else {
scroll_view(s); None
} }
} else {
None
} }
_ => { } else {
scroll_view(s); None
}
}
};
match extension {
Some(extension) => {
// Load these once at the start of your program
let ps: SyntaxSet = syntect::dumps::from_binary(include_bytes!(
"../../assets/syntaxes.bin"
));
if let Some(syntax) = ps.find_syntax_by_extension(&extension) {
let ts: ThemeSet = syntect::dumps::from_binary(include_bytes!(
"../../assets/themes.bin"
));
let mut h = HighlightLines::new(syntax, &ts.themes["OneHalfDark"]);
let mut v = vec![];
for line in s.lines() {
let ranges: Vec<(Style, &str)> = h.highlight(line, &ps);
for range in ranges {
v.push(DrawCommand::DrawString(range.0, range.1.to_string()));
}
v.push(DrawCommand::NextLine);
} }
scroll_view_lines_if_needed(v, true);
} else {
scroll_view(s);
} }
} }
_ => { _ => {