Skip to content

Commit

Permalink
Merge pull request #39 from pamburus/feature/quotes
Browse files Browse the repository at this point in the history
new: configurable formatting added
  • Loading branch information
pamburus authored May 28, 2023
2 parents 14aab6a + 6dcb91f commit 7092d15
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 14 deletions.
1 change: 0 additions & 1 deletion .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
enum_discrim_align_threshold = 8
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ categories = ["command-line-utilities"]
description = "Utility for viewing json-formatted log files."
keywords = ["cli", "human", "log"]
name = "hl"
version = "0.14.0"
version = "0.15.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
12 changes: 12 additions & 0 deletions etc/defaults/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,18 @@ fields:
# List of exact field names to hide.
hide: []

# Formatting settings.
formatting:
punctuation:
logger-name-separator: ':'
field-key-value-separator: ':'
string-opening-quote: "'"
string-closing-quote: "'"
source-location-separator: '@ '
hidden-fields-indicator: ' ...'
level-left-separator: '|'
level-right-separator: '|'

# Number of processing threads, configured automatically based on CPU count if not specified.
concurrency: ~

Expand Down
2 changes: 1 addition & 1 deletion etc/defaults/themes/one-dark-green.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ elements:
message:
foreground: bright-white
field:
foreground: bright-black
foreground: default
key:
foreground: green
modes: [underline]
Expand Down
6 changes: 4 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use std::num::NonZeroUsize;

use crate::datefmt::{DateTimeFormat, DateTimeFormatter};
use crate::error::*;
use crate::formatting::RecordFormatter;
use crate::formatting::{RecordFormatter};
use crate::model::{Filter, Parser, ParserSettings, RawRecord};
use crate::scanning::{BufFactory, Scanner, Segment, SegmentBufFactory};
use crate::settings::Fields;
use crate::settings::{Fields, Formatting};
use crate::theme::Theme;
use crate::timezone::Tz;
use crate::IncludeExcludeKeyFilter;
Expand All @@ -29,6 +29,7 @@ pub struct Options {
pub concurrency: usize,
pub filter: Filter,
pub fields: FieldOptions,
pub formatting: Formatting,
pub time_zone: Tz,
pub hide_empty_fields: bool,
}
Expand Down Expand Up @@ -90,6 +91,7 @@ impl App {
),
self.options.hide_empty_fields,
self.options.fields.filter.clone(),
self.options.formatting.clone(),
)
.with_field_unescaping(!self.options.raw_fields);
let mut processor = SegmentProcesor::new(&parser, &mut formatter, &self.options.filter);
Expand Down
35 changes: 27 additions & 8 deletions src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::datefmt;
use crate::filtering::IncludeExcludeSetting;
use crate::fmtx;
use crate::model;
use crate::settings::Formatting;
use crate::theme;
use crate::IncludeExcludeKeyFilter;

Expand All @@ -31,6 +32,7 @@ pub struct RecordFormatter {
ts_width: usize,
hide_empty_fields: bool,
fields: Arc<IncludeExcludeKeyFilter>,
cfg: Formatting,
}

impl RecordFormatter {
Expand All @@ -39,6 +41,7 @@ impl RecordFormatter {
ts_formatter: DateTimeFormatter,
hide_empty_fields: bool,
fields: Arc<IncludeExcludeKeyFilter>,
cfg: Formatting,
) -> Self {
let ts_width = ts_formatter.max_length();
RecordFormatter {
Expand All @@ -48,6 +51,7 @@ impl RecordFormatter {
ts_width,
hide_empty_fields,
fields,
cfg,
}
}

Expand Down Expand Up @@ -90,7 +94,7 @@ impl RecordFormatter {
s.space();
s.element(Element::Level, |s| {
s.batch(|buf| {
buf.push(b'|');
buf.extend_from_slice(self.cfg.punctuation.level_left_separator.as_bytes());
});
s.element(Element::LevelInner, |s| {
s.batch(|buf| {
Expand All @@ -103,7 +107,9 @@ impl RecordFormatter {
})
})
});
s.batch(|buf| buf.push(b'|'));
s.batch(|buf| {
buf.extend_from_slice(self.cfg.punctuation.level_right_separator.as_bytes())
});
});
//
// logger
Expand All @@ -114,7 +120,9 @@ impl RecordFormatter {
s.element(Element::LoggerInner, |s| {
s.batch(|buf| buf.extend_from_slice(logger.as_bytes()))
});
s.batch(|buf| buf.push(b':'));
s.batch(|buf| {
buf.extend_from_slice(self.cfg.punctuation.logger_name_separator.as_bytes())
});
});
}
//
Expand All @@ -140,15 +148,24 @@ impl RecordFormatter {
}
if some_fields_hidden {
s.element(Element::Ellipsis, |s| {
s.batch(|buf| buf.extend_from_slice(b" ..."))
s.batch(|buf| {
buf.extend_from_slice(
self.cfg.punctuation.hidden_fields_indicator.as_bytes(),
)
})
});
}
//
// caller
//
if let Some(text) = rec.caller {
s.element(Element::Caller, |s| {
s.batch(|buf| buf.extend_from_slice(b" @ "));
s.batch(|buf| {
buf.push(b' ');
buf.extend_from_slice(
self.cfg.punctuation.source_location_separator.as_bytes(),
)
});
s.element(Element::CallerInner, |s| {
s.batch(|buf| buf.extend_from_slice(text.as_bytes()))
});
Expand Down Expand Up @@ -313,7 +330,9 @@ impl<'a> FieldFormatter<'a> {
s.batch(|buf| buf.push(b.to_ascii_lowercase()));
}
});
s.batch(|buf| buf.push(b'='));
s.batch(|buf| {
buf.extend_from_slice(self.rf.cfg.punctuation.field_key_value_separator.as_bytes())
});
if self.rf.unescape_fields {
self.format_value(s, value, filter, setting);
} else {
Expand All @@ -335,9 +354,9 @@ impl<'a> FieldFormatter<'a> {
b'"' => {
s.element(Element::String, |s| {
s.batch(|buf| {
buf.push(b'\'');
buf.extend_from_slice(self.rf.cfg.punctuation.string_opening_quote.as_bytes());
format_str_unescaped(buf, value.get());
buf.push(b'\'');
buf.extend_from_slice(self.rf.cfg.punctuation.string_closing_quote.as_bytes());
})
});
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ fn run() -> Result<()> {
settings: settings.fields,
filter: Arc::new(fields),
},
formatting: settings.formatting,
time_zone: tz,
hide_empty_fields,
});
Expand Down
36 changes: 36 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Settings {
pub concurrency: Option<usize>,
pub time_format: String,
pub time_zone: Tz,
pub formatting: Formatting,
pub theme: String,
}

Expand Down Expand Up @@ -115,3 +116,38 @@ pub struct Field {
}

// ---

#[derive(Debug, Deserialize, Clone)]
pub struct Formatting {
pub punctuation: Punctuation,
}

// ---

#[derive(Debug, Deserialize, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct Punctuation {
pub logger_name_separator: String,
pub field_key_value_separator: String,
pub string_opening_quote: String,
pub string_closing_quote: String,
pub source_location_separator: String,
pub hidden_fields_indicator: String,
pub level_left_separator: String,
pub level_right_separator: String,
}

impl Default for Punctuation {
fn default() -> Self {
Self {
logger_name_separator: ":".into(),
field_key_value_separator: ":".into(),
string_opening_quote: "'".into(),
string_closing_quote: "'".into(),
source_location_separator: "@ ".into(),
hidden_fields_indicator: " ...".into(),
level_left_separator: "|".into(),
level_right_separator: "|".into(),
}
}
}

0 comments on commit 7092d15

Please sign in to comment.