Skip to content

Commit

Permalink
new: added sync indicator in follow mode
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Jul 8, 2023
1 parent defcc42 commit 243eb4b
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 4 deletions.
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.20.0-beta.11"
version = "0.20.0-beta.11.1"
edition = "2021"
build = "build.rs"

Expand Down
1 change: 1 addition & 0 deletions benches/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ fn benchmark(c: &mut Criterion) {
}
.into(),
levels: HashMap::new(),
indicators: themecfg::IndicatorPack::default(),
});
let fields = vec![
(b"key1", b"value1"),
Expand Down
11 changes: 10 additions & 1 deletion etc/defaults/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ fields:
# Configuration of the predefined set of fields.
predefined:
time:
names: [ts, TS, time, TIME, Time, _SOURCE_REALTIME_TIMESTAMP, __REALTIME_TIMESTAMP]
names:
[
ts,
TS,
time,
TIME,
Time,
_SOURCE_REALTIME_TIMESTAMP,
__REALTIME_TIMESTAMP,
]
logger:
names: [logger, LOGGER, Logger]
level:
Expand Down
9 changes: 9 additions & 0 deletions etc/defaults/themes/universal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ levels:
level:
foreground: bright-red
modes: [reverse]
indicators:
sync:
synced:
text: ' '
failed:
text: '!'
inner:
style:
foreground: 'yellow'
8 changes: 8 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ impl App {

let mut window = BTreeMap::<Key,Line>::new();
let mut last_ts: Option<Timestamp> = None;
let mut prev_ts: Option<Timestamp> = None;
let mut mem_usage = 0;
let mem_limit = n * usize::from(self.options.buffer_size);

Expand All @@ -437,7 +438,14 @@ impl App {
break;
}
if let Some(entry) = window.pop_first() {
let sync_indicator = if prev_ts.map(|ts| ts <= entry.0.0).unwrap_or(true) {
&self.options.theme.indicators.sync.synced
} else {
&self.options.theme.indicators.sync.failed
};
prev_ts = Some(entry.0.0);
mem_usage -= entry.1.1.end - entry.1.1.start;
output.write_all(sync_indicator.value.as_bytes())?;
output.write_all(&entry.1.0[entry.1.1.clone()])?;
}
}
Expand Down
80 changes: 79 additions & 1 deletion src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ pub trait StylingPush<B: Push<u8>> {
pub struct Theme {
packs: EnumMap<Level, StylePack>,
default: StylePack,
pub indicators: IndicatorPack,
}

impl Theme {
pub fn none() -> Self {
Self {
packs: EnumMap::default(),
default: StylePack::default(),
indicators: IndicatorPack::default(),
}
}

Expand Down Expand Up @@ -81,7 +83,11 @@ impl<S: Borrow<themecfg::Theme>> From<S> for Theme {
for (level, pack) in &s.levels {
packs[*level] = StylePack::load(&s.elements.clone().merged(pack.clone()));
}
Self { default, packs }
Self {
default,
packs,
indicators: IndicatorPack::from(&s.indicators),
}
}
}

Expand All @@ -96,6 +102,17 @@ impl Style {
buf.extend_from_slice(self.0.data())
}

#[inline(always)]
pub fn with<B: Push<u8>, F: FnOnce(&mut B)>(&self, buf: &mut B, f: F) {
if self.0.data().is_empty() {
f(buf)
} else {
buf.extend_from_slice(self.0.data());
f(buf);
buf.extend_from_slice(Self::reset().0.data());
}
}

pub fn reset() -> Self {
Sequence::reset().into()
}
Expand Down Expand Up @@ -262,6 +279,67 @@ impl StylePack {

// ---

#[derive(Default)]
pub struct IndicatorPack {
pub sync: SyncIndicatorPack,
}

impl From<&themecfg::IndicatorPack> for IndicatorPack {
fn from(indicator: &themecfg::IndicatorPack) -> Self {
Self {
sync: SyncIndicatorPack::from(&indicator.sync),
}
}
}

// ---

#[derive(Default)]
pub struct SyncIndicatorPack {
pub synced: Indicator,
pub failed: Indicator,
}

impl From<&themecfg::SyncIndicatorPack> for SyncIndicatorPack {
fn from(indicator: &themecfg::SyncIndicatorPack) -> Self {
Self {
synced: Indicator::from(&indicator.synced),
failed: Indicator::from(&indicator.failed),
}
}
}

// ---

#[derive(Default)]
pub struct Indicator {
pub value: String,
}

impl From<&themecfg::Indicator> for Indicator {
fn from(indicator: &themecfg::Indicator) -> Self {
let mut buf = Vec::new();
let os = Style::from(&indicator.outer.style);
let is = Style::from(&indicator.inner.style);
os.apply(&mut buf);
os.with(&mut buf, |buf| {
buf.extend(indicator.outer.prefix.as_bytes());
is.with(buf, |buf| {
buf.extend(indicator.inner.prefix.as_bytes());
buf.extend(indicator.text.as_bytes());
buf.extend(indicator.outer.prefix.as_bytes());
});
buf.extend(indicator.outer.suffix.as_bytes());
});

Self {
value: String::from_utf8(buf).unwrap(),
}
}
}

// ---

#[cfg(test)]
mod tests {
use super::*;
Expand Down
66 changes: 66 additions & 0 deletions src/themecfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::{error::*, level::Level};
pub struct Theme {
pub elements: StylePack,
pub levels: HashMap<Level, StylePack>,
pub indicators: IndicatorPack,
}

impl Theme {
Expand Down Expand Up @@ -308,6 +309,71 @@ impl fmt::Display for RGB {

// ---

#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[serde(default)]
pub struct IndicatorPack {
pub sync: SyncIndicatorPack,
}

// ---

#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub struct SyncIndicatorPack {
pub synced: Indicator,
pub failed: Indicator,
}

impl Default for SyncIndicatorPack {
fn default() -> Self {
Self {
synced: Indicator {
outer: IndicatorStyle::default(),
inner: IndicatorStyle::default(),
text: " ".into(),
},
failed: Indicator {
outer: IndicatorStyle::default(),
inner: IndicatorStyle {
prefix: String::default(),
suffix: String::default(),
style: Style {
modes: Vec::default(),
background: None,
foreground: Some(Color::Plain(PlainColor::Yellow)),
},
},
text: "!".into(),
},
}
}
}

// ---

#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[serde(default)]
pub struct Indicator {
pub outer: IndicatorStyle,
pub inner: IndicatorStyle,
pub text: String,
}

// ---

#[derive(Clone, Debug, Default, Deserialize)]
#[serde(rename_all = "kebab-case")]
#[serde(default)]
pub struct IndicatorStyle {
pub prefix: String,
pub suffix: String,
pub style: Style,
}

// ---

#[derive(RustEmbed)]
#[folder = "etc/defaults/themes/"]
struct Assets;
Expand Down

0 comments on commit 243eb4b

Please sign in to comment.