Compare commits
1 Commits
01de2390ac
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 05cbe05cc0 |
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -1171,8 +1171,10 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"devicons",
|
"devicons",
|
||||||
|
"libclide-macros",
|
||||||
"log",
|
"log",
|
||||||
"strum",
|
"strum",
|
||||||
|
"syntect",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
# CLIDE
|
# CLIDE
|
||||||
|
[](https://github.com/shaunrd0/clide)
|
||||||
|
[](https://gitlab.com/shaunrd0/clide)
|
||||||
|
[](https://git.shaunreed.com/shaunrd0/clide)
|
||||||
|
|
||||||
[](https://github.com/shaunrd0/clide/actions/workflows/check.yaml)
|
[](https://github.com/shaunrd0/clide/actions/workflows/check.yaml)
|
||||||
|
|
||||||
@@ -38,7 +41,7 @@ export QMAKE=$HOME/Qt/6.7.3/gcc_64/bin/qmake6
|
|||||||
export LD_LIBRARY_PATH=$HOME/Qt/6.7.3/gcc_64/lib
|
export LD_LIBRARY_PATH=$HOME/Qt/6.7.3/gcc_64/lib
|
||||||
```
|
```
|
||||||
|
|
||||||
Though environment variables set using `export` will take precedence, these can also be set in [.cargo/config.toml](./.cargo/config.toml) for conveinence
|
Though environment variables set using `export` will take precedence, these can also be set in [.cargo/config.toml](./.cargo/config.toml) for convenience
|
||||||
|
|
||||||
```toml
|
```toml
|
||||||
[env]
|
[env]
|
||||||
|
|||||||
@@ -6,24 +6,17 @@ use proc_macro::TokenStream;
|
|||||||
use quote::quote;
|
use quote::quote;
|
||||||
use syn::{ItemStruct, parse_macro_input};
|
use syn::{ItemStruct, parse_macro_input};
|
||||||
|
|
||||||
#[proc_macro_attribute]
|
#[proc_macro_derive(Loggable)]
|
||||||
pub fn log_id(_attr: TokenStream, item: TokenStream) -> TokenStream {
|
pub fn loggable(item: TokenStream) -> TokenStream {
|
||||||
let input = parse_macro_input!(item as ItemStruct);
|
let input = parse_macro_input!(item as ItemStruct);
|
||||||
|
|
||||||
let struct_name = &input.ident;
|
let struct_name = &input.ident;
|
||||||
let generics = &input.generics;
|
let generics = &input.generics;
|
||||||
|
|
||||||
// This is the important part
|
|
||||||
let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
|
let (impl_generics, type_generics, where_clause) = generics.split_for_impl();
|
||||||
|
|
||||||
let struct_name_str = struct_name.to_string();
|
let struct_name_str = struct_name.to_string();
|
||||||
|
|
||||||
let expanded = quote! {
|
let expanded = quote! {
|
||||||
#input
|
impl #impl_generics libclide::log::Loggable for #struct_name #type_generics #where_clause {
|
||||||
|
const ID: &'static str = #struct_name_str;
|
||||||
impl #impl_generics #struct_name #type_generics #where_clause {
|
|
||||||
#[allow(unused)]
|
|
||||||
pub const ID: &'static str = #struct_name_str;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -8,3 +8,5 @@ anyhow = { workspace = true }
|
|||||||
strum = { workspace = true }
|
strum = { workspace = true }
|
||||||
log = { workspace = true }
|
log = { workspace = true }
|
||||||
devicons = { workspace = true }
|
devicons = { workspace = true }
|
||||||
|
libclide-macros = { path = "../libclide-macros" }
|
||||||
|
syntect = "5.3.0"
|
||||||
|
|||||||
@@ -4,15 +4,21 @@
|
|||||||
|
|
||||||
pub mod entry_meta;
|
pub mod entry_meta;
|
||||||
|
|
||||||
use devicons::FileIcon;
|
use anyhow::Context;
|
||||||
|
pub use entry_meta::icon;
|
||||||
|
use std::fs;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
pub fn icon<P: AsRef<str>>(p: P) -> FileIcon {
|
pub fn read_file<P: AsRef<Path>>(p: P) -> anyhow::Result<String> {
|
||||||
let path = p.as_ref();
|
let path = p.as_ref();
|
||||||
if Path::new(&path).is_dir() {
|
let meta =
|
||||||
// Ensures directories are given a folder icon and not mistakenly resolved to a language.
|
fs::metadata(path).unwrap_or_else(|_| panic!("Failed to get file metadata {path:?}"));
|
||||||
// For example, a directory named `cpp` would otherwise return a C++ icon.
|
if !meta.is_file() {
|
||||||
return FileIcon::from("dir/");
|
crate::warn!(target:"FileSystem", "Attempted to open file {path:?} that is not a valid file");
|
||||||
|
Err(anyhow::anyhow!(
|
||||||
|
"Attempted to open file {path:?} that is not a valid file"
|
||||||
|
))?;
|
||||||
}
|
}
|
||||||
FileIcon::from(path)
|
let path_str = path.to_string_lossy().to_string();
|
||||||
|
fs::read_to_string(path_str.as_str()).context(format!("Failed to read file {path:?}"))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,3 +52,13 @@ impl EntryMeta {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn icon<P: AsRef<str>>(p: P) -> FileIcon {
|
||||||
|
let path = p.as_ref();
|
||||||
|
if Path::new(&path).is_dir() {
|
||||||
|
// Ensures directories are given a folder icon and not mistakenly resolved to a language.
|
||||||
|
// For example, a directory named `cpp` would otherwise return a C++ icon.
|
||||||
|
return FileIcon::from("dir/");
|
||||||
|
}
|
||||||
|
FileIcon::from(path)
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,3 +3,8 @@
|
|||||||
// SPDX-License-Identifier: GNU General Public License v3.0 or later
|
// SPDX-License-Identifier: GNU General Public License v3.0 or later
|
||||||
|
|
||||||
pub mod macros;
|
pub mod macros;
|
||||||
|
|
||||||
|
pub use libclide_macros::Loggable;
|
||||||
|
pub trait Loggable {
|
||||||
|
const ID: &'static str;
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,111 +9,67 @@
|
|||||||
//! explicitly set. This is to avoid implicit pooling of log messages under the same default target,
|
//! explicitly set. This is to avoid implicit pooling of log messages under the same default target,
|
||||||
//! which can make it difficult to filter log messages by their source.
|
//! which can make it difficult to filter log messages by their source.
|
||||||
//!
|
//!
|
||||||
//! The target argument can be overridden using one of the following macros.
|
//! The Loggable trait can be implemented to automatically associate log messages with a struct.
|
||||||
//! ```
|
|
||||||
//! libclide::log!(target: "CustomTarget", "This log message will have the target 'CustomTarget'");
|
|
||||||
//! ```
|
//! ```
|
||||||
|
//! use libclide::log::Loggable;
|
||||||
//!
|
//!
|
||||||
//! The target argument will default to Self::ID if not provided.
|
//! #[derive(Loggable)]
|
||||||
//! This is an error if Self::ID is not defined, forcing you to use the explicit form.
|
|
||||||
//! ```
|
|
||||||
//! libclide::log!("This log message will use target Self::ID, the name of the struct it was invoked in");
|
|
||||||
//! ```
|
|
||||||
//!
|
|
||||||
//! Self::ID can be defined using the `#[log_id]` attribute macro, which will automatically generate
|
|
||||||
//! a constant ID field with the name of the struct as its value.
|
|
||||||
//! ```
|
|
||||||
//! #[log_id]
|
|
||||||
//! struct MyStruct;
|
//! struct MyStruct;
|
||||||
//! impl MyStruct {
|
//! impl MyStruct {
|
||||||
//! fn my_method(&self) {
|
//! fn my_method(&self) {
|
||||||
//! libclide::log!("This log message will use target Self::ID, which is 'MyStruct'");
|
//! libclide::info!("This log message will use target <Self as Loggable>::ID, which is 'MyStruct'");
|
||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
//! If the struct does not derive or implement Loggable, the target variant of the log macros must
|
||||||
|
//! be used instead.
|
||||||
|
//! ```
|
||||||
|
//! libclide::info!(target: "CustomTarget", "This log message will have the target 'CustomTarget'");
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! info {
|
macro_rules! info {
|
||||||
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
|
|
||||||
log::info!(logger: $logger, target: $target, $($arg)+)
|
|
||||||
});
|
|
||||||
|
|
||||||
(target: $target:expr, $($arg:tt)+) => ({
|
(target: $target:expr, $($arg:tt)+) => ({
|
||||||
log::info!(target: $target, $($arg)+)
|
log::info!(target: $target, $($arg)+)
|
||||||
});
|
});
|
||||||
|
|
||||||
(logger: $logger:expr, $($arg:tt)+) => ({
|
|
||||||
log::info!(logger: $logger, target: Self::ID, $($arg)+)
|
|
||||||
});
|
|
||||||
|
|
||||||
($($arg:tt)+) => (log::info!(target: Self::ID, $($arg)+))
|
($($arg:tt)+) => (log::info!(target: Self::ID, $($arg)+))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! debug {
|
macro_rules! debug {
|
||||||
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
|
|
||||||
log::debug!(logger: $logger, target: $target, $($arg)+)
|
|
||||||
});
|
|
||||||
|
|
||||||
(target: $target:expr, $($arg:tt)+) => ({
|
(target: $target:expr, $($arg:tt)+) => ({
|
||||||
log::debug!(target: $target, $($arg)+)
|
log::debug!(target: $target, $($arg)+)
|
||||||
});
|
});
|
||||||
|
|
||||||
(logger: $logger:expr, $($arg:tt)+) => ({
|
|
||||||
log::debug!(logger: $logger, target: Self::ID, $($arg)+)
|
|
||||||
});
|
|
||||||
|
|
||||||
($($arg:tt)+) => (log::debug!(target: Self::ID, $($arg)+))
|
($($arg:tt)+) => (log::debug!(target: Self::ID, $($arg)+))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! warn {
|
macro_rules! warn {
|
||||||
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
|
|
||||||
log::warn!(logger: $logger, target: $target, $($arg)+)
|
|
||||||
});
|
|
||||||
|
|
||||||
(target: $target:expr, $($arg:tt)+) => ({
|
(target: $target:expr, $($arg:tt)+) => ({
|
||||||
log::warn!(target: $target, $($arg)+)
|
log::warn!(target: $target, $($arg)+)
|
||||||
});
|
});
|
||||||
|
|
||||||
(logger: $logger:expr, $($arg:tt)+) => ({
|
|
||||||
log::warn!(logger: $logger, target: Self::ID, $($arg)+)
|
|
||||||
});
|
|
||||||
|
|
||||||
($($arg:tt)+) => (log::warn!(target: Self::ID, $($arg)+))
|
($($arg:tt)+) => (log::warn!(target: Self::ID, $($arg)+))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! error {
|
macro_rules! error {
|
||||||
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
|
|
||||||
log::error!(logger: $logger, target: $target, $($arg)+)
|
|
||||||
});
|
|
||||||
|
|
||||||
(target: $target:expr, $($arg:tt)+) => ({
|
(target: $target:expr, $($arg:tt)+) => ({
|
||||||
log::error!(target: $target, $($arg)+)
|
log::error!(target: $target, $($arg)+)
|
||||||
});
|
});
|
||||||
|
|
||||||
(logger: $logger:expr, $($arg:tt)+) => ({
|
|
||||||
log::error!(logger: $logger, target: Self::ID, $($arg)+)
|
|
||||||
});
|
|
||||||
|
|
||||||
($($arg:tt)+) => (log::error!(target: Self::ID, $($arg)+))
|
($($arg:tt)+) => (log::error!(target: Self::ID, $($arg)+))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! trace {
|
macro_rules! trace {
|
||||||
(logger: $logger:expr, target: $target:expr, $($arg:tt)+) => ({
|
|
||||||
log::trace!(logger: $logger, target: $target, $($arg)+)
|
|
||||||
});
|
|
||||||
|
|
||||||
(target: $target:expr, $($arg:tt)+) => ({
|
(target: $target:expr, $($arg:tt)+) => ({
|
||||||
log::trace!(target: $target, $($arg)+)
|
log::trace!(target: $target, $($arg)+)
|
||||||
});
|
});
|
||||||
|
|
||||||
(logger: $logger:expr, $($arg:tt)+) => ({
|
|
||||||
log::trace!(logger: $logger, target: Self::ID, $($arg)+)
|
|
||||||
});
|
|
||||||
|
|
||||||
($($arg:tt)+) => (log::trace!(target: Self::ID, $($arg)+))
|
($($arg:tt)+) => (log::trace!(target: Self::ID, $($arg)+))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,3 +3,4 @@
|
|||||||
// SPDX-License-Identifier: GNU General Public License v3.0 or later
|
// SPDX-License-Identifier: GNU General Public License v3.0 or later
|
||||||
|
|
||||||
pub mod colors;
|
pub mod colors;
|
||||||
|
pub mod highlighter;
|
||||||
|
|||||||
66
libclide/src/theme/highlighter.rs
Normal file
66
libclide/src/theme/highlighter.rs
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
use syntect::easy::HighlightLines;
|
||||||
|
use syntect::highlighting::ThemeSet;
|
||||||
|
use syntect::html::{IncludeBackground, append_highlighted_html_for_styled_line};
|
||||||
|
use syntect::parsing::SyntaxSet;
|
||||||
|
use syntect::util::LinesWithEndings;
|
||||||
|
|
||||||
|
pub struct Highlighter {
|
||||||
|
path: String,
|
||||||
|
ss: SyntaxSet,
|
||||||
|
ts: ThemeSet,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Highlighter {
|
||||||
|
pub fn new<P: AsRef<Path>>(p: P) -> anyhow::Result<Highlighter> {
|
||||||
|
let path = p.as_ref();
|
||||||
|
let meta =
|
||||||
|
fs::metadata(path).unwrap_or_else(|_| panic!("Failed to get file metadata {path:?}"));
|
||||||
|
let ss = SyntaxSet::load_defaults_nonewlines();
|
||||||
|
let ts = ThemeSet::load_defaults();
|
||||||
|
if !meta.is_file() {
|
||||||
|
crate::error!(target:"FileSystem", "Attempted to open file {path:?} that is not a valid file");
|
||||||
|
Err(anyhow::anyhow!(
|
||||||
|
"Attempted to open file {path:?} that is not a valid file"
|
||||||
|
))?;
|
||||||
|
}
|
||||||
|
Ok(Highlighter {
|
||||||
|
path: path.to_string_lossy().to_string(),
|
||||||
|
ss,
|
||||||
|
ts,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn syntax_highlight_text<P: AsRef<str>>(&self, p: P) -> String {
|
||||||
|
let text = p.as_ref();
|
||||||
|
let theme = &self.ts.themes["base16-ocean.dark"];
|
||||||
|
let lang = self
|
||||||
|
.ss
|
||||||
|
.find_syntax_by_extension(
|
||||||
|
Path::new(self.path.as_str())
|
||||||
|
.extension()
|
||||||
|
.map(|s| s.to_str())
|
||||||
|
.unwrap_or_else(|| Some("md"))
|
||||||
|
.expect("Failed to get file extension"),
|
||||||
|
)
|
||||||
|
.unwrap_or_else(|| self.ss.find_syntax_plain_text());
|
||||||
|
let mut highlighter = HighlightLines::new(lang, theme);
|
||||||
|
// If you care about the background, see `start_highlighted_html_snippet(theme);`.
|
||||||
|
let mut output = String::from("<pre>\n");
|
||||||
|
for line in LinesWithEndings::from(text) {
|
||||||
|
let regions = highlighter
|
||||||
|
.highlight_line(line, &self.ss)
|
||||||
|
.expect("Failed to highlight");
|
||||||
|
|
||||||
|
append_highlighted_html_for_styled_line(
|
||||||
|
®ions[..],
|
||||||
|
IncludeBackground::No,
|
||||||
|
&mut output,
|
||||||
|
)
|
||||||
|
.expect("Failed to insert highlighted html");
|
||||||
|
}
|
||||||
|
output.push_str("</pre>\n");
|
||||||
|
output
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,7 +14,6 @@ Rectangle {
|
|||||||
|
|
||||||
ListModel {
|
ListModel {
|
||||||
id: model
|
id: model
|
||||||
|
|
||||||
}
|
}
|
||||||
ListView {
|
ListView {
|
||||||
id: listView
|
id: listView
|
||||||
@@ -38,10 +37,10 @@ Rectangle {
|
|||||||
|
|
||||||
anchors.fill: parent
|
anchors.fill: parent
|
||||||
model: model
|
model: model
|
||||||
|
verticalLayoutDirection: ListView.BottomToTop
|
||||||
|
|
||||||
delegate: Text {
|
delegate: Text {
|
||||||
color: listView.getLogColor(level)
|
color: listView.getLogColor(level)
|
||||||
font.family: "monospace"
|
|
||||||
text: `[${level}] ${message}`
|
text: `[${level}] ${message}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,22 +18,6 @@ MenuBar {
|
|||||||
ClideMenu {
|
ClideMenu {
|
||||||
title: qsTr("&File")
|
title: qsTr("&File")
|
||||||
|
|
||||||
ClideMenuItem {
|
|
||||||
action: Action {
|
|
||||||
id: actionNewProject
|
|
||||||
|
|
||||||
text: qsTr("&New Project...")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ClideMenuItem {
|
|
||||||
action: Action {
|
|
||||||
id: actionOpen
|
|
||||||
|
|
||||||
text: qsTr("&Open...")
|
|
||||||
}
|
|
||||||
|
|
||||||
onTriggered: FileSystem.setDirectory(FileSystem.filePath)
|
|
||||||
}
|
|
||||||
ClideMenuItem {
|
ClideMenuItem {
|
||||||
action: Action {
|
action: Action {
|
||||||
id: actionSave
|
id: actionSave
|
||||||
@@ -41,6 +25,13 @@ MenuBar {
|
|||||||
text: qsTr("&Save")
|
text: qsTr("&Save")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ClideMenuItem {
|
||||||
|
action: Action {
|
||||||
|
id: actionReload
|
||||||
|
|
||||||
|
text: qsTr("&Reload")
|
||||||
|
}
|
||||||
|
}
|
||||||
MenuSeparator {
|
MenuSeparator {
|
||||||
background: Rectangle {
|
background: Rectangle {
|
||||||
border.color: color
|
border.color: color
|
||||||
@@ -67,37 +58,9 @@ MenuBar {
|
|||||||
|
|
||||||
ClideMenuItem {
|
ClideMenuItem {
|
||||||
action: Action {
|
action: Action {
|
||||||
id: actionUndo
|
id: actionCloseTab
|
||||||
|
|
||||||
text: qsTr("&Undo")
|
text: qsTr("&Close Tab")
|
||||||
}
|
|
||||||
}
|
|
||||||
ClideMenuItem {
|
|
||||||
action: Action {
|
|
||||||
id: actionRedo
|
|
||||||
|
|
||||||
text: qsTr("&Redo")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ClideMenuItem {
|
|
||||||
action: Action {
|
|
||||||
id: actionCut
|
|
||||||
|
|
||||||
text: qsTr("&Cut")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ClideMenuItem {
|
|
||||||
action: Action {
|
|
||||||
id: actionCopy
|
|
||||||
|
|
||||||
text: qsTr("&Copy")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ClideMenuItem {
|
|
||||||
action: Action {
|
|
||||||
id: actionPaste
|
|
||||||
|
|
||||||
text: qsTr("&Paste")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -132,13 +95,6 @@ MenuBar {
|
|||||||
ClideMenu {
|
ClideMenu {
|
||||||
title: qsTr("&Help")
|
title: qsTr("&Help")
|
||||||
|
|
||||||
ClideMenuItem {
|
|
||||||
action: Action {
|
|
||||||
id: actionDocumentation
|
|
||||||
|
|
||||||
text: qsTr("&Documentation")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ClideMenuItem {
|
ClideMenuItem {
|
||||||
action: Action {
|
action: Action {
|
||||||
id: actionAbout
|
id: actionAbout
|
||||||
|
|||||||
@@ -3,8 +3,6 @@
|
|||||||
<file alias="kilroy.png">resources/images/kilroy-256.png</file>
|
<file alias="kilroy.png">resources/images/kilroy-256.png</file>
|
||||||
</qresource>
|
</qresource>
|
||||||
<qresource prefix="/fonts">
|
<qresource prefix="/fonts">
|
||||||
<file alias="saucecodepro.ttf">resources/SauceCodeProNerdFont-Black.ttf</file>
|
|
||||||
<file alias="saucecodepro-light.ttf">resources/SauceCodeProNerdFont-Light.ttf</file>
|
|
||||||
<file alias="saucecodepro-xlight.ttf">resources/SauceCodeProNerdFont-ExtraLight.ttf</file>
|
<file alias="saucecodepro-xlight.ttf">resources/SauceCodeProNerdFont-ExtraLight.ttf</file>
|
||||||
</qresource>
|
</qresource>
|
||||||
</RCC>
|
</RCC>
|
||||||
Binary file not shown.
Binary file not shown.
@@ -4,13 +4,9 @@
|
|||||||
|
|
||||||
use cxx_qt_lib::{QModelIndex, QString};
|
use cxx_qt_lib::{QModelIndex, QString};
|
||||||
use dirs;
|
use dirs;
|
||||||
|
use libclide::error;
|
||||||
|
use libclide::theme::highlighter::Highlighter;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::path::Path;
|
|
||||||
use syntect::easy::HighlightLines;
|
|
||||||
use syntect::highlighting::ThemeSet;
|
|
||||||
use syntect::html::{IncludeBackground, append_highlighted_html_for_styled_line};
|
|
||||||
use syntect::parsing::SyntaxSet;
|
|
||||||
use syntect::util::LinesWithEndings;
|
|
||||||
|
|
||||||
#[cxx_qt::bridge]
|
#[cxx_qt::bridge]
|
||||||
pub mod qobject {
|
pub mod qobject {
|
||||||
@@ -70,49 +66,15 @@ impl Default for FileSystemImpl {
|
|||||||
|
|
||||||
impl qobject::FileSystem {
|
impl qobject::FileSystem {
|
||||||
fn read_file(&self, path: &QString) -> QString {
|
fn read_file(&self, path: &QString) -> QString {
|
||||||
if path.is_empty() {
|
let text = libclide::fs::read_file(path.to_string()).unwrap_or_else(|_| {
|
||||||
return QString::default();
|
error!(target: "qobject::FileSystem", "Failed to read file at path {path:?}");
|
||||||
}
|
String::default()
|
||||||
let meta = fs::metadata(path.to_string())
|
});
|
||||||
.unwrap_or_else(|_| panic!("Failed to get file metadata {path:?}"));
|
if let Ok(highlighter) = Highlighter::new(path.to_string()) {
|
||||||
if !meta.is_file() {
|
QString::from(highlighter.syntax_highlight_text(text))
|
||||||
libclide::warn!(target:"FileSystem", "Attempted to open file {path:?} that is not a valid file");
|
|
||||||
return QString::default();
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
// If you care about the background, see `start_highlighted_html_snippet(theme);`.
|
|
||||||
let mut output = String::from("<pre>\n");
|
|
||||||
for line in LinesWithEndings::from(lines.as_str()) {
|
|
||||||
let regions = highlighter
|
|
||||||
.highlight_line(line, &ss)
|
|
||||||
.expect("Failed to highlight");
|
|
||||||
|
|
||||||
append_highlighted_html_for_styled_line(
|
|
||||||
®ions[..],
|
|
||||||
IncludeBackground::No,
|
|
||||||
&mut output,
|
|
||||||
)
|
|
||||||
.expect("Failed to insert highlighted html");
|
|
||||||
}
|
|
||||||
|
|
||||||
output.push_str("</pre>\n");
|
|
||||||
QString::from(output)
|
|
||||||
} else {
|
} else {
|
||||||
QString::default()
|
error!(target: "qobject::FileSystem", "Failed to create highlighter");
|
||||||
|
QString::from(text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ mod menu_bar;
|
|||||||
|
|
||||||
use crate::AppContext;
|
use crate::AppContext;
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use libclide_macros::log_id;
|
use libclide::log::Loggable;
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use ratatui::Terminal;
|
use ratatui::Terminal;
|
||||||
use ratatui::backend::CrosstermBackend;
|
use ratatui::backend::CrosstermBackend;
|
||||||
@@ -29,20 +29,19 @@ use tui_logger::{
|
|||||||
TuiLoggerFile, TuiLoggerLevelOutput, init_logger, set_default_level, set_log_file,
|
TuiLoggerFile, TuiLoggerLevelOutput, init_logger, set_default_level, set_log_file,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[log_id]
|
#[derive(Loggable)]
|
||||||
struct Tui {
|
struct Tui {
|
||||||
terminal: Terminal<CrosstermBackend<Stdout>>,
|
terminal: Terminal<CrosstermBackend<Stdout>>,
|
||||||
root_path: std::path::PathBuf,
|
root_path: std::path::PathBuf,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(app_context: AppContext) -> Result<()> {
|
pub fn run(app_context: AppContext) -> Result<()> {
|
||||||
libclide::trace!(target:Tui::ID, "Starting TUI");
|
libclide::trace!(target: "clide::tui::run", "Starting TUI");
|
||||||
Tui::new(app_context)?.start()
|
Tui::new(app_context)?.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tui {
|
impl Tui {
|
||||||
fn new(app_context: AppContext) -> Result<Self> {
|
fn new(app_context: AppContext) -> Result<Self> {
|
||||||
libclide::trace!("Building {}", Self::ID);
|
|
||||||
init_logger(LevelFilter::Trace)?;
|
init_logger(LevelFilter::Trace)?;
|
||||||
set_default_level(LevelFilter::Trace);
|
set_default_level(LevelFilter::Trace);
|
||||||
libclide::debug!("Logging initialized");
|
libclide::debug!("Logging initialized");
|
||||||
|
|||||||
@@ -2,13 +2,13 @@
|
|||||||
//
|
//
|
||||||
// SPDX-License-Identifier: GNU General Public License v3.0 or later
|
// SPDX-License-Identifier: GNU General Public License v3.0 or later
|
||||||
|
|
||||||
use libclide_macros::log_id;
|
use libclide::log::Loggable;
|
||||||
use ratatui::buffer::Buffer;
|
use ratatui::buffer::Buffer;
|
||||||
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
use ratatui::layout::{Constraint, Direction, Layout, Rect};
|
||||||
use ratatui::text::{Line, Span};
|
use ratatui::text::{Line, Span};
|
||||||
use ratatui::widgets::{Block, Borders, Clear, Padding, Paragraph, Widget, Wrap};
|
use ratatui::widgets::{Block, Borders, Clear, Padding, Paragraph, Widget, Wrap};
|
||||||
|
|
||||||
#[log_id]
|
#[derive(Loggable)]
|
||||||
pub struct About {}
|
pub struct About {}
|
||||||
|
|
||||||
impl About {
|
impl About {
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ use crate::tui::explorer::Explorer;
|
|||||||
use crate::tui::logger::Logger;
|
use crate::tui::logger::Logger;
|
||||||
use crate::tui::menu_bar::MenuBar;
|
use crate::tui::menu_bar::MenuBar;
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{Context, Result};
|
||||||
use libclide_macros::log_id;
|
use libclide::log::Loggable;
|
||||||
use ratatui::DefaultTerminal;
|
use ratatui::DefaultTerminal;
|
||||||
use ratatui::buffer::Buffer;
|
use ratatui::buffer::Buffer;
|
||||||
use ratatui::crossterm::event;
|
use ratatui::crossterm::event;
|
||||||
@@ -30,7 +30,7 @@ pub enum AppComponent {
|
|||||||
MenuBar,
|
MenuBar,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[log_id]
|
#[derive(Loggable)]
|
||||||
pub struct App<'a> {
|
pub struct App<'a> {
|
||||||
editor_tab: EditorTab,
|
editor_tab: EditorTab,
|
||||||
explorer: Explorer<'a>,
|
explorer: Explorer<'a>,
|
||||||
@@ -42,7 +42,7 @@ pub struct App<'a> {
|
|||||||
|
|
||||||
impl<'a> App<'a> {
|
impl<'a> App<'a> {
|
||||||
pub fn new(root_path: PathBuf) -> Result<Self> {
|
pub fn new(root_path: PathBuf) -> Result<Self> {
|
||||||
libclide::trace!(target:Self::ID, "Building {}", Self::ID);
|
libclide::trace!("Building {}", Self::ID);
|
||||||
let app = Self {
|
let app = Self {
|
||||||
editor_tab: EditorTab::new(),
|
editor_tab: EditorTab::new(),
|
||||||
explorer: Explorer::new(&root_path)?,
|
explorer: Explorer::new(&root_path)?,
|
||||||
@@ -56,13 +56,13 @@ impl<'a> App<'a> {
|
|||||||
|
|
||||||
/// Logic that should be executed once on application startup.
|
/// Logic that should be executed once on application startup.
|
||||||
pub fn start(&mut self) -> Result<()> {
|
pub fn start(&mut self) -> Result<()> {
|
||||||
libclide::trace!(target:Self::ID, "Starting App");
|
libclide::trace!("Starting App");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
pub fn run(mut self, mut terminal: DefaultTerminal) -> Result<()> {
|
||||||
self.start()?;
|
self.start()?;
|
||||||
libclide::trace!(target:Self::ID, "Entering App run loop");
|
libclide::trace!("Entering App run loop");
|
||||||
loop {
|
loop {
|
||||||
terminal.draw(|f| {
|
terminal.draw(|f| {
|
||||||
f.render_widget(&mut self, f.area());
|
f.render_widget(&mut self, f.area());
|
||||||
@@ -88,7 +88,7 @@ impl<'a> App<'a> {
|
|||||||
Some(editor) => editor.component_state.help_text.clone(),
|
Some(editor) => editor.component_state.help_text.clone(),
|
||||||
None => {
|
None => {
|
||||||
if !self.editor_tab.is_empty() {
|
if !self.editor_tab.is_empty() {
|
||||||
libclide::error!(target:Self::ID, "Failed to get Editor while drawing bottom status bar");
|
libclide::error!("Failed to get Editor while drawing bottom status bar");
|
||||||
}
|
}
|
||||||
"Failed to get current Editor while getting widget help text".to_string()
|
"Failed to get current Editor while getting widget help text".to_string()
|
||||||
}
|
}
|
||||||
@@ -112,26 +112,26 @@ impl<'a> App<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn clear_focus(&mut self) {
|
fn clear_focus(&mut self) {
|
||||||
libclide::info!(target:Self::ID, "Clearing all widget focus");
|
libclide::info!("Clearing all widget focus");
|
||||||
self.explorer.component_state.set_focus(Focus::Inactive);
|
self.explorer.component_state.set_focus(Focus::Inactive);
|
||||||
self.explorer.component_state.set_focus(Focus::Inactive);
|
self.explorer.component_state.set_focus(Focus::Inactive);
|
||||||
self.logger.component_state.set_focus(Focus::Inactive);
|
self.logger.component_state.set_focus(Focus::Inactive);
|
||||||
self.menu_bar.component_state.set_focus(Focus::Inactive);
|
self.menu_bar.component_state.set_focus(Focus::Inactive);
|
||||||
match self.editor_tab.current_editor_mut() {
|
match self.editor_tab.current_editor_mut() {
|
||||||
None => {
|
None => {
|
||||||
libclide::error!(target:Self::ID, "Failed to get current Editor while clearing focus")
|
libclide::error!("Failed to get current Editor while clearing focus")
|
||||||
}
|
}
|
||||||
Some(editor) => editor.component_state.set_focus(Focus::Inactive),
|
Some(editor) => editor.component_state.set_focus(Focus::Inactive),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn change_focus(&mut self, focus: AppComponent) {
|
fn change_focus(&mut self, focus: AppComponent) {
|
||||||
libclide::info!(target:Self::ID, "Changing widget focus to {:?}", focus);
|
libclide::info!("Changing widget focus to {:?}", focus);
|
||||||
self.clear_focus();
|
self.clear_focus();
|
||||||
match focus {
|
match focus {
|
||||||
AppComponent::Editor => match self.editor_tab.current_editor_mut() {
|
AppComponent::Editor => match self.editor_tab.current_editor_mut() {
|
||||||
None => {
|
None => {
|
||||||
libclide::error!(target:Self::ID, "Failed to get current Editor while changing focus")
|
libclide::error!("Failed to get current Editor while changing focus")
|
||||||
}
|
}
|
||||||
Some(editor) => editor.component_state.set_focus(Focus::Active),
|
Some(editor) => editor.component_state.set_focus(Focus::Active),
|
||||||
},
|
},
|
||||||
@@ -274,13 +274,15 @@ impl<'a> Component for App<'a> {
|
|||||||
Action::Quit | Action::Handled => Ok(action),
|
Action::Quit | Action::Handled => Ok(action),
|
||||||
Action::Save => match self.editor_tab.current_editor_mut() {
|
Action::Save => match self.editor_tab.current_editor_mut() {
|
||||||
None => {
|
None => {
|
||||||
libclide::error!(target:Self::ID, "Failed to get current editor while handling App Action::Save");
|
libclide::error!(
|
||||||
|
"Failed to get current editor while handling App Action::Save"
|
||||||
|
);
|
||||||
Ok(Action::Noop)
|
Ok(Action::Noop)
|
||||||
}
|
}
|
||||||
Some(editor) => match editor.save() {
|
Some(editor) => match editor.save() {
|
||||||
Ok(_) => Ok(Action::Handled),
|
Ok(_) => Ok(Action::Handled),
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
libclide::error!(target:Self::ID, "Failed to save editor contents: {e}");
|
libclide::error!("Failed to save editor contents: {e}");
|
||||||
Ok(Action::Noop)
|
Ok(Action::Noop)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -299,14 +301,16 @@ impl<'a> Component for App<'a> {
|
|||||||
Err(_) => Ok(Action::Noop),
|
Err(_) => Ok(Action::Noop),
|
||||||
},
|
},
|
||||||
Action::ReloadFile => {
|
Action::ReloadFile => {
|
||||||
libclide::trace!(target:Self::ID, "Reloading file for current editor");
|
libclide::trace!("Reloading file for current editor");
|
||||||
if let Some(editor) = self.editor_tab.current_editor_mut() {
|
if let Some(editor) = self.editor_tab.current_editor_mut() {
|
||||||
editor
|
editor
|
||||||
.reload_contents()
|
.reload_contents()
|
||||||
.map(|_| Action::Handled)
|
.map(|_| Action::Handled)
|
||||||
.context("Failed to handle Action::ReloadFile")
|
.context("Failed to handle Action::ReloadFile")
|
||||||
} else {
|
} else {
|
||||||
libclide::error!(target:Self::ID, "Failed to get current editor while handling App Action::ReloadFile");
|
libclide::error!(
|
||||||
|
"Failed to get current editor while handling App Action::ReloadFile"
|
||||||
|
);
|
||||||
Ok(Action::Noop)
|
Ok(Action::Noop)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,8 +7,8 @@
|
|||||||
use crate::tui::component::Focus::Inactive;
|
use crate::tui::component::Focus::Inactive;
|
||||||
use Focus::Active;
|
use Focus::Active;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
use libclide::log::Loggable;
|
||||||
use libclide::theme::colors::Colors;
|
use libclide::theme::colors::Colors;
|
||||||
use libclide_macros::log_id;
|
|
||||||
use ratatui::crossterm::event::{Event, KeyEvent, MouseEvent};
|
use ratatui::crossterm::event::{Event, KeyEvent, MouseEvent};
|
||||||
use ratatui::style::Color;
|
use ratatui::style::Color;
|
||||||
|
|
||||||
@@ -62,8 +62,7 @@ pub trait Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Default)]
|
#[derive(Debug, Clone, Default, Loggable)]
|
||||||
#[log_id]
|
|
||||||
pub struct ComponentState {
|
pub struct ComponentState {
|
||||||
pub(crate) focus: Focus,
|
pub(crate) focus: Focus,
|
||||||
pub(crate) vis: Visibility,
|
pub(crate) vis: Visibility,
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use anyhow::{Context, Result, bail};
|
|||||||
use edtui::{
|
use edtui::{
|
||||||
EditorEventHandler, EditorState, EditorTheme, EditorView, LineNumbers, Lines, SyntaxHighlighter,
|
EditorEventHandler, EditorState, EditorTheme, EditorView, LineNumbers, Lines, SyntaxHighlighter,
|
||||||
};
|
};
|
||||||
use libclide_macros::log_id;
|
use libclide::log::Loggable;
|
||||||
use ratatui::buffer::Buffer;
|
use ratatui::buffer::Buffer;
|
||||||
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
||||||
use ratatui::layout::{Alignment, Rect};
|
use ratatui::layout::{Alignment, Rect};
|
||||||
@@ -16,18 +16,18 @@ use ratatui::widgets::{Block, Borders, Padding, Widget};
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use syntect::parsing::SyntaxSet;
|
use syntect::parsing::SyntaxSet;
|
||||||
|
|
||||||
#[log_id]
|
#[derive(Loggable)]
|
||||||
pub struct Editor {
|
pub struct Editor {
|
||||||
pub state: EditorState,
|
pub state: EditorState,
|
||||||
pub event_handler: EditorEventHandler,
|
pub event_handler: EditorEventHandler,
|
||||||
pub file_path: Option<std::path::PathBuf>,
|
pub file_path: Option<PathBuf>,
|
||||||
syntax_set: SyntaxSet,
|
syntax_set: SyntaxSet,
|
||||||
pub(crate) component_state: ComponentState,
|
pub(crate) component_state: ComponentState,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Editor {
|
impl Editor {
|
||||||
pub fn new(path: &std::path::Path) -> Self {
|
pub fn new(path: &std::path::Path) -> Self {
|
||||||
libclide::trace!(target:Self::ID, "Building {}", Self::ID);
|
libclide::trace!("Building {}", <Self as Loggable>::ID);
|
||||||
Editor {
|
Editor {
|
||||||
state: EditorState::default(),
|
state: EditorState::default(),
|
||||||
event_handler: EditorEventHandler::default(),
|
event_handler: EditorEventHandler::default(),
|
||||||
@@ -41,10 +41,10 @@ impl Editor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn reload_contents(&mut self) -> Result<()> {
|
pub fn reload_contents(&mut self) -> Result<()> {
|
||||||
libclide::trace!(target:Self::ID, "Reloading editor file contents {:?}", self.file_path);
|
libclide::trace!("Reloading editor file contents {:?}", self.file_path);
|
||||||
match self.file_path.clone() {
|
match self.file_path.clone() {
|
||||||
None => {
|
None => {
|
||||||
libclide::error!(target:Self::ID, "Failed to reload editor contents with None file_path");
|
libclide::error!("Failed to reload editor contents with None file_path");
|
||||||
bail!("Failed to reload editor contents with None file_path")
|
bail!("Failed to reload editor contents with None file_path")
|
||||||
}
|
}
|
||||||
Some(path) => self.set_contents(&path),
|
Some(path) => self.set_contents(&path),
|
||||||
@@ -52,7 +52,7 @@ impl Editor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_contents(&mut self, path: &std::path::Path) -> Result<()> {
|
pub fn set_contents(&mut self, path: &std::path::Path) -> Result<()> {
|
||||||
libclide::trace!(target:Self::ID, "Setting Editor contents from path {:?}", path);
|
libclide::trace!("Setting Editor contents from path {:?}", path);
|
||||||
if let Ok(contents) = std::fs::read_to_string(path) {
|
if let Ok(contents) = std::fs::read_to_string(path) {
|
||||||
let lines: Vec<_> = contents
|
let lines: Vec<_> = contents
|
||||||
.lines()
|
.lines()
|
||||||
@@ -68,10 +68,10 @@ impl Editor {
|
|||||||
|
|
||||||
pub fn save(&self) -> Result<()> {
|
pub fn save(&self) -> Result<()> {
|
||||||
if let Some(path) = &self.file_path {
|
if let Some(path) = &self.file_path {
|
||||||
libclide::trace!(target:Self::ID, "Saving Editor contents {:?}", path);
|
libclide::trace!("Saving Editor contents {:?}", path);
|
||||||
return std::fs::write(path, self.state.lines.to_string()).map_err(|e| e.into());
|
return std::fs::write(path, self.state.lines.to_string()).map_err(|e| e.into());
|
||||||
};
|
};
|
||||||
libclide::error!(target:Self::ID, "Failed saving Editor contents; file_path was None");
|
libclide::error!("Failed saving Editor contents; file_path was None");
|
||||||
bail!("File not saved. No file path set.")
|
bail!("File not saved. No file path set.")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
use crate::tui::component::{Action, Component, Focus, FocusState};
|
use crate::tui::component::{Action, Component, Focus, FocusState};
|
||||||
use crate::tui::editor::Editor;
|
use crate::tui::editor::Editor;
|
||||||
use anyhow::{Context, Result, anyhow};
|
use anyhow::{Context, Result, anyhow};
|
||||||
use libclide_macros::log_id;
|
use libclide::log::Loggable;
|
||||||
use ratatui::buffer::Buffer;
|
use ratatui::buffer::Buffer;
|
||||||
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};
|
||||||
use ratatui::layout::Rect;
|
use ratatui::layout::Rect;
|
||||||
@@ -16,7 +16,7 @@ use std::collections::HashMap;
|
|||||||
// Render the tabs with keys as titles
|
// Render the tabs with keys as titles
|
||||||
// Tab keys can be file names.
|
// Tab keys can be file names.
|
||||||
// Render the editor using the key as a reference for lookup
|
// Render the editor using the key as a reference for lookup
|
||||||
#[log_id]
|
#[derive(Loggable)]
|
||||||
pub struct EditorTab {
|
pub struct EditorTab {
|
||||||
pub(crate) editors: HashMap<String, Editor>,
|
pub(crate) editors: HashMap<String, Editor>,
|
||||||
tab_order: Vec<String>,
|
tab_order: Vec<String>,
|
||||||
@@ -25,7 +25,7 @@ pub struct EditorTab {
|
|||||||
|
|
||||||
impl EditorTab {
|
impl EditorTab {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
libclide::trace!(target:Self::ID, "Building {}", Self::ID);
|
libclide::trace!("Building {}", <Self as Loggable>::ID);
|
||||||
Self {
|
Self {
|
||||||
editors: HashMap::new(),
|
editors: HashMap::new(),
|
||||||
tab_order: Vec::new(),
|
tab_order: Vec::new(),
|
||||||
@@ -35,7 +35,11 @@ impl EditorTab {
|
|||||||
|
|
||||||
pub fn next_editor(&mut self) {
|
pub fn next_editor(&mut self) {
|
||||||
let next = (self.current_editor + 1) % self.tab_order.len();
|
let next = (self.current_editor + 1) % self.tab_order.len();
|
||||||
libclide::trace!(target:Self::ID, "Moving from {} to next editor tab at {}", self.current_editor, next);
|
libclide::trace!(
|
||||||
|
"Moving from {} to next editor tab at {}",
|
||||||
|
self.current_editor,
|
||||||
|
next
|
||||||
|
);
|
||||||
self.set_tab_focus(Focus::Active, next);
|
self.set_tab_focus(Focus::Active, next);
|
||||||
self.current_editor = next;
|
self.current_editor = next;
|
||||||
}
|
}
|
||||||
@@ -45,7 +49,11 @@ impl EditorTab {
|
|||||||
.current_editor
|
.current_editor
|
||||||
.checked_sub(1)
|
.checked_sub(1)
|
||||||
.unwrap_or(self.tab_order.len() - 1);
|
.unwrap_or(self.tab_order.len() - 1);
|
||||||
libclide::trace!(target:Self::ID, "Moving from {} to previous editor tab at {}", self.current_editor, prev);
|
libclide::trace!(
|
||||||
|
"Moving from {} to previous editor tab at {}",
|
||||||
|
self.current_editor,
|
||||||
|
prev
|
||||||
|
);
|
||||||
self.set_tab_focus(Focus::Active, prev);
|
self.set_tab_focus(Focus::Active, prev);
|
||||||
self.current_editor = prev;
|
self.current_editor = prev;
|
||||||
}
|
}
|
||||||
@@ -54,7 +62,7 @@ impl EditorTab {
|
|||||||
match self.tab_order.get(index) {
|
match self.tab_order.get(index) {
|
||||||
None => {
|
None => {
|
||||||
if !self.tab_order.is_empty() {
|
if !self.tab_order.is_empty() {
|
||||||
libclide::error!(target:Self::ID, "Failed to get editor tab key with invalid index {index}");
|
libclide::error!("Failed to get editor tab key with invalid index {index}");
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
@@ -72,16 +80,19 @@ impl EditorTab {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_current_tab_focus(&mut self, focus: Focus) {
|
pub fn set_current_tab_focus(&mut self, focus: Focus) {
|
||||||
libclide::trace!(target:Self::ID, "Setting current tab {} focus to {:?}", self.current_editor, focus);
|
libclide::trace!(
|
||||||
|
"Setting current tab {} focus to {:?}",
|
||||||
|
self.current_editor,
|
||||||
|
focus
|
||||||
|
);
|
||||||
self.set_tab_focus(focus, self.current_editor)
|
self.set_tab_focus(focus, self.current_editor)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_tab_focus(&mut self, focus: Focus, index: usize) {
|
pub fn set_tab_focus(&mut self, focus: Focus, index: usize) {
|
||||||
libclide::trace!(target:Self::ID, "Setting tab {} focus to {:?}", index, focus);
|
libclide::trace!("Setting tab {} focus to {:?}", index, focus);
|
||||||
if focus == Focus::Active && index != self.current_editor {
|
if focus == Focus::Active && index != self.current_editor {
|
||||||
// If we are setting another tab to active, disable the current one.
|
// If we are setting another tab to active, disable the current one.
|
||||||
libclide::trace!(
|
libclide::trace!(
|
||||||
target:Self::ID,
|
|
||||||
"New tab {} focus set to Active; Setting current tab {} to Inactive",
|
"New tab {} focus set to Active; Setting current tab {} to Inactive",
|
||||||
index,
|
index,
|
||||||
self.current_editor
|
self.current_editor
|
||||||
@@ -90,12 +101,11 @@ impl EditorTab {
|
|||||||
}
|
}
|
||||||
match self.get_editor_key(index) {
|
match self.get_editor_key(index) {
|
||||||
None => {
|
None => {
|
||||||
libclide::error!(target:Self::ID, "Failed setting tab focus for invalid key {index}");
|
libclide::error!("Failed setting tab focus for invalid key {index}");
|
||||||
}
|
}
|
||||||
Some(key) => match self.editors.get_mut(&key) {
|
Some(key) => match self.editors.get_mut(&key) {
|
||||||
None => {
|
None => {
|
||||||
libclide::error!(
|
libclide::error!(
|
||||||
target:Self::ID,
|
|
||||||
"Failed to update tab focus at index {} with invalid key: {}",
|
"Failed to update tab focus at index {} with invalid key: {}",
|
||||||
self.current_editor,
|
self.current_editor,
|
||||||
self.tab_order[self.current_editor]
|
self.tab_order[self.current_editor]
|
||||||
@@ -107,12 +117,12 @@ impl EditorTab {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn open_tab(&mut self, path: &std::path::Path) -> Result<()> {
|
pub fn open_tab(&mut self, path: &std::path::Path) -> Result<()> {
|
||||||
libclide::trace!(target:Self::ID, "Opening new EditorTab with path {:?}", path);
|
libclide::trace!("Opening new EditorTab with path {:?}", path);
|
||||||
if self
|
if self
|
||||||
.editors
|
.editors
|
||||||
.contains_key(&path.to_string_lossy().to_string())
|
.contains_key(&path.to_string_lossy().to_string())
|
||||||
{
|
{
|
||||||
libclide::warn!(target:Self::ID, "EditorTab already opened with this file");
|
libclide::warn!("EditorTab already opened with this file");
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -137,12 +147,12 @@ impl EditorTab {
|
|||||||
.to_owned();
|
.to_owned();
|
||||||
match self.editors.remove(&key) {
|
match self.editors.remove(&key) {
|
||||||
None => {
|
None => {
|
||||||
libclide::error!(target:Self::ID, "Failed to remove editor tab {key} with invalid index {index}")
|
libclide::error!("Failed to remove editor tab {key} with invalid index {index}")
|
||||||
}
|
}
|
||||||
Some(_) => {
|
Some(_) => {
|
||||||
self.prev_editor();
|
self.prev_editor();
|
||||||
self.tab_order.remove(index);
|
self.tab_order.remove(index);
|
||||||
libclide::info!(target:Self::ID, "Closed editor tab {key} at index {index}")
|
libclide::info!("Closed editor tab {key} at index {index}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -5,7 +5,7 @@
|
|||||||
use crate::tui::component::{Action, Component, ComponentState, Focus, FocusState};
|
use crate::tui::component::{Action, Component, ComponentState, Focus, FocusState};
|
||||||
use anyhow::{Context, Result, bail};
|
use anyhow::{Context, Result, bail};
|
||||||
use libclide::fs::entry_meta::EntryMeta;
|
use libclide::fs::entry_meta::EntryMeta;
|
||||||
use libclide_macros::log_id;
|
use libclide::log::Loggable;
|
||||||
use ratatui::buffer::Buffer;
|
use ratatui::buffer::Buffer;
|
||||||
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, MouseEvent, MouseEventKind};
|
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent, MouseEvent, MouseEventKind};
|
||||||
use ratatui::layout::{Alignment, Position, Rect};
|
use ratatui::layout::{Alignment, Position, Rect};
|
||||||
@@ -16,8 +16,7 @@ use std::fs;
|
|||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use tui_tree_widget::{Tree, TreeItem, TreeState};
|
use tui_tree_widget::{Tree, TreeItem, TreeState};
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Loggable)]
|
||||||
#[log_id]
|
|
||||||
pub struct Explorer<'a> {
|
pub struct Explorer<'a> {
|
||||||
root_path: EntryMeta,
|
root_path: EntryMeta,
|
||||||
tree_items: TreeItem<'a, String>,
|
tree_items: TreeItem<'a, String>,
|
||||||
@@ -27,7 +26,7 @@ pub struct Explorer<'a> {
|
|||||||
|
|
||||||
impl<'a> Explorer<'a> {
|
impl<'a> Explorer<'a> {
|
||||||
pub fn new(path: &PathBuf) -> Result<Self> {
|
pub fn new(path: &PathBuf) -> Result<Self> {
|
||||||
libclide::trace!("Building {}", Self::ID);
|
libclide::trace!("Building {}", <Self as Loggable>::ID);
|
||||||
let explorer = Explorer {
|
let explorer = Explorer {
|
||||||
root_path: EntryMeta::new(path)?,
|
root_path: EntryMeta::new(path)?,
|
||||||
tree_items: Self::build_tree_from_path(path)?,
|
tree_items: Self::build_tree_from_path(path)?,
|
||||||
@@ -69,7 +68,7 @@ impl<'a> Explorer<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: The first argument is a unique identifier, where no 2 TreeItems may share the same.
|
// Note: The first argument is a unique identifier, where no. 2 TreeItems may share the same.
|
||||||
// For a file tree this is fine because we shouldn't list the same object twice.
|
// For a file tree this is fine because we shouldn't list the same object twice.
|
||||||
TreeItem::new(
|
TreeItem::new(
|
||||||
path_meta.abs_path.clone(),
|
path_meta.abs_path.clone(),
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
// SPDX-License-Identifier: GNU General Public License v3.0 or later
|
// SPDX-License-Identifier: GNU General Public License v3.0 or later
|
||||||
|
|
||||||
use crate::tui::component::{Action, Component, ComponentState, Focus, FocusState};
|
use crate::tui::component::{Action, Component, ComponentState, Focus, FocusState};
|
||||||
use libclide_macros::log_id;
|
use libclide::log::Loggable;
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use ratatui::buffer::Buffer;
|
use ratatui::buffer::Buffer;
|
||||||
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent};
|
use ratatui::crossterm::event::{Event, KeyCode, KeyEvent};
|
||||||
@@ -14,7 +14,7 @@ use tui_logger::{TuiLoggerLevelOutput, TuiLoggerSmartWidget, TuiWidgetEvent, Tui
|
|||||||
|
|
||||||
/// Any log written as info!(target:self.id(), "message") will work with this logger.
|
/// Any log written as info!(target:self.id(), "message") will work with this logger.
|
||||||
/// The logger is bound to info!, debug!, error!, trace! macros within Tui::new().
|
/// The logger is bound to info!, debug!, error!, trace! macros within Tui::new().
|
||||||
#[log_id]
|
#[derive(Loggable)]
|
||||||
pub struct Logger {
|
pub struct Logger {
|
||||||
state: TuiWidgetState,
|
state: TuiWidgetState,
|
||||||
pub(crate) component_state: ComponentState,
|
pub(crate) component_state: ComponentState,
|
||||||
@@ -22,7 +22,7 @@ pub struct Logger {
|
|||||||
|
|
||||||
impl Logger {
|
impl Logger {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
libclide::trace!(target:Self::ID, "Building {}", Self::ID);
|
libclide::trace!("Building {}", <Self as Loggable>::ID);
|
||||||
let state = TuiWidgetState::new();
|
let state = TuiWidgetState::new();
|
||||||
state.transition(TuiWidgetEvent::HideKey);
|
state.transition(TuiWidgetEvent::HideKey);
|
||||||
Self {
|
Self {
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use crate::tui::menu_bar::MenuBarItemOption::{
|
|||||||
About, CloseTab, Exit, Reload, Save, ShowHideExplorer, ShowHideLogger,
|
About, CloseTab, Exit, Reload, Save, ShowHideExplorer, ShowHideLogger,
|
||||||
};
|
};
|
||||||
use anyhow::Context;
|
use anyhow::Context;
|
||||||
use libclide_macros::log_id;
|
use libclide::log::Loggable;
|
||||||
use ratatui::buffer::Buffer;
|
use ratatui::buffer::Buffer;
|
||||||
use ratatui::crossterm::event::{KeyCode, KeyEvent};
|
use ratatui::crossterm::event::{KeyCode, KeyEvent};
|
||||||
use ratatui::layout::Rect;
|
use ratatui::layout::Rect;
|
||||||
@@ -21,6 +21,7 @@ use strum::{EnumIter, FromRepr, IntoEnumIterator};
|
|||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromRepr, EnumIter)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, FromRepr, EnumIter)]
|
||||||
enum MenuBarItem {
|
enum MenuBarItem {
|
||||||
File,
|
File,
|
||||||
|
Edit,
|
||||||
View,
|
View,
|
||||||
Help,
|
Help,
|
||||||
}
|
}
|
||||||
@@ -68,19 +69,21 @@ impl MenuBarItem {
|
|||||||
MenuBarItem::File => "File",
|
MenuBarItem::File => "File",
|
||||||
MenuBarItem::View => "View",
|
MenuBarItem::View => "View",
|
||||||
MenuBarItem::Help => "Help",
|
MenuBarItem::Help => "Help",
|
||||||
|
MenuBarItem::Edit => "Edit",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn options(&self) -> &[MenuBarItemOption] {
|
pub fn options(&self) -> &[MenuBarItemOption] {
|
||||||
match self {
|
match self {
|
||||||
MenuBarItem::File => &[Save, CloseTab, Reload, Exit],
|
MenuBarItem::File => &[Save, Reload, Exit],
|
||||||
|
MenuBarItem::Edit => &[CloseTab],
|
||||||
MenuBarItem::View => &[ShowHideExplorer, ShowHideLogger],
|
MenuBarItem::View => &[ShowHideExplorer, ShowHideLogger],
|
||||||
MenuBarItem::Help => &[About],
|
MenuBarItem::Help => &[About],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[log_id]
|
#[derive(Debug, Loggable)]
|
||||||
pub struct MenuBar {
|
pub struct MenuBar {
|
||||||
selected: MenuBarItem,
|
selected: MenuBarItem,
|
||||||
opened: Option<MenuBarItem>,
|
opened: Option<MenuBarItem>,
|
||||||
@@ -91,7 +94,7 @@ pub struct MenuBar {
|
|||||||
impl MenuBar {
|
impl MenuBar {
|
||||||
const DEFAULT_HELP: &str = "(←/h)/(→/l): Select option | Enter: Choose selection";
|
const DEFAULT_HELP: &str = "(←/h)/(→/l): Select option | Enter: Choose selection";
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
libclide::trace!("Building {}", Self::ID);
|
libclide::trace!("Building");
|
||||||
Self {
|
Self {
|
||||||
selected: MenuBarItem::File,
|
selected: MenuBarItem::File,
|
||||||
opened: None,
|
opened: None,
|
||||||
|
|||||||
Reference in New Issue
Block a user