Skip to content

Commit

Permalink
Added highlighting settings for YAML and Bash (#297)
Browse files Browse the repository at this point in the history
* Added settings for YAML highlighting

* Added styling settings for Bash, added red and yellow colors for warning/error/critical messages in the log window
  • Loading branch information
mikekazakov committed Jun 8, 2024
1 parent 0bd3f00 commit 31a69cf
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"lexer": "bash",
"wordlists": [
"if then elif else fi time for in until while do done case esac coproc select function"
],
"properties": {
"lexer.bash.styling.inside.string": "1",
"lexer.bash.styling.inside.backticks": "1",
"lexer.bash.styling.inside.parameter": "1",
"lexer.bash.styling.inside.heredoc": "1",
"lexer.bash.command.substitution": "1"
},
"mapping": {
"SCE_SH_DEFAULT": "default",
"SCE_SH_ERROR": "preprocessor",
"SCE_SH_COMMENTLINE": "comment",
"SCE_SH_NUMBER": "number",
"SCE_SH_WORD": "keyword",
"SCE_SH_STRING": "string",
"SCE_SH_CHARACTER": "string",
"SCE_SH_OPERATOR": "operator",
"SCE_SH_IDENTIFIER": "identifier",
"SCE_SH_SCALAR": "keyword",
"SCE_SH_PARAM": "keyword",
"SCE_SH_BACKTICKS": "operator",
"SCE_SH_HERE_DELIM": "operator",
"SCE_SH_HERE_Q": "operator"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
{
"langs": [
"langs": [
{
"name": "Bash",
"settings": "Bash.json",
"filemask": "*.sh"
},
{
"name": "C++",
"settings": "C++.json",
Expand All @@ -9,6 +14,11 @@
"name": "JSON",
"settings": "JSON.json",
"filemask": "*.json"
},
{
"name": "YAML",
"settings": "YAML.json",
"filemask": "*.yaml, *.yml"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"lexer": "yaml",
"wordlists": [
"true false yes no"
],
"properties": {
},
"mapping": {
"SCE_YAML_DEFAULT": "default",
"SCE_YAML_COMMENT": "comment",
"SCE_YAML_IDENTIFIER": "keyword",
"SCE_YAML_KEYWORD": "keyword",
"SCE_YAML_NUMBER": "number",
"SCE_YAML_REFERENCE": "identifier",
"SCE_YAML_DOCUMENT": "keyword",
"SCE_YAML_TEXT": "string",
"SCE_YAML_ERROR": "preprocessor",
"SCE_YAML_OPERATOR": "operator"
}
}
45 changes: 42 additions & 3 deletions Source/Utility/source/SpdLogWindow.mm
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (C) 2022-2023 Michael Kazakov. Subject to GNU General Public License version 3.
// Copyright (C) 2022-2024 Michael Kazakov. Subject to GNU General Public License version 3.
#include "SpdLogWindow.h"
#include <spdlog/spdlog.h>
#include <spdlog/sinks/base_sink.h>
Expand Down Expand Up @@ -98,6 +98,9 @@ @implementation NCSpdLogWindowController {
std::shared_ptr<nc::utility::SpdLogUISink> m_Sink;
NSTimer *m_DrainTimer;
NSDictionary<NSAttributedStringKey, id> *m_TextAttrs;
NSDictionary<NSAttributedStringKey, id> *m_WarningAttrs;
NSDictionary<NSAttributedStringKey, id> *m_ErrorAttrs;
NSDictionary<NSAttributedStringKey, id> *m_CriticalAttrs;
bool m_AutoScroll;
}

Expand Down Expand Up @@ -218,8 +221,21 @@ - (void)createControls

m_TextAttrs = @{
NSFontAttributeName: [NSFont monospacedSystemFontOfSize:10. weight:NSFontWeightRegular],
NSForegroundColorAttributeName: [NSColor textColor]
NSForegroundColorAttributeName: NSColor.textColor
};
m_WarningAttrs = @{
NSFontAttributeName: [NSFont monospacedSystemFontOfSize:10. weight:NSFontWeightSemibold],
NSForegroundColorAttributeName: NSColor.systemYellowColor
};
m_ErrorAttrs = @{
NSFontAttributeName: [NSFont monospacedSystemFontOfSize:10. weight:NSFontWeightSemibold],
NSForegroundColorAttributeName: NSColor.systemRedColor
};
m_CriticalAttrs = @{
NSFontAttributeName: [NSFont monospacedSystemFontOfSize:10. weight:NSFontWeightBold],
NSForegroundColorAttributeName: NSColor.systemRedColor
};

m_ScrollView = sv;
m_TextView = tv;
m_TextStorage = tv.textStorage;
Expand All @@ -244,11 +260,34 @@ + (NSWindow *)makeWindow
return wnd;
}

- (void)highlightString:(NSMutableAttributedString *)_str
withAttributes:(NSDictionary<NSAttributedStringKey, id> *)_attrs
forSubstring:(NSString *)_sub_str
{
NSString *str = _str.string;
const size_t length = str.length;

NSRange search_range = NSMakeRange(0, str.length);
NSRange found;
while( (found = [str rangeOfString:_sub_str options:0 range:search_range]).location != NSNotFound ) {
[_str setAttributes:_attrs range:found];
search_range = NSMakeRange(NSMaxRange(found), length - NSMaxRange(found));
}
}

- (void)highlightString:(NSMutableAttributedString *)_str
{
[self highlightString:_str withAttributes:m_WarningAttrs forSubstring:@" [warning] "];
[self highlightString:_str withAttributes:m_ErrorAttrs forSubstring:@" [error] "];
[self highlightString:_str withAttributes:m_CriticalAttrs forSubstring:@" [critical] "];
}

- (void)acceptNewString:(NSString *)_str
{
dispatch_assert_main_queue();
assert(_str != nil);
if( auto as = [[NSAttributedString alloc] initWithString:_str attributes:m_TextAttrs] ) {
if( auto as = [[NSMutableAttributedString alloc] initWithString:_str attributes:m_TextAttrs] ) {
[self highlightString:as];
[m_TextStorage appendAttributedString:as];
if( m_AutoScroll )
[m_TextView scrollToEndOfDocument:nil];
Expand Down

0 comments on commit 31a69cf

Please sign in to comment.