Fix panic when loading bad text in the GUI.

This commit is contained in:
Shaun Reed 2026-02-01 17:15:21 -05:00
parent 0fac2b71ab
commit a5bed9ed2c
5 changed files with 54 additions and 41 deletions

View File

@ -153,12 +153,13 @@ SplitView {
id: areaConsole id: areaConsole
height: 100 height: 100
placeholderText: qsTr("Placeholder for bash terminal.") width: parent.width
placeholderText: qsTr("shaun@pc:~/Code/clide$ ")
placeholderTextColor: "white" placeholderTextColor: "white"
readOnly: true readOnly: true
wrapMode: TextArea.Wrap wrapMode: TextArea.Wrap
background: Rectangle { background: Rectangle {
color: RustColors.editor_background color: RustColors.terminal_background
implicitHeight: 100 implicitHeight: 100
// border.color: control.enabled ? RustColors.active : RustColors.inactive // border.color: control.enabled ? RustColors.active : RustColors.inactive
} }

View File

@ -45,22 +45,25 @@ SplitView {
ColumnLayout { ColumnLayout {
spacing: 2 spacing: 2
// TODO: Make a ClideBreadCrumb element to support select parent paths as root
Rectangle { Rectangle {
width: navigationView.width width: navigationView.width
height: breadCrumb.height + 5 height: 25
color: RustColors.explorer_background
color: RustColors.explorer_text
Text { Text {
id: breadCrumb id: breadCrumb
anchors.fill: parent
text: clideTreeView.rootDirectory text: clideTreeView.rootDirectory
color: RustColors.explorer_text
elide: Text.ElideLeft elide: Text.ElideLeft
horizontalAlignment: Text.AlignHCenter horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
} }
} }
ClideTreeView { ClideTreeView {
id: clideTreeView id: clideTreeView
onFileClicked: path => root.projectDir = path onFileClicked: path => clideEditor.filePath = path
width: navigationView.width width: navigationView.width
height: navigationView.height height: navigationView.height
@ -70,6 +73,7 @@ SplitView {
} }
} }
ClideEditor { ClideEditor {
id: clideEditor
SplitView.fillWidth: true SplitView.fillWidth: true
// Provide a path to the file currently open in the text editor. // Provide a path to the file currently open in the text editor.

View File

@ -119,11 +119,10 @@ TreeView {
switch (button) { switch (button) {
case Qt.LeftButton: case Qt.LeftButton:
fileSystemTreeView.toggleExpanded(treeDelegate.row) fileSystemTreeView.toggleExpanded(treeDelegate.row)
fileSystemTreeView.lastIndex = treeDelegate.index
// If this model item doesn't have children, it means it's // If this model item doesn't have children, it means it's
// representing a file. // representing a file.
if (!treeDelegate.hasChildren) if (!treeDelegate.hasChildren)
root.fileClicked(treeDelegate.filePath) fileSystemTreeView.fileClicked(treeDelegate.filePath)
break; break;
case Qt.RightButton: case Qt.RightButton:
if (treeDelegate.hasChildren) if (treeDelegate.hasChildren)

View File

@ -36,6 +36,7 @@ pub mod qobject {
#[qproperty(QColor, explorer_background)] #[qproperty(QColor, explorer_background)]
#[qproperty(QColor, explorer_folder)] #[qproperty(QColor, explorer_folder)]
#[qproperty(QColor, explorer_folder_open)] #[qproperty(QColor, explorer_folder_open)]
#[qproperty(QColor, terminal_background)]
type RustColors = super::RustColorsImpl; type RustColors = super::RustColorsImpl;
} }
} }
@ -65,6 +66,7 @@ pub struct RustColorsImpl {
explorer_background: QColor, explorer_background: QColor,
explorer_folder: QColor, explorer_folder: QColor,
explorer_folder_open: QColor, explorer_folder_open: QColor,
terminal_background: QColor,
} }
impl Default for RustColorsImpl { impl Default for RustColorsImpl {
@ -87,11 +89,12 @@ impl Default for RustColorsImpl {
editor_highlight: QColor::try_from("#ccced3").unwrap(), editor_highlight: QColor::try_from("#ccced3").unwrap(),
gutter: QColor::try_from("#1e1f22").unwrap(), gutter: QColor::try_from("#1e1f22").unwrap(),
explorer_hovered: QColor::try_from("#4c5053").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_text_selected: QColor::try_from("#262626").unwrap(),
explorer_background: QColor::try_from("#1E1F22").unwrap(), explorer_background: QColor::try_from("#1E1F22").unwrap(),
explorer_folder: QColor::try_from("#54585b").unwrap(), explorer_folder: QColor::try_from("#54585b").unwrap(),
explorer_folder_open: QColor::try_from("#2b2b2b").unwrap(), explorer_folder_open: QColor::try_from("#2b2b2b").unwrap(),
terminal_background: QColor::try_from("#2C2E32").unwrap(),
} }
} }
} }

View File

@ -48,14 +48,16 @@ use dirs;
use log::warn; use log::warn;
use std::fs; use std::fs;
use std::io::BufRead; use std::io::BufRead;
use syntect::easy::HighlightFile; use std::path::Path;
use syntect::easy::{HighlightFile, HighlightLines};
use syntect::highlighting::ThemeSet; use syntect::highlighting::ThemeSet;
use syntect::html::{ use syntect::html::{
IncludeBackground, append_highlighted_html_for_styled_line, start_highlighted_html_snippet, IncludeBackground, append_highlighted_html_for_styled_line, start_highlighted_html_snippet,
}; };
use syntect::parsing::SyntaxSet; 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 { pub struct FileSystemImpl {
file_path: QString, file_path: QString,
root_index: QModelIndex, root_index: QModelIndex,
@ -76,42 +78,46 @@ impl qobject::FileSystem {
if path.is_empty() { if path.is_empty() {
return QString::default(); return QString::default();
} }
if !fs::metadata(path.to_string()) let meta = fs::metadata(path.to_string())
.expect(format!("Failed to get file metadata {path:?}").as_str()) .expect(format!("Failed to get file metadata {path:?}").as_str());
.is_file() if !meta.is_file() {
{
warn!(target:"FileSystem", "Attempted to open file {path:?} that is not a valid file"); warn!(target:"FileSystem", "Attempted to open file {path:?} that is not a valid file");
return QString::default(); return QString::default();
} }
let ss = SyntaxSet::load_defaults_nonewlines(); let path_str = path.to_string();
let ts = ThemeSet::load_defaults(); if let Ok(lines) = fs::read_to_string(path_str.as_str()) {
let theme = &ts.themes["base16-ocean.dark"]; 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 = append_highlighted_html_for_styled_line(
HighlightFile::new(path.to_string(), &ss, theme).expect("Failed to create highlighter"); &regions[..],
let (mut output, _bg) = start_highlighted_html_snippet(theme); IncludeBackground::Yes,
let mut line = String::new(); &mut output,
while highlighter )
.reader .expect("Failed to insert highlighted html");
.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( output.push_str("</pre>\n");
&regions[..], QString::from(output)
IncludeBackground::Yes, } else {
&mut output, return QString::default();
)
.expect("Failed to insert highlighted html");
line.clear();
} }
output.push_str("</pre>\n");
QString::from(output)
} }
// There will never be more than one column. // There will never be more than one column.