Skip to content

Commit

Permalink
Merge pull request #116 from Etto48/feature/better_app_settings
Browse files Browse the repository at this point in the history
Better app settings
  • Loading branch information
Etto48 authored Sep 5, 2024
2 parents 49ce31a + f7db252 commit fa5ea25
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 40 deletions.
3 changes: 2 additions & 1 deletion docs/SETTINGS.md
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,8 @@ The following app settings can be customized in the app settings:
|------|------|-------------|
|history_limit|usize|Maximum number of modifications that are stored in the undo/redo history.|
|log_limit|usize|Maximum number of log messages that are stored in the log.|
|theme|Option<String>|The name of the theme to use. The available themes are: `"auto"`, `"dark"`, `"light"`. `"auto"` chooses automatically between `"dark"` and `"light"` based on the background color of the terminal. By default, the theme is `"auto"`.|
|log_level|Verbosity|The minimum level of log messages that are shown. Can be `"info"`, `"debug"`, `"warning"` or `"error"`.|
|theme|ThemePreference|The theme to use, can be `"auto"`, `"light"` or `"dark"`.|

## Custom

Expand Down
1 change: 1 addition & 0 deletions src/app/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ impl App {
}
};
logger.change_limit(settings.app.log_limit);
logger.change_verbosity(settings.app.log_level);
Self::print_loading_status(
&settings.color,
&format!("Opening \"{}\"...", args.path),
Expand Down
36 changes: 25 additions & 11 deletions src/app/log/logger.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,35 @@
use std::{collections::VecDeque, ops::Index};

use crate::app::App;
use crate::app::{settings::verbosity::Verbosity, App};

use super::{log_line::LogLine, notification::NotificationLevel};

#[derive(Debug, Clone, Default)]
pub struct Logger {
pub(super) limit: usize,
pub(super) log: VecDeque<LogLine>,
pub(super) verbosity: Verbosity,
pub(super) notification: NotificationLevel,
}

impl Logger {
pub fn with_limit(limit: usize) -> Self {
pub fn new(limit: usize, verbosity: Verbosity) -> Self {
Self {
limit,
log: VecDeque::with_capacity(limit),
verbosity,
notification: NotificationLevel::None,
}
}

pub fn log(&mut self, level: NotificationLevel, message: &str) {
self.notification.bump_notification_level(level);
if self.log.len() >= self.limit && self.limit > 0 {
self.log.pop_front();
if level >= self.verbosity.as_notification_level() {
self.notification.bump_notification_level(level);
if self.log.len() >= self.limit && self.limit > 0 {
self.log.pop_front();
}
self.log.push_back(LogLine::new(level, message.to_string()));
}
self.log.push_back(LogLine::new(level, message.to_string()));
}

pub fn clear(&mut self) {
Expand Down Expand Up @@ -65,15 +69,25 @@ impl Logger {
}
}

pub fn change_verbosity(&mut self, verbosity: Verbosity) {
self.verbosity = verbosity;
for log_line in &self.log {
if log_line.level < verbosity.as_notification_level() {
self.notification.bump_notification_level(log_line.level);
}
}
if self.notification < verbosity.as_notification_level() {
self.notification = NotificationLevel::None;
}
}

pub fn merge(&mut self, other: &Self) {
for log_line in &other.log {
if self.log.len() >= self.limit && self.limit > 0 {
self.log.pop_front();
}
self.log.push_back(log_line.clone());
self.log(log_line.level, &log_line.message);
}
self.notification
.bump_notification_level(other.notification);
}
}

Expand Down Expand Up @@ -127,7 +141,7 @@ mod test {

#[test]
fn test_logger_with_limit() {
let mut logger = Logger::with_limit(5);
let mut logger = Logger::new(5, Verbosity::Debug);
for i in 0..10 {
logger.log(
NotificationLevel::Error,
Expand All @@ -141,7 +155,7 @@ mod test {

#[test]
fn test_logger_change_limit() {
let mut logger = Logger::with_limit(2);
let mut logger = Logger::new(2, Verbosity::Debug);
logger.log(NotificationLevel::Error, "Test error message 1");
logger.log(NotificationLevel::Error, "Test error message 2");
logger.log(NotificationLevel::Error, "Test error message 3");
Expand Down
20 changes: 7 additions & 13 deletions src/app/log/notification.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::fmt::{Display, Formatter};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, PartialOrd, Ord)]
pub enum NotificationLevel {
#[default]
None,
Debug,
Info,
Warning,
Error,
None = 0,
Debug = 1,
Info = 2,
Warning = 3,
Error = 4,
}

impl NotificationLevel {
Expand All @@ -22,13 +22,7 @@ impl NotificationLevel {
}

pub fn notification_level_as_u8(&self) -> u8 {
match self {
NotificationLevel::None => 0,
NotificationLevel::Debug => 1,
NotificationLevel::Info => 2,
NotificationLevel::Warning => 3,
NotificationLevel::Error => 4,
}
*self as u8
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/app/mockup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ pub mod test {

use ratatui::{backend::TestBackend, Terminal};

use crate::app::App;
use crate::app::{settings::verbosity::Verbosity, App};

impl App {
/// Create a mockup of the app with the given data.
/// Sets the logger verbosity to debug.
pub fn mockup(data: Vec<u8>) -> Self {
let mut app = App::default();
let mut input_file =
Expand All @@ -17,6 +19,7 @@ pub mod test {
let mut terminal = Terminal::new(TestBackend::new(80, 25)).unwrap();
app.open_file(&input_file.path().to_string_lossy(), &mut terminal)
.expect("Failed to open file for mockup.");
app.logger.change_verbosity(Verbosity::Debug);
app
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/app/settings/app_settings.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use mlua::UserDataRegistry;
use serde::{Deserialize, Serialize};

use super::Settings;
use super::{theme_preference::ThemePreference, verbosity::Verbosity, Settings};

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct AppSettings {
pub history_limit: usize,
pub log_limit: usize,
pub theme: Option<String>,
pub log_level: Verbosity,
pub theme: ThemePreference,
}

impl AppSettings {
Expand Down Expand Up @@ -43,7 +44,8 @@ impl Default for AppSettings {
Self {
history_limit: 1024,
log_limit: 1024,
theme: None,
log_level: Verbosity::default(),
theme: ThemePreference::default(),
}
}
}
14 changes: 4 additions & 10 deletions src/app/settings/color_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::app::App;
use crate::{EditColorSettings, RegisterColorSettings};

use super::app_settings::AppSettings;
use super::theme_preference::ThemePreference;

#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[derive(EditColorSettings!)]
Expand Down Expand Up @@ -248,16 +249,9 @@ impl ColorSettings {
terminal_theme: Theme,
) -> Result<Self, String> {
let theme = match &app_settings.theme {
Some(theme) if theme.to_lowercase() == "light" => Theme::Light,
Some(theme) if theme.to_lowercase() == "dark" => Theme::Dark,
any => {
if let Some(theme) = any {
if theme.to_lowercase() != "auto" {
return Err(format!("Invalid theme: {}", theme));
}
}
terminal_theme
}
ThemePreference::Light => Theme::Light,
ThemePreference::Dark => Theme::Dark,
ThemePreference::Auto => terminal_theme,
};
let mut color_settings = Self::get_default_theme(theme);
color_settings
Expand Down
2 changes: 2 additions & 0 deletions src/app/settings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ pub mod register_color_settings_macro;
#[macro_use]
pub mod edit_color_settings;
pub mod settings_value;
pub mod theme_preference;
pub mod verbosity;
10 changes: 10 additions & 0 deletions src/app/settings/theme_preference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum ThemePreference {
Light,
Dark,
#[default]
Auto,
}
24 changes: 24 additions & 0 deletions src/app/settings/verbosity.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use serde::{Deserialize, Serialize};

use crate::app::log::NotificationLevel;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
pub enum Verbosity {
Debug,
#[default]
Info,
Warning,
Error,
}

impl Verbosity {
pub fn as_notification_level(&self) -> NotificationLevel {
match self {
Verbosity::Debug => NotificationLevel::Debug,
Verbosity::Info => NotificationLevel::Info,
Verbosity::Warning => NotificationLevel::Warning,
Verbosity::Error => NotificationLevel::Error,
}
}
}
4 changes: 3 additions & 1 deletion test/default_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,9 @@
},
"app": {
"history_limit": 1024,
"log_limit": 1024
"log_limit": 1024,
"log_level": "info",
"theme": "auto"
},
"custom": {}
}

0 comments on commit fa5ea25

Please sign in to comment.