Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 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 crates/jp_cli/src/cmd/query/tool/prompter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use jp_conversation::event::SelectOption;
use jp_editor::{EditOutcome, EditorBackend};
use jp_inquire::{InlineOption, ReplyEditMode, ReplyOutcome, prompt::PromptBackend};
use jp_llm::tool::executor::PermissionInfo;
use jp_md::{format::DefaultBackground, shade::ShadedWriter};
use jp_printer::{Printer, PromptWriter};
use jp_term::{background::DefaultBackground, shade::ShadedWriter};
use jp_tool::AnswerType;
use serde_json::Value;

Expand Down
2 changes: 1 addition & 1 deletion crates/jp_cli/src/cmd/query/tool/prompter_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use std::{sync::Arc, time::Duration};

use jp_editor::MockEditorBackend;
use jp_inquire::{ReplyOutcome, prompt::MockPromptBackend};
use jp_md::format::BackgroundFill;
use jp_printer::{OutputFormat, PrintableExt as _, SharedBuffer};
use jp_term::background::BackgroundFill;
use serde_json::json;

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/jp_cli/src/cmd/query/turn/coordinator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use jp_llm::{
event::{Event, EventPart, FinishReason},
event_builder::EventBuilder,
};
use jp_md::format::DefaultBackground;
use jp_printer::Printer;
use jp_term::background::DefaultBackground;

use crate::{
cmd::query::{interrupt::InterruptAction, stream::TurnView},
Expand Down
6 changes: 2 additions & 4 deletions crates/jp_cli/src/render/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,11 @@ use jp_config::style::{
use jp_conversation::event::ChatResponse;
use jp_md::{
buffer::{Buffer, Event, Fixups},
format::{
BackgroundFill, CodeBlockState, DefaultBackground, Formatter, TerminalOptions,
render_separator,
},
format::{CodeBlockState, Formatter, TerminalOptions, render_separator},
theme,
};
use jp_printer::{OutputWidth, PrintableExt as _, Printer, RegionStyle, StatusRegion};
use jp_term::background::{BackgroundFill, DefaultBackground};
use tracing::warn;

/// The kind of content last pushed into the renderer.
Expand Down
7 changes: 2 additions & 5 deletions crates/jp_cli/src/render/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,9 @@ use jp_config::{
};
use jp_conversation::event::ToolCallResponse;
use jp_llm::{CommandResult, run_tool_command, tool::InvocationContext};
use jp_md::{
format::{DefaultBackground, Formatter},
shade::ShadedWriter,
};
use jp_md::format::Formatter;
use jp_printer::{ErrChannel, LineSink, OutputLines, RegionStyle, StatusRegion};
use jp_term::osc::hyperlink;
use jp_term::{background::DefaultBackground, osc::hyperlink, shade::ShadedWriter};
use serde_json::{Map, Value};
use tokio_util::sync::CancellationToken;
use tracing::warn;
Expand Down
2 changes: 1 addition & 1 deletion crates/jp_cli/src/render/tool_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use jp_config::{
style::stderr_rows::{RowCount, StderrRows},
};
use jp_conversation::event::ToolCallResponse;
use jp_md::format::{BackgroundFill, DefaultBackground};
use jp_printer::{ErrChannel, OutputFormat, Printer, SharedBuffer, TerminalCapability};
use jp_term::background::{BackgroundFill, DefaultBackground};
use serde_json::{Map, Value};

use super::*;
Expand Down
2 changes: 1 addition & 1 deletion crates/jp_cli/src/render/turn_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use std::sync::{

use jp_config::style::StyleConfig;
use jp_conversation::event::{ChatRequest, ChatResponse};
use jp_md::format::DefaultBackground;
use jp_printer::Printer;
use jp_term::background::DefaultBackground;

use super::{ChatRenderer, RenderFlow, StructuredRenderer};

Expand Down
2 changes: 2 additions & 0 deletions crates/jp_md/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ repository.workspace = true
version.workspace = true

[dependencies]
jp_term = { workspace = true }

comrak = { workspace = true }
syntect = { workspace = true, features = ["regex-fancy", "default-themes", "default-syntaxes"] }
two-face = { workspace = true, features = ["syntect-fancy"] }
Expand Down
53 changes: 5 additions & 48 deletions crates/jp_md/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
//! Markdown formatting utilities.

use std::{borrow::Cow, fmt, sync::LazyLock};
use std::{fmt, sync::LazyLock};

use comrak::{
Arena,
nodes::{NodeList, NodeValue},
options::{Extension, ListStyleType, Render},
};
use jp_term::{
ansi::{self, AnsiState, Segment},
background::{DefaultBackground, line_fill},
};
use syntect::{highlighting::Theme, parsing::SyntaxSet};
use two_face::syntax;

use crate::{
ansi::{self, AnsiState, Segment},
render::{self, HrOptions, RenderOptions},
table::TableOptions,
theme,
Expand All @@ -33,19 +36,6 @@ const DEFAULT_WIDTH: usize = 80;
/// Default maximum column width for tables.
const DEFAULT_TABLE_MAX_COL_WIDTH: usize = 40;

/// How a default background color fills each line.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackgroundFill {
/// Fill to the last visible character on the line.
Content,

/// Fill to a fixed column width (padding with spaces if needed).
Column(usize),

/// Fill to the end of the terminal window via `\x1b[K`.
Terminal,
}

/// Controls how horizontal rules (`---`) are rendered in terminal output.
#[derive(Debug, Clone, Copy, Default)]
pub enum HrStyle {
Expand All @@ -58,17 +48,6 @@ pub enum HrStyle {
Line,
}

/// A default background color applied to all content, with a fill mode
/// controlling how far it extends on each line.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DefaultBackground {
/// SGR background parameter, e.g. `"48;5;236"` or `"48;2;80;73;69"`.
pub param: String,

/// How far the background extends on each line.
pub fill: BackgroundFill,
}

/// Per-call options for [`Formatter::format_terminal_with`].
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct TerminalOptions {
Expand Down Expand Up @@ -484,28 +463,6 @@ fn ends_with_tight_list<'a>(root: &'a comrak::nodes::AstNode<'a>) -> bool {
)
}

/// The text that extends a background from `column` to the end of the line.
///
/// The one place [`BackgroundFill`] is interpreted.
/// Every writer that maintains a region background consults this so the three
/// modes cannot drift apart: `Content` adds nothing, `Terminal` defers to the
/// terminal's erase-to-end-of- line, and `Column` pads with real spaces — the
/// only form a host that lays out its own sub-window (an `fzf` preview pane)
/// renders, since it does not implement the erase.
///
/// The caller is responsible for having the background active before writing
/// the result.
pub(crate) fn line_fill(fill: BackgroundFill, column: usize) -> Cow<'static, str> {
match fill {
BackgroundFill::Content => Cow::Borrowed(""),
BackgroundFill::Terminal => Cow::Borrowed("\x1b[K"),
BackgroundFill::Column(target) => match target.saturating_sub(column) {
0 => Cow::Borrowed(""),
pad => Cow::Owned(" ".repeat(pad)),
},
}
}

/// Render an inter-block separator (blank line) with optional background fill.
/// Used between blocks and after closing code fences.
///
Expand Down
2 changes: 2 additions & 0 deletions crates/jp_md/src/format_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use jp_term::background::BackgroundFill;

use super::*;

struct TestCase {
Expand Down
2 changes: 0 additions & 2 deletions crates/jp_md/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@
reason = "we don't host the docs, and use them mainly for LSP integration"
)]

mod ansi;
pub mod buffer;
pub mod format;
pub mod heading;
mod render;
pub mod shade;
mod table;
pub mod theme;
mod writer;
11 changes: 7 additions & 4 deletions crates/jp_md/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ use comrak::{
NodeTaskItem, NodeValue,
},
};
use syntect::highlighting::Theme;

use crate::{
use jp_term::{
ansi::{
BG_END, BOLD_END, BOLD_START, FG_END, ITALIC_END, ITALIC_START, STRIKETHROUGH_END,
STRIKETHROUGH_START, UNDERLINE_END, UNDERLINE_START,
},
format::{DefaultBackground, HrStyle, SYNTAXES},
background::DefaultBackground,
};
use syntect::highlighting::Theme;

use crate::{
format::{HrStyle, SYNTAXES},
table,
writer::TerminalWriter,
};
Expand Down
6 changes: 2 additions & 4 deletions crates/jp_md/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,11 @@
use std::{cmp::min, fmt::Write as _};

use comrak::nodes::{NodeValue, TableAlignment};
use jp_term::ansi::{self, AnsiState, RESET, Segment};
use unicode_segmentation::UnicodeSegmentation as _;
use unicode_width::UnicodeWidthStr as _;

use crate::{
ansi::{self, AnsiState, RESET, Segment},
render::{RenderOptions, TerminalFormatter},
};
use crate::render::{RenderOptions, TerminalFormatter};

/// Type alias for comrak AST node references.
type Node<'a> = &'a comrak::nodes::AstNode<'a>;
Expand Down
12 changes: 7 additions & 5 deletions crates/jp_md/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ use std::{
fmt::{self, Write},
};

use crate::{
use jp_term::{
ansi::{self, AnsiState, RESET, Segment},
format::{self, BackgroundFill, DefaultBackground},
background::{BackgroundFill, DefaultBackground, line_fill},
};

use crate::format;

/// ANSI-aware terminal writer with word-wrapping support.
#[expect(clippy::struct_excessive_bools)]
pub struct TerminalWriter<'w> {
Expand Down Expand Up @@ -432,8 +434,8 @@ impl<'w> TerminalWriter<'w> {

/// Extend the active background to the end of the current line.
///
/// The fill itself comes from [`format::line_fill`], the single
/// interpretation of [`BackgroundFill`].
/// The fill itself comes from [`line_fill`], the single interpretation of
/// [`BackgroundFill`].
/// The background escape always precedes it so a temporary background
/// (inline code, say) can't bleed into the fill.
///
Expand All @@ -444,7 +446,7 @@ impl<'w> TerminalWriter<'w> {
return Ok(());
};

let fill = format::line_fill(bg.fill, self.column);
let fill = line_fill(bg.fill, self.column);
if fill.is_empty() {
return Ok(());
}
Expand Down
1 change: 0 additions & 1 deletion crates/jp_md/src/writer_tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::*;
use crate::format::{BackgroundFill, DefaultBackground};

/// Regression test: a 1-byte prefix used to underflow in `write_prefix` because
/// `prefix.len() - 2` wraps to `usize::MAX`.
Expand Down
Loading
Loading