Fix panic when loading bad text in the GUI.
This commit is contained in:
@@ -36,6 +36,7 @@ pub mod qobject {
|
||||
#[qproperty(QColor, explorer_background)]
|
||||
#[qproperty(QColor, explorer_folder)]
|
||||
#[qproperty(QColor, explorer_folder_open)]
|
||||
#[qproperty(QColor, terminal_background)]
|
||||
type RustColors = super::RustColorsImpl;
|
||||
}
|
||||
}
|
||||
@@ -65,6 +66,7 @@ pub struct RustColorsImpl {
|
||||
explorer_background: QColor,
|
||||
explorer_folder: QColor,
|
||||
explorer_folder_open: QColor,
|
||||
terminal_background: QColor,
|
||||
}
|
||||
|
||||
impl Default for RustColorsImpl {
|
||||
@@ -87,11 +89,12 @@ impl Default for RustColorsImpl {
|
||||
editor_highlight: QColor::try_from("#ccced3").unwrap(),
|
||||
gutter: QColor::try_from("#1e1f22").unwrap(),
|
||||
explorer_hovered: QColor::try_from("#4c5053").unwrap(),
|
||||
explorer_text: QColor::try_from("#3b3b3b").unwrap(),
|
||||
explorer_text: QColor::try_from("#FFF").unwrap(),
|
||||
explorer_text_selected: QColor::try_from("#262626").unwrap(),
|
||||
explorer_background: QColor::try_from("#1E1F22").unwrap(),
|
||||
explorer_folder: QColor::try_from("#54585b").unwrap(),
|
||||
explorer_folder_open: QColor::try_from("#2b2b2b").unwrap(),
|
||||
terminal_background: QColor::try_from("#2C2E32").unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,14 +48,16 @@ use dirs;
|
||||
use log::warn;
|
||||
use std::fs;
|
||||
use std::io::BufRead;
|
||||
use syntect::easy::HighlightFile;
|
||||
use std::path::Path;
|
||||
use syntect::easy::{HighlightFile, HighlightLines};
|
||||
use syntect::highlighting::ThemeSet;
|
||||
use syntect::html::{
|
||||
IncludeBackground, append_highlighted_html_for_styled_line, start_highlighted_html_snippet,
|
||||
};
|
||||
use syntect::parsing::SyntaxSet;
|
||||
use syntect::util::LinesWithEndings;
|
||||
|
||||
// TODO: Impleent a provider for QFileSystemModel::setIconProvider for icons.
|
||||
// TODO: Implement a provider for QFileSystemModel::setIconProvider for icons.
|
||||
pub struct FileSystemImpl {
|
||||
file_path: QString,
|
||||
root_index: QModelIndex,
|
||||
@@ -76,42 +78,46 @@ impl qobject::FileSystem {
|
||||
if path.is_empty() {
|
||||
return QString::default();
|
||||
}
|
||||
if !fs::metadata(path.to_string())
|
||||
.expect(format!("Failed to get file metadata {path:?}").as_str())
|
||||
.is_file()
|
||||
{
|
||||
let meta = fs::metadata(path.to_string())
|
||||
.expect(format!("Failed to get file metadata {path:?}").as_str());
|
||||
if !meta.is_file() {
|
||||
warn!(target:"FileSystem", "Attempted to open file {path:?} that is not a valid file");
|
||||
return QString::default();
|
||||
}
|
||||
let ss = SyntaxSet::load_defaults_nonewlines();
|
||||
let ts = ThemeSet::load_defaults();
|
||||
let theme = &ts.themes["base16-ocean.dark"];
|
||||
let path_str = path.to_string();
|
||||
if let Ok(lines) = fs::read_to_string(path_str.as_str()) {
|
||||
let ss = SyntaxSet::load_defaults_nonewlines();
|
||||
let ts = ThemeSet::load_defaults();
|
||||
let theme = &ts.themes["base16-ocean.dark"];
|
||||
let lang = ss
|
||||
.find_syntax_by_extension(
|
||||
Path::new(path_str.as_str())
|
||||
.extension()
|
||||
.map(|s| s.to_str())
|
||||
.unwrap_or_else(|| Some("md"))
|
||||
.expect("Failed to get file extension"),
|
||||
)
|
||||
.unwrap_or_else(|| ss.find_syntax_plain_text());
|
||||
let mut highlighter = HighlightLines::new(lang, theme);
|
||||
let (mut output, _bg) = start_highlighted_html_snippet(theme);
|
||||
for line in LinesWithEndings::from(lines.as_str()) {
|
||||
let regions = highlighter
|
||||
.highlight_line(line, &ss)
|
||||
.expect("Failed to highlight");
|
||||
|
||||
let mut highlighter =
|
||||
HighlightFile::new(path.to_string(), &ss, theme).expect("Failed to create highlighter");
|
||||
let (mut output, _bg) = start_highlighted_html_snippet(theme);
|
||||
let mut line = String::new();
|
||||
while highlighter
|
||||
.reader
|
||||
.read_line(&mut line)
|
||||
.expect("Failed to read file.")
|
||||
> 0
|
||||
{
|
||||
let regions = highlighter
|
||||
.highlight_lines
|
||||
.highlight_line(&line, &ss)
|
||||
.expect("Failed to highlight");
|
||||
append_highlighted_html_for_styled_line(
|
||||
®ions[..],
|
||||
IncludeBackground::Yes,
|
||||
&mut output,
|
||||
)
|
||||
.expect("Failed to insert highlighted html");
|
||||
}
|
||||
|
||||
append_highlighted_html_for_styled_line(
|
||||
®ions[..],
|
||||
IncludeBackground::Yes,
|
||||
&mut output,
|
||||
)
|
||||
.expect("Failed to insert highlighted html");
|
||||
line.clear();
|
||||
output.push_str("</pre>\n");
|
||||
QString::from(output)
|
||||
} else {
|
||||
return QString::default();
|
||||
}
|
||||
output.push_str("</pre>\n");
|
||||
QString::from(output)
|
||||
}
|
||||
|
||||
// There will never be more than one column.
|
||||
|
||||
Reference in New Issue
Block a user