Skip to content

Commit

Permalink
Merge pull request #3 from ynqa/v0.1.1/main
Browse files Browse the repository at this point in the history
v0.1.1
  • Loading branch information
ynqa authored May 30, 2024
2 parents 84be9a2 + 85989fb commit 62725ec
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 16 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
@@ -1,6 +1,6 @@
[package]
name = "sigrs"
version = "0.1.0"
version = "0.1.1"
authors = ["ynqa <un.pensiero.vano@gmail.com>"]
edition = "2021"
description = "Interactive grep (for streaming)"
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Interactive grep
based on key inputs at any given moment.
- Additionally, by starting in this mode,
it is also possible to grep through static data such as files.
- like [ugrep](https://github.com/Genivia/ugrep) with `-Q` option.

## Installation

Expand Down Expand Up @@ -114,11 +115,13 @@ Options:
--retrieval-timeout <RETRIEVAL_TIMEOUT_MILLIS>
Timeout to read a next line from the stream in milliseconds. [default: 10]
--render-interval <RENDER_INTERVAL_MILLIS>
Interval to render a log line in milliseconds. [default: 10]
Interval to render a line in milliseconds. [default: 10]
-q, --queue-capacity <QUEUE_CAPACITY>
Queue capacity to store the logs. [default: 1000]
--archived
Queue capacity to store lines. [default: 1000]
-a, --archived
Archived mode to grep through static data.
-i, --ignore-case
Case insensitive search.
-h, --help
Print help (see more with '--help')
-V, --version
Expand Down
12 changes: 11 additions & 1 deletion src/archived.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct Archived {
text_editor_snapshot: Snapshot<text_editor::State>,
lines: Snapshot<listbox::State>,
highlight_style: ContentStyle,
case_insensitive: bool,
}

impl promkit::Finalizer for Archived {
Expand Down Expand Up @@ -63,7 +64,14 @@ impl promkit::Renderer for Archived {
.listbox
.items()
.par_iter()
.filter_map(|line| sig::styled(&query, &line.to_string(), self.highlight_style))
.filter_map(|line| {
sig::styled(
&query,
&line.to_string(),
self.highlight_style,
self.case_insensitive,
)
})
.collect();

self.lines.after_mut().listbox = listbox::Listbox::from_iter(list);
Expand All @@ -76,13 +84,15 @@ pub fn run(
text_editor: text_editor::State,
lines: listbox::State,
highlight_style: ContentStyle,
case_insensitive: bool,
) -> anyhow::Result<()> {
Prompt {
renderer: Archived {
keymap: ActiveKeySwitcher::new("default", keymap::default),
text_editor_snapshot: Snapshot::new(text_editor),
lines: Snapshot::new(lines),
highlight_style,
case_insensitive,
},
}
.run()
Expand Down
26 changes: 19 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,40 @@ pub struct Args {
#[arg(
long = "render-interval",
default_value = "10",
help = "Interval to render a log line in milliseconds.",
help = "Interval to render a line in milliseconds.",
long_help = "Adjust this value to prevent screen flickering
when a large volume of logs is rendered in a short period."
when a large volume of lines is rendered in a short period."
)]
pub render_interval_millis: u64,

#[arg(
short = 'q',
long = "queue-capacity",
default_value = "1000",
help = "Queue capacity to store the logs.",
long_help = "Queue capacity for storing logs.
This value is used for temporary storage of log data
help = "Queue capacity to store lines.",
long_help = "Queue capacity for storing lines.
This value is used for temporary storage of lines
and should be adjusted based on the system's memory capacity.
Increasing this value allows for more logs to be stored temporarily,
which can be beneficial when digging deeper into logs with the digger."
Increasing this value allows for more lines to be stored temporarily,
which can be beneficial when digging deeper into lines with the digger."
)]
pub queue_capacity: usize,

#[arg(
short = 'a',
long = "archived",
default_value = "false",
help = "Archived mode to grep through static data."
)]
pub archived: bool,

#[arg(
short = 'i',
long = "ignore-case",
default_value = "false",
help = "Case insensitive search."
)]
pub case_insensitive: bool,
}

impl Drop for Args {
Expand Down Expand Up @@ -139,6 +148,7 @@ async fn main() -> anyhow::Result<()> {
lines: Default::default(),
},
highlight_style,
args.case_insensitive,
)?;
} else {
let queue = sig::run(
Expand All @@ -158,6 +168,7 @@ async fn main() -> anyhow::Result<()> {
Duration::from_millis(args.retrieval_timeout_millis),
Duration::from_millis(args.render_interval_millis),
args.queue_capacity,
args.case_insensitive,
)
.await?;

Expand Down Expand Up @@ -189,6 +200,7 @@ async fn main() -> anyhow::Result<()> {
lines: Default::default(),
},
highlight_style,
args.case_insensitive,
)?;
}

Expand Down
14 changes: 11 additions & 3 deletions src/sig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ use promkit::{
mod keymap;
use crate::{stdin, terminal::Terminal};

fn matched(queries: &[&str], line: &str) -> anyhow::Result<Vec<Match>> {
fn matched(queries: &[&str], line: &str, case_insensitive: bool) -> anyhow::Result<Vec<Match>> {
let mut matched = Vec::new();
RegexMatcherBuilder::new()
.case_insensitive(case_insensitive)
.build_many(queries)?
.find_iter_at(line.as_bytes(), 0, |m| {
if m.start() >= line.as_bytes().len() {
Expand All @@ -35,7 +36,12 @@ fn matched(queries: &[&str], line: &str) -> anyhow::Result<Vec<Match>> {
Ok(matched)
}

pub fn styled(query: &str, line: &str, highlight_style: ContentStyle) -> Option<StyledGraphemes> {
pub fn styled(
query: &str,
line: &str,
highlight_style: ContentStyle,
case_insensitive: bool,
) -> Option<StyledGraphemes> {
let piped = &query
.split('|')
.map(|s| s.trim())
Expand All @@ -47,7 +53,7 @@ pub fn styled(query: &str, line: &str, highlight_style: ContentStyle) -> Option<
if query.is_empty() {
Some(styled)
} else {
match matched(piped, line) {
match matched(piped, line, case_insensitive) {
Ok(matches) => {
if matches.is_empty() {
None
Expand All @@ -71,6 +77,7 @@ pub async fn run(
retrieval_timeout: Duration,
render_interval: Duration,
queue_capacity: usize,
case_insensitive: bool,
) -> anyhow::Result<VecDeque<String>> {
let keymap = ActiveKeySwitcher::new("default", keymap::default);
let size = crossterm::terminal::size()?;
Expand Down Expand Up @@ -112,6 +119,7 @@ pub async fn run(
&text_editor.texteditor.text_without_cursor().to_string(),
&line,
highlight_style,
case_insensitive,
) {
let matrix = styled.matrixify(size.0 as usize, size.1 as usize, 0).0;
let term = readonly_term.read().await;
Expand Down

0 comments on commit 62725ec

Please sign in to comment.