summaryrefslogtreecommitdiffstats
path: root/src/render
diff options
context:
space:
mode:
Diffstat (limited to 'src/render')
-rw-r--r--src/render/cmd.rs80
-rw-r--r--src/render/markdown.rs36
-rw-r--r--src/render/mod.rs8
-rw-r--r--src/render/repl.rs12
4 files changed, 68 insertions, 68 deletions
diff --git a/src/render/cmd.rs b/src/render/cmd.rs
index 62a0a34..bf7a006 100644
--- a/src/render/cmd.rs
+++ b/src/render/cmd.rs
@@ -6,10 +6,11 @@ use crate::repl::{ReplyStreamEvent, SharedAbortSignal};
use anyhow::Result;
use crossbeam::channel::Receiver;
+#[allow(clippy::unnecessary_wraps, clippy::module_name_repetitions)]
pub fn cmd_render_stream(
- rx: Receiver<ReplyStreamEvent>,
+ rx: &Receiver<ReplyStreamEvent>,
light_theme: bool,
- abort: SharedAbortSignal,
+ abort: &SharedAbortSignal,
) -> Result<()> {
let mut buffer = String::new();
let mut markdown_render = MarkdownRender::new(light_theme);
@@ -25,7 +26,7 @@ pub fn cmd_render_stream(
let mut lines: Vec<&str> = text.split('\n').collect();
buffer = lines.pop().unwrap_or_default().to_string();
let output = lines.join("\n");
- print_now!("{}\n", markdown_render.render(&output))
+ print_now!("{}\n", markdown_render.render(&output));
} else {
buffer = format!("{buffer}{text}");
if !(markdown_render.is_code_block()
@@ -36,7 +37,7 @@ pub fn cmd_render_stream(
{
if let Some((output, remain)) = split_line(&buffer) {
print_now!("{}", markdown_render.render_line_stateless(&output));
- buffer = remain
+ buffer = remain;
}
}
}
@@ -74,8 +75,8 @@ fn split_line(line: &str) -> Option<(String, String)> {
index += 2;
continue;
}
- do_balance(&mut balance, &chars[index..index + 1]);
- index += 1
+ do_balance(&mut balance, &chars[index..=index]);
+ index += 1;
}
None
@@ -101,25 +102,25 @@ impl Kind {
fn from_chars(chars: &[char]) -> Option<Self> {
let kind = match chars.len() {
1 => match chars[0] {
- '(' => Kind::ParentheseStart,
- ')' => Kind::ParentheseEnd,
- '[' => Kind::BracketStart,
- ']' => Kind::BracketEnd,
- '*' => Kind::Asterisk,
- '\'' => Kind::SingleQuota,
- '"' => Kind::DoubleQuota,
- '~' => Kind::Tilde,
- '`' => Kind::Backtick,
+ '(' => Self::ParentheseStart,
+ ')' => Self::ParentheseEnd,
+ '[' => Self::BracketStart,
+ ']' => Self::BracketEnd,
+ '*' => Self::Asterisk,
+ '\'' => Self::SingleQuota,
+ '"' => Self::DoubleQuota,
+ '~' => Self::Tilde,
+ '`' => Self::Backtick,
_ => return None,
},
2 if chars[0] == chars[1] => match chars[0] {
- '*' => Kind::Asterisk2,
- '~' => Kind::Tilde2,
+ '*' => Self::Asterisk2,
+ '~' => Self::Tilde2,
_ => return None,
},
3 => {
if chars == ['`', '`', '`'] {
- Kind::Backtick3
+ Self::Backtick3
} else {
return None;
}
@@ -131,22 +132,12 @@ impl Kind {
}
fn do_balance(balance: &mut Vec<Kind>, chars: &[char]) -> bool {
- if let Some(kind) = Kind::from_chars(chars) {
+ Kind::from_chars(chars).map_or(false, |kind| {
let last = balance.last();
match (kind, last) {
- (Kind::ParentheseStart | Kind::BracketStart, _) => {
- balance.push(kind);
- true
- }
- (Kind::ParentheseEnd, Some(&Kind::ParentheseStart)) => {
- balance.pop();
- true
- }
- (Kind::BracketEnd, Some(&Kind::BracketStart)) => {
- balance.pop();
- true
- }
- (Kind::Asterisk, Some(&Kind::Asterisk))
+ (Kind::ParentheseEnd, Some(&Kind::ParentheseStart))
+ | (Kind::BracketEnd, Some(&Kind::BracketStart))
+ | (Kind::Asterisk, Some(&Kind::Asterisk))
| (Kind::Asterisk2, Some(&Kind::Asterisk2))
| (Kind::SingleQuota, Some(&Kind::SingleQuota))
| (Kind::DoubleQuota, Some(&Kind::DoubleQuota))
@@ -157,22 +148,25 @@ fn do_balance(balance: &mut Vec<Kind>, chars: &[char]) -> bool {
balance.pop();
true
}
- (Kind::Asterisk, _)
- | (Kind::Asterisk2, _)
- | (Kind::SingleQuota, _)
- | (Kind::DoubleQuota, _)
- | (Kind::Tilde, _)
- | (Kind::Tilde2, _)
- | (Kind::Backtick, _)
- | (Kind::Backtick3, _) => {
+ (
+ Kind::ParentheseStart
+ | Kind::BracketStart
+ | Kind::Asterisk
+ | Kind::Asterisk2
+ | Kind::SingleQuota
+ | Kind::DoubleQuota
+ | Kind::Tilde
+ | Kind::Tilde2
+ | Kind::Backtick
+ | Kind::Backtick3,
+ _,
+ ) => {
balance.push(kind);
true
}
_ => false,
}
- } else {
- false
- }
+ })
}
#[cfg(test)]
diff --git a/src/render/markdown.rs b/src/render/markdown.rs
index 5bdca9d..75904af 100644
--- a/src/render/markdown.rs
+++ b/src/render/markdown.rs
@@ -8,6 +8,7 @@ use syntect::{easy::HighlightLines, parsing::SyntaxReference};
/// Monokai Extended
const MD_THEME: &[u8] = include_bytes!("../../assets/monokai-extended.theme.bin");
const MD_THEME_LIGHT: &[u8] = include_bytes!("../../assets/monokai-extended-light.theme.bin");
+#[allow(clippy::doc_markdown)]
/// Comes from https://github.com/sharkdp/bat/raw/5e77ca37e89c873e4490b42ff556370dc5c6ba4f/assets/syntaxes.bin
const SYNTAXES: &[u8] = include_bytes!("../../assets/syntaxes.bin");
@@ -20,6 +21,7 @@ lazy_static! {
};
}
+#[allow(clippy::module_name_repetitions)]
pub struct MarkdownRender {
syntax_set: SyntaxSet,
md_theme: Theme,
@@ -67,7 +69,7 @@ impl MarkdownRender {
output.unwrap_or_else(|| line.to_string())
}
- pub fn is_code_block(&self) -> bool {
+ pub const fn is_code_block(&self) -> bool {
matches!(
self.prev_line_type,
LineType::CodeBegin | LineType::CodeInner
@@ -123,13 +125,14 @@ impl MarkdownRender {
}
fn render_code_line(&self, line: &str) -> Option<String> {
- self.code_syntax
- .as_ref()
- .map(|syntax| self.render_line_inner(line, syntax))
- .unwrap_or_else(|| Some(format!("{}", line.with(self.code_color))))
+ self.code_syntax.as_ref().map_or_else(
+ || Some(format!("{}", line.with(self.code_color))),
+ |syntax| self.render_line_inner(line, syntax),
+ )
}
fn find_syntax(&self, lang: &str) -> Option<&SyntaxReference> {
+ #[allow(clippy::option_if_let_else)]
if let Some(new_lang) = LANGE_MAPS.get(&lang.to_ascii_lowercase()) {
self.syntax_set.find_syntax_by_name(new_lang)
} else {
@@ -154,17 +157,17 @@ fn as_terminal_escaped(ranges: &[(Style, &str)]) -> String {
let fg = blend_fg_color(style.foreground, style.background);
let mut text = text.with(convert_color(fg));
if style.font_style.contains(FontStyle::BOLD) {
- text = text.bold()
+ text = text.bold();
}
if style.font_style.contains(FontStyle::UNDERLINE) {
- text = text.underlined()
+ text = text.underlined();
}
output.push_str(&text.to_string());
}
output
}
-fn convert_color(c: SyntectColor) -> Color {
+const fn convert_color(c: SyntectColor) -> Color {
Color::Rgb {
r: c.r,
g: c.g,
@@ -176,14 +179,14 @@ fn blend_fg_color(fg: SyntectColor, bg: SyntectColor) -> SyntectColor {
if fg.a == 0xff {
return fg;
}
- let ratio = fg.a as u32;
- let r = (fg.r as u32 * ratio + bg.r as u32 * (255 - ratio)) / 255;
- let g = (fg.g as u32 * ratio + bg.g as u32 * (255 - ratio)) / 255;
- let b = (fg.b as u32 * ratio + bg.b as u32 * (255 - ratio)) / 255;
+ let ratio = u32::from(fg.a);
+ let r = (u32::from(fg.r) * ratio + u32::from(bg.r) * (255 - ratio)) / 255;
+ let g = (u32::from(fg.g) * ratio + u32::from(bg.g) * (255 - ratio)) / 255;
+ let b = (u32::from(fg.b) * ratio + u32::from(bg.b) * (255 - ratio)) / 255;
SyntectColor {
- r: r as u8,
- g: g as u8,
- b: b as u8,
+ r: u8::try_from(r).unwrap_or(u8::MAX),
+ g: u8::try_from(g).unwrap_or(u8::MAX),
+ b: u8::try_from(b).unwrap_or(u8::MAX),
a: 255,
}
}
@@ -209,8 +212,7 @@ fn get_code_color(theme: &Theme) -> Color {
});
scope
.and_then(|v| v.style.foreground)
- .map(convert_color)
- .unwrap_or_else(|| Color::Yellow)
+ .map_or_else(|| Color::Yellow, convert_color)
}
#[cfg(test)]
diff --git a/src/render/mod.rs b/src/render/mod.rs
index 5980302..20d4d0e 100644
--- a/src/render/mod.rs
+++ b/src/render/mod.rs
@@ -3,6 +3,7 @@ mod markdown;
mod repl;
use self::cmd::cmd_render_stream;
+#[allow(clippy::module_name_repetitions)]
pub use self::markdown::MarkdownRender;
use self::repl::repl_render_stream;
@@ -16,10 +17,11 @@ use crossbeam::channel::unbounded;
use crossbeam::sync::WaitGroup;
use std::thread::spawn;
+#[allow(clippy::module_name_repetitions)]
pub fn render_stream(
input: &str,
client: &ChatGptClient,
- config: SharedConfig,
+ config: &SharedConfig,
repl: bool,
abort: SharedAbortSignal,
wg: WaitGroup,
@@ -30,9 +32,9 @@ pub fn render_stream(
let abort_clone = abort.clone();
spawn(move || {
let err = if repl {
- repl_render_stream(rx, light_theme, abort)
+ repl_render_stream(&rx, light_theme, &abort)
} else {
- cmd_render_stream(rx, light_theme, abort)
+ cmd_render_stream(&rx, light_theme, &abort)
};
if let Err(err) = err {
let err = format!("{err:?}");
diff --git a/src/render/repl.rs b/src/render/repl.rs
index 04dee73..2bdf3b2 100644
--- a/src/render/repl.rs
+++ b/src/render/repl.rs
@@ -16,10 +16,11 @@ use std::{
};
use unicode_width::UnicodeWidthStr;
+#[allow(clippy::module_name_repetitions)]
pub fn repl_render_stream(
- rx: Receiver<ReplyStreamEvent>,
+ rx: &Receiver<ReplyStreamEvent>,
light_theme: bool,
- abort: SharedAbortSignal,
+ abort: &SharedAbortSignal,
) -> Result<()> {
enable_raw_mode()?;
let mut stdout = io::stdout();
@@ -32,9 +33,9 @@ pub fn repl_render_stream(
}
fn repl_render_stream_inner(
- rx: Receiver<ReplyStreamEvent>,
+ rx: &Receiver<ReplyStreamEvent>,
light_theme: bool,
- abort: SharedAbortSignal,
+ abort: &SharedAbortSignal,
writer: &mut Stdout,
) -> Result<()> {
let mut last_tick = Instant::now();
@@ -118,7 +119,8 @@ fn repl_render_stream_inner(
}
fn recover_cursor(writer: &mut Stdout, terminal_columns: u16, buffer: &str) -> Result<()> {
- let buffer_rows = (buffer.width() as u16 + terminal_columns - 1) / terminal_columns;
+ let buffer_rows = (u16::try_from(buffer.width()).unwrap_or(u16::MAX) + terminal_columns - 1)
+ / terminal_columns;
let (_, row) = cursor::position()?;
if buffer_rows == 0 {
queue!(writer, cursor::MoveTo(0, row))?;