Add some checks before reading file.

This commit is contained in:
2025-03-31 19:26:04 -04:00
parent b426b88b79
commit 365940267f
3 changed files with 36 additions and 21 deletions

View File

@@ -77,7 +77,7 @@ impl Default for RustColorsImpl {
active: QColor::try_from("#a9acb0").unwrap(),
inactive: QColor::try_from("#FFF").unwrap(),
editor_background: QColor::try_from("#2b2b2b").unwrap(),
editor_text: QColor::try_from("#ccced3").unwrap(),
editor_text: QColor::try_from("#acaea3").unwrap(),
editor_highlighted_text: QColor::try_from("#ccced3").unwrap(),
editor_highlight: QColor::try_from("#ccced3").unwrap(),
gutter: QColor::try_from("#1e1f22").unwrap(),

View File

@@ -42,6 +42,8 @@ pub mod qobject {
use cxx_qt_lib::{QModelIndex, QString};
use dirs;
use std::fs;
use std::fs::FileType;
use log::warn;
// TODO: Impleent a provider for QFileSystemModel::setIconProvider for icons.
pub struct FileSystemImpl {
@@ -64,11 +66,20 @@ impl qobject::FileSystem {
if path.is_empty() {
return QString::default();
}
// TODO: What if the file is binary or something horrible?
QString::from(
fs::read_to_string(path.to_string())
.expect(format!("Failed to read file {}", path).as_str()),
)
// TODO: Use syntect for syntax highlighting?
// https://github.com/trishume/syntect/blob/master/examples/syncat.rs
if fs::metadata(path.to_string())
.expect(format!("Failed to get file metadata {}", path).as_str())
.is_file()
{
QString::from(
fs::read_to_string(path.to_string())
.expect(format!("Failed to read file {}", path).as_str()),
)
} else {
warn!("Attempted to open file {} that is not a valid file", path);
QString::default()
}
}
// There will never be more than one column.