Skip to content

Commit

Permalink
feat: CLi support raw data highlight & automatically scroll to end on…
Browse files Browse the repository at this point in the history
… commit
  • Loading branch information
honhimW committed Dec 17, 2024
1 parent dab198d commit 2b3c899
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 19 deletions.
22 changes: 17 additions & 5 deletions src/components/console_output.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::components::console_output::OutputKind::{ERR, STD};
use std::borrow::Cow;
use ratatui::layout::{Position, Rect};
use ratatui::prelude::Text;
use ratatui::style::{Style};
use ratatui::style::{Style, Stylize};
use ratatui::widgets::{Paragraph, Wrap};
use ratatui_macros::{line, span};
use std::cmp;
use strum::Display;
use OutputKind::{Else, CMD};
use OutputKind::{ERR, STD, CMD, Else, Raw};
use crate::components::raw_value::raw_value_to_highlight_text;
use crate::theme::get_color;

pub struct ConsoleData<'a> {
Expand All @@ -24,7 +25,8 @@ pub enum OutputKind {
CMD,
STD,
ERR,
Else(Style)
Else(Style),
Raw,
}

impl ConsoleData<'_> {
Expand All @@ -44,13 +46,23 @@ impl ConsoleData<'_> {
let mut text = Text::default();
for (kind, l) in self.lines.iter() {
let new_line = match kind {

CMD => line![span!(Style::default().fg(get_color(|t| &t.tab.cli.console.cmd)); l.clone())],
STD => line![span!(Style::default().fg(get_color(|t| &t.tab.cli.console.out)); l.clone())],
ERR => line![span!(Style::default().fg(get_color(|t| &t.tab.cli.console.err)); l.clone())],
Else(style) => line![span!(*style; l.clone())],
Raw => line![],
};
text.push_line(new_line);
if matches!(kind, Raw) {
let (highlight_text, content_type) = raw_value_to_highlight_text(Cow::from(l.clone()), true);
let ct = content_type.map(|ct| ct.to_string()).unwrap_or_default();
text.push_line(line![span!(Style::default().dim(); format!("```{ct}"))]);
for x in highlight_text.lines {
text.push_line(x)
}
text.push_line(line![span!(Style::default().dim(); "```")])
}

}
let mut paragraph = Paragraph::new(text).wrap(Wrap { trim: false });
paragraph = paragraph.scroll((self.position.y, self.position.x));
Expand Down
2 changes: 1 addition & 1 deletion src/components/hash_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl HashValue {
.map(|content| {
let mut text = Text::default();
text.push_line(Line::default());
let highlight_text = raw_value_to_highlight_text(Cow::from(content), false);
let (highlight_text, _) = raw_value_to_highlight_text(Cow::from(content), false);
for line in highlight_text.lines {
text.push_line(line);
}
Expand Down
8 changes: 8 additions & 0 deletions src/components/highlight_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,23 +106,31 @@ impl HighlightProcessor {
}
let is_json = self.process_json()?;
if is_json {
self.content_type = Some(ContentType::Json);
return Ok(());
}
let is_xml = self.process_xml()?;
if is_xml {
self.content_type = Some(ContentType::Xml);
return Ok(());
}
let is_ron = self.process_ron()?;
if is_ron {
self.content_type = Some(ContentType::Ron);
return Ok(());
}
let is_plain = self.process_plain()?;
if is_plain {
self.content_type = Some(ContentType::String);
return Ok(());
}
Ok(())
}

pub fn get_content_type(&self) -> Option<ContentType> {
self.content_type.clone()
}

#[allow(unused)]
pub fn get_cursor_path(&self, row: usize, column: usize) -> Result<String> {
if let Some(tree) = &self.tree {
Expand Down
2 changes: 1 addition & 1 deletion src/components/list_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl ListValue {
.map(|content| {
let mut text = Text::default();
text.push_line(Line::default());
let highlight_text = raw_value_to_highlight_text(Cow::from(content), false);
let (highlight_text, _) = raw_value_to_highlight_text(Cow::from(content), false);
for line in highlight_text.lines {
text.push_line(line);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/raw_paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct RawParagraph<'a> {

impl<'a> RawParagraph<'a> {
pub fn new(raw: String, content_type: Option<ContentType>, format: bool) -> Self {
let text = raw_value_to_highlight_text_with_content_type(Cow::from(raw.clone()), content_type.clone(), format);
let (text, _) = raw_value_to_highlight_text_with_content_type(Cow::from(raw.clone()), content_type.clone(), format);
let paragraph = Paragraph::new(text).wrap(Wrap { trim: false });
Self {
raw,
Expand Down
7 changes: 4 additions & 3 deletions src/components/raw_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use crate::theme::get_color;
use crate::utils::ContentType;
use crate::components::highlight_value::{HighlightKind, HighlightProcessor, HighlightText};

pub fn raw_value_to_highlight_text(value: Cow<str>, format: bool) -> Text {
pub fn raw_value_to_highlight_text(value: Cow<str>, format: bool) -> (Text, Option<ContentType>) {
raw_value_to_highlight_text_with_content_type(value, None, format)
}

pub fn raw_value_to_highlight_text_with_content_type(value: Cow<str>, content_type: Option<ContentType>, format: bool) -> Text {
pub fn raw_value_to_highlight_text_with_content_type(value: Cow<str>, content_type: Option<ContentType>, format: bool) -> (Text, Option<ContentType>) {
let mut processor = HighlightProcessor::new(value.to_string(), content_type);
if !format {
processor.disable_formatting();
Expand Down Expand Up @@ -58,5 +58,6 @@ pub fn raw_value_to_highlight_text_with_content_type(value: Cow<str>, content_ty
}
}
}
text

(text, processor.get_content_type())
}
2 changes: 1 addition & 1 deletion src/components/set_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl SetValue {
.map(|content| {
let mut text = Text::default();
text.push_line(Line::default());
let highlight_text = raw_value_to_highlight_text(Cow::from(content), false);
let (highlight_text, _) = raw_value_to_highlight_text(Cow::from(content), false);
for line in highlight_text.lines {
text.push_line(line);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/stream_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ impl SteamView {
.map(|content| {
let mut text = Text::default();
text.push_line(Line::default());
let highlight_text = raw_value_to_highlight_text(Cow::from(content), false);
let (highlight_text, _) = raw_value_to_highlight_text(Cow::from(content), false);
for line in highlight_text.lines {
text.push_line(line);
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/zset_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl ZSetValue {
.map(|content| {
let mut text = Text::default();
text.push_line(Line::default());
let highlight_text = raw_value_to_highlight_text(Cow::from(content), false);
let (highlight_text, _) = raw_value_to_highlight_text(Cow::from(content), false);
for line in highlight_text.lines {
text.push_line(line);
}
Expand Down
13 changes: 9 additions & 4 deletions src/tabs/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ impl CliTab {
self.lock_input = true;
self.lock_at = Some(Instant::now());
self.console_data.push(OutputKind::CMD, format!(">_ {}", command));
self.scroll_end();
self.console_data.build_paragraph();
if command.is_empty() {
self.lock_input = false;
Expand Down Expand Up @@ -466,10 +467,14 @@ fn value_to_lines(value: &Value, pad: u16) -> Vec<(OutputKind, String)> {
}
Value::BulkString(bulk_string) => {
let bulk_string = bytes_to_string(bulk_string.clone()).unwrap_or_else(|e| e.to_string());
let bulk_string = bulk_string.replace("\t", "\\t");
// let bulk_string = format!("\"{}\"", bulk_string);
let lines = bulk_string.lines();
lines.map(|line| format(line)).collect_vec()
if pad == 0 {
vec![(OutputKind::Raw, bulk_string)]
} else {
let bulk_string = bulk_string.replace("\t", "\\t");
// let bulk_string = format!("\"{}\"", bulk_string);
let lines = bulk_string.lines();
lines.map(|line| format(line)).collect_vec()
}
}
Value::Array(array) => {
let mut lines = vec![];
Expand Down
3 changes: 2 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use ron::ser::PrettyConfig;
use serde::Serialize;
use tui_textarea::TextArea;
use jaded::Parser;
use strum::Display;

#[allow(unused)]
#[derive(Default, Clone)]
#[derive(Default, Clone, Display)]
pub enum ContentType {
String,
#[default]
Expand Down

0 comments on commit 2b3c899

Please sign in to comment.