mirror of
https://github.com/tealdeer-rs/tealdeer.git
synced 2026-08-23 08:34:18 +02:00
Render indents without styles
This commit is contained in:
parent
d0108b23e4
commit
a788eb8de1
6 changed files with 120 additions and 60 deletions
|
|
@ -7,6 +7,7 @@ use crate::{config::Indent, extensions::FindFrom, types::LineType};
|
|||
#[derive(Debug, Clone, Copy, Eq)]
|
||||
/// Represents a snippet from a page of a specific highlighting class.
|
||||
pub enum PageSnippet<T> {
|
||||
Indent(T),
|
||||
CommandName(T),
|
||||
Variable(T),
|
||||
NormalCode(T),
|
||||
|
|
@ -23,6 +24,7 @@ impl<T> PageSnippet<T> {
|
|||
F: FnOnce(T) -> U,
|
||||
{
|
||||
match self {
|
||||
PageSnippet::Indent(s) => PageSnippet::Indent(f(s)),
|
||||
PageSnippet::CommandName(s) => PageSnippet::CommandName(f(s)),
|
||||
PageSnippet::Variable(s) => PageSnippet::Variable(f(s)),
|
||||
PageSnippet::NormalCode(s) => PageSnippet::NormalCode(f(s)),
|
||||
|
|
@ -37,7 +39,8 @@ impl<T> PageSnippet<T> {
|
|||
impl<T: PartialEq<U>, U> PartialEq<PageSnippet<U>> for PageSnippet<T> {
|
||||
fn eq(&self, other: &PageSnippet<U>) -> bool {
|
||||
match (self, other) {
|
||||
(PageSnippet::CommandName(s), PageSnippet::CommandName(t))
|
||||
(PageSnippet::Indent(s), PageSnippet::Indent(t))
|
||||
| (PageSnippet::CommandName(s), PageSnippet::CommandName(t))
|
||||
| (PageSnippet::Variable(s), PageSnippet::Variable(t))
|
||||
| (PageSnippet::NormalCode(s), PageSnippet::NormalCode(t))
|
||||
| (PageSnippet::Description(s), PageSnippet::Description(t))
|
||||
|
|
@ -54,9 +57,8 @@ impl PageSnippet<&str> {
|
|||
use PageSnippet::*;
|
||||
|
||||
match self {
|
||||
CommandName(s) | Variable(s) | NormalCode(s) | Description(s) | Text(s) | Title(s) => {
|
||||
s.is_empty()
|
||||
}
|
||||
Indent(s) | CommandName(s) | Variable(s) | NormalCode(s) | Description(s) | Text(s)
|
||||
| Title(s) => s.is_empty(),
|
||||
Linebreak => false,
|
||||
}
|
||||
}
|
||||
|
|
@ -87,7 +89,7 @@ where
|
|||
LineType::Title(title) => {
|
||||
if show_title {
|
||||
process_snippet(PageSnippet::Linebreak)?;
|
||||
process_snippet(PageSnippet::Title(&base_indent))?;
|
||||
process_snippet(PageSnippet::Indent(&base_indent))?;
|
||||
process_snippet(PageSnippet::Title(&title))?;
|
||||
process_snippet(PageSnippet::Linebreak)?;
|
||||
} else {
|
||||
|
|
@ -99,17 +101,17 @@ where
|
|||
debug!("Detected command name: {}", &command);
|
||||
}
|
||||
LineType::Description(text) => {
|
||||
process_snippet(PageSnippet::Description(&base_indent))?;
|
||||
process_snippet(PageSnippet::Indent(&base_indent))?;
|
||||
process_snippet(PageSnippet::Description(&text))?;
|
||||
process_snippet(PageSnippet::Linebreak)?;
|
||||
}
|
||||
LineType::ExampleText(text) => {
|
||||
process_snippet(PageSnippet::Text(&base_indent))?;
|
||||
process_snippet(PageSnippet::Indent(&base_indent))?;
|
||||
process_snippet(PageSnippet::Text(&text))?;
|
||||
process_snippet(PageSnippet::Linebreak)?;
|
||||
}
|
||||
LineType::ExampleCode(text) => {
|
||||
process_snippet(PageSnippet::NormalCode(&command_indent))?;
|
||||
process_snippet(PageSnippet::Indent(&command_indent))?;
|
||||
highlight_code(&command, &text, process_snippet)?;
|
||||
process_snippet(PageSnippet::Linebreak)?;
|
||||
}
|
||||
|
|
@ -242,6 +244,45 @@ fn is_freestanding_substring(surrounding: &str, substring: (usize, usize)) -> bo
|
|||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn indentation_uses_dedicated_snippets() {
|
||||
let lines = [
|
||||
LineType::Title("tldr".to_owned()),
|
||||
LineType::Description("description".to_owned()),
|
||||
LineType::ExampleText("example".to_owned()),
|
||||
LineType::ExampleCode("tldr --help".to_owned()),
|
||||
];
|
||||
let mut snippets = Vec::new();
|
||||
|
||||
highlight_lines(
|
||||
lines.into_iter(),
|
||||
&mut |snippet| {
|
||||
snippets.push(snippet.map(str::to_owned));
|
||||
Ok::<(), ()>(())
|
||||
},
|
||||
true,
|
||||
true,
|
||||
Indent {
|
||||
base: 2,
|
||||
command: 4,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
snippets
|
||||
.iter()
|
||||
.filter(|snippet| matches!(snippet, PageSnippet::Indent(_)))
|
||||
.collect::<Vec<_>>(),
|
||||
[
|
||||
&PageSnippet::Indent(" ".to_owned()),
|
||||
&PageSnippet::Indent(" ".to_owned()),
|
||||
&PageSnippet::Indent(" ".to_owned()),
|
||||
&PageSnippet::Indent(" ".to_owned())
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_is_freestanding_substring() {
|
||||
assert!(is_freestanding_substring("I love tldr", (0, 1)));
|
||||
|
|
|
|||
|
|
@ -87,6 +87,7 @@ fn print_snippet(
|
|||
use PageSnippet::*;
|
||||
|
||||
match snip {
|
||||
Indent(s) => write!(writer, "{s}"),
|
||||
CommandName(s) | Title(s) => write!(writer, "{}", s.paint(style.command_name)),
|
||||
Variable(s) => write!(writer, "{}", s.paint(style.example_variable)),
|
||||
NormalCode(s) => write!(writer, "{}", s.paint(style.example_code)),
|
||||
|
|
@ -95,3 +96,21 @@ fn print_snippet(
|
|||
Linebreak => writeln!(writer),
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn indent_output_is_unstyled() {
|
||||
let mut output = Vec::new();
|
||||
let style = StyleConfig {
|
||||
example_code: yansi::Style::new().underline(),
|
||||
..StyleConfig::default()
|
||||
};
|
||||
|
||||
print_snippet(&mut output, PageSnippet::Indent(" "), &style).unwrap();
|
||||
|
||||
assert_eq!(output, b" ");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,35 +3,35 @@
|
|||
Ubuntuのバージョンが16.04か、それ以降で対話モードを使う場合`apt-get`の代わりとして使用します。
|
||||
詳しくはこちら: <https://manned.org/apt.8>
|
||||
|
||||
[32m [0m[32m利用可能なパーケージとバージョンのリストの更新(他の`apt`コマンドの前での実行を推奨):[0m
|
||||
[32m利用可能なパーケージとバージョンのリストの更新(他の`apt`コマンドの前での実行を推奨):[0m
|
||||
|
||||
[36m [0m[36msudo [0m[36mapt[0m[36m update[0m
|
||||
[36msudo [0m[36mapt[0m[36m update[0m
|
||||
|
||||
[32m [0m[32m指定されたパッケージの検索:[0m
|
||||
[32m指定されたパッケージの検索:[0m
|
||||
|
||||
[36m [0m[36mapt[0m[36m search [0m[4;36mパッケージ[0m
|
||||
[36mapt[0m[36m search [0m[4;36mパッケージ[0m
|
||||
|
||||
[32m [0m[32mパッケージの情報を出力:[0m
|
||||
[32mパッケージの情報を出力:[0m
|
||||
|
||||
[36m [0m[36mapt[0m[36m show [0m[4;36mパッケージ[0m
|
||||
[36mapt[0m[36m show [0m[4;36mパッケージ[0m
|
||||
|
||||
[32m [0m[32mパッケージのインストール、または利用可能な最新バージョンに更新:[0m
|
||||
[32mパッケージのインストール、または利用可能な最新バージョンに更新:[0m
|
||||
|
||||
[36m [0m[36msudo [0m[36mapt[0m[36m install [0m[4;36mパッケージ[0m
|
||||
[36msudo [0m[36mapt[0m[36m install [0m[4;36mパッケージ[0m
|
||||
|
||||
[32m [0m[32mパッケージの削除(`sudo apt remove --purge`の場合設定ファイルも削除):[0m
|
||||
[32mパッケージの削除(`sudo apt remove --purge`の場合設定ファイルも削除):[0m
|
||||
|
||||
[36m [0m[36msudo [0m[36mapt[0m[36m remove [0m[4;36mパッケージ[0m
|
||||
[36msudo [0m[36mapt[0m[36m remove [0m[4;36mパッケージ[0m
|
||||
|
||||
[32m [0m[32mインストールされている全てのパッケージを最新のバージョンにアップグレード:[0m
|
||||
[32mインストールされている全てのパッケージを最新のバージョンにアップグレード:[0m
|
||||
|
||||
[36m [0m[36msudo [0m[36mapt[0m[36m upgrade[0m
|
||||
[36msudo [0m[36mapt[0m[36m upgrade[0m
|
||||
|
||||
[32m [0m[32mインストールできるすべてのパッケージを表示:[0m
|
||||
[32mインストールできるすべてのパッケージを表示:[0m
|
||||
|
||||
[36m [0m[36mapt[0m[36m list[0m
|
||||
[36mapt[0m[36m list[0m
|
||||
|
||||
[32m [0m[32mインストールされた全てのパッケージを表示(依存関係も表示):[0m
|
||||
[32mインストールされた全てのパッケージを表示(依存関係も表示):[0m
|
||||
|
||||
[36m [0m[36mapt[0m[36m list --installed[0m
|
||||
[36mapt[0m[36m list --installed[0m
|
||||
|
||||
|
|
|
|||
|
|
@ -2,31 +2,31 @@
|
|||
An SVG (Scalable Vector Graphics) editing program.
|
||||
Use -z to not open the GUI and only process files in the console.
|
||||
|
||||
[32m [0m[32mOpen an SVG file in the Inkscape GUI:[0m
|
||||
[32mOpen an SVG file in the Inkscape GUI:[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m [0m[4;36mfilename.svg[0m
|
||||
[36minkscape[0m[36m [0m[4;36mfilename.svg[0m
|
||||
|
||||
[32m [0m[32mExport an SVG file into a bitmap with the default format (PNG) and the default resolution (90 DPI):[0m
|
||||
[32mExport an SVG file into a bitmap with the default format (PNG) and the default resolution (90 DPI):[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m -e [0m[4;36mfilename.png[0m
|
||||
[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m -e [0m[4;36mfilename.png[0m
|
||||
|
||||
[32m [0m[32mExport an SVG file into a bitmap of 600x400 pixels (aspect ratio distortion may occur):[0m
|
||||
[32mExport an SVG file into a bitmap of 600x400 pixels (aspect ratio distortion may occur):[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m -e [0m[4;36mfilename.png[0m[36m -w [0m[4;36m600[0m[36m -h [0m[4;36m400[0m
|
||||
[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m -e [0m[4;36mfilename.png[0m[36m -w [0m[4;36m600[0m[36m -h [0m[4;36m400[0m
|
||||
|
||||
[32m [0m[32mExport a single object, given its ID, into a bitmap:[0m
|
||||
[32mExport a single object, given its ID, into a bitmap:[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m -i [0m[4;36mid[0m[36m -e [0m[4;36mobject.png[0m
|
||||
[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m -i [0m[4;36mid[0m[36m -e [0m[4;36mobject.png[0m
|
||||
|
||||
[32m [0m[32mExport an SVG document to PDF, converting all texts to paths:[0m
|
||||
[32mExport an SVG document to PDF, converting all texts to paths:[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m | [0m[36minkscape[0m[36m | [0m[36minkscape[0m[36m --export-pdf=[0m[4;36minkscape.pdf[0m[36m | [0m[36minkscape[0m[36m | [0m[36minkscape[0m[36m --export-text-to-path[0m
|
||||
[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m | [0m[36minkscape[0m[36m | [0m[36minkscape[0m[36m --export-pdf=[0m[4;36minkscape.pdf[0m[36m | [0m[36minkscape[0m[36m | [0m[36minkscape[0m[36m --export-text-to-path[0m
|
||||
|
||||
[32m [0m[32mDuplicate the object with id="path123", rotate the duplicate 90 degrees, save the file, and quit Inkscape:[0m
|
||||
[32mDuplicate the object with id="path123", rotate the duplicate 90 degrees, save the file, and quit Inkscape:[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m --select=path123 --verb=EditDuplicate --verb=ObjectRotate90 --verb=FileSave --verb=FileQuit[0m
|
||||
[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m --select=path123 --verb=EditDuplicate --verb=ObjectRotate90 --verb=FileSave --verb=FileQuit[0m
|
||||
|
||||
[32m [0m[32mSome invalid command just to test the correct highlighting of the command name:[0m
|
||||
[32mSome invalid command just to test the correct highlighting of the command name:[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m --use-inkscape=v3.0 file[0m
|
||||
[36minkscape[0m[36m --use-inkscape=v3.0 file[0m
|
||||
|
||||
|
|
|
|||
|
|
@ -2,31 +2,31 @@
|
|||
An SVG (Scalable Vector Graphics) editing program.
|
||||
Use -z to not open the GUI and only process files in the console.
|
||||
|
||||
[44;30m [0m[44;30mOpen an SVG file in the Inkscape GUI:[0m
|
||||
[44;30mOpen an SVG file in the Inkscape GUI:[0m
|
||||
|
||||
[1minkscape[0m [3;4mfilename.svg[0m
|
||||
|
||||
[44;30m [0m[44;30mExport an SVG file into a bitmap with the default format (PNG) and the default resolution (90 DPI):[0m
|
||||
[44;30mExport an SVG file into a bitmap with the default format (PNG) and the default resolution (90 DPI):[0m
|
||||
|
||||
[1minkscape[0m [3;4mfilename.svg[0m -e [3;4mfilename.png[0m
|
||||
|
||||
[44;30m [0m[44;30mExport an SVG file into a bitmap of 600x400 pixels (aspect ratio distortion may occur):[0m
|
||||
[44;30mExport an SVG file into a bitmap of 600x400 pixels (aspect ratio distortion may occur):[0m
|
||||
|
||||
[1minkscape[0m [3;4mfilename.svg[0m -e [3;4mfilename.png[0m -w [3;4m600[0m -h [3;4m400[0m
|
||||
|
||||
[44;30m [0m[44;30mExport a single object, given its ID, into a bitmap:[0m
|
||||
[44;30mExport a single object, given its ID, into a bitmap:[0m
|
||||
|
||||
[1minkscape[0m [3;4mfilename.svg[0m -i [3;4mid[0m -e [3;4mobject.png[0m
|
||||
|
||||
[44;30m [0m[44;30mExport an SVG document to PDF, converting all texts to paths:[0m
|
||||
[44;30mExport an SVG document to PDF, converting all texts to paths:[0m
|
||||
|
||||
[1minkscape[0m [3;4mfilename.svg[0m | [1minkscape[0m | [1minkscape[0m --export-pdf=[3;4minkscape.pdf[0m | [1minkscape[0m | [1minkscape[0m --export-text-to-path
|
||||
|
||||
[44;30m [0m[44;30mDuplicate the object with id="path123", rotate the duplicate 90 degrees, save the file, and quit Inkscape:[0m
|
||||
[44;30mDuplicate the object with id="path123", rotate the duplicate 90 degrees, save the file, and quit Inkscape:[0m
|
||||
|
||||
[1minkscape[0m [3;4mfilename.svg[0m --select=path123 --verb=EditDuplicate --verb=ObjectRotate90 --verb=FileSave --verb=FileQuit
|
||||
|
||||
[44;30m [0m[44;30mSome invalid command just to test the correct highlighting of the command name:[0m
|
||||
[44;30mSome invalid command just to test the correct highlighting of the command name:[0m
|
||||
|
||||
[1minkscape[0m --use-inkscape=v3.0 file
|
||||
|
||||
|
|
|
|||
|
|
@ -1,34 +1,34 @@
|
|||
|
||||
[36m [0m[36minkscape[0m
|
||||
[36minkscape[0m
|
||||
|
||||
An SVG (Scalable Vector Graphics) editing program.
|
||||
Use -z to not open the GUI and only process files in the console.
|
||||
|
||||
[32m [0m[32mOpen an SVG file in the Inkscape GUI:[0m
|
||||
[32mOpen an SVG file in the Inkscape GUI:[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m [0m[4;36mfilename.svg[0m
|
||||
[36minkscape[0m[36m [0m[4;36mfilename.svg[0m
|
||||
|
||||
[32m [0m[32mExport an SVG file into a bitmap with the default format (PNG) and the default resolution (90 DPI):[0m
|
||||
[32mExport an SVG file into a bitmap with the default format (PNG) and the default resolution (90 DPI):[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m -e [0m[4;36mfilename.png[0m
|
||||
[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m -e [0m[4;36mfilename.png[0m
|
||||
|
||||
[32m [0m[32mExport an SVG file into a bitmap of 600x400 pixels (aspect ratio distortion may occur):[0m
|
||||
[32mExport an SVG file into a bitmap of 600x400 pixels (aspect ratio distortion may occur):[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m -e [0m[4;36mfilename.png[0m[36m -w [0m[4;36m600[0m[36m -h [0m[4;36m400[0m
|
||||
[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m -e [0m[4;36mfilename.png[0m[36m -w [0m[4;36m600[0m[36m -h [0m[4;36m400[0m
|
||||
|
||||
[32m [0m[32mExport a single object, given its ID, into a bitmap:[0m
|
||||
[32mExport a single object, given its ID, into a bitmap:[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m -i [0m[4;36mid[0m[36m -e [0m[4;36mobject.png[0m
|
||||
[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m -i [0m[4;36mid[0m[36m -e [0m[4;36mobject.png[0m
|
||||
|
||||
[32m [0m[32mExport an SVG document to PDF, converting all texts to paths:[0m
|
||||
[32mExport an SVG document to PDF, converting all texts to paths:[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m | [0m[36minkscape[0m[36m | [0m[36minkscape[0m[36m --export-pdf=[0m[4;36minkscape.pdf[0m[36m | [0m[36minkscape[0m[36m | [0m[36minkscape[0m[36m --export-text-to-path[0m
|
||||
[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m | [0m[36minkscape[0m[36m | [0m[36minkscape[0m[36m --export-pdf=[0m[4;36minkscape.pdf[0m[36m | [0m[36minkscape[0m[36m | [0m[36minkscape[0m[36m --export-text-to-path[0m
|
||||
|
||||
[32m [0m[32mDuplicate the object with id="path123", rotate the duplicate 90 degrees, save the file, and quit Inkscape:[0m
|
||||
[32mDuplicate the object with id="path123", rotate the duplicate 90 degrees, save the file, and quit Inkscape:[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m --select=path123 --verb=EditDuplicate --verb=ObjectRotate90 --verb=FileSave --verb=FileQuit[0m
|
||||
[36minkscape[0m[36m [0m[4;36mfilename.svg[0m[36m --select=path123 --verb=EditDuplicate --verb=ObjectRotate90 --verb=FileSave --verb=FileQuit[0m
|
||||
|
||||
[32m [0m[32mSome invalid command just to test the correct highlighting of the command name:[0m
|
||||
[32mSome invalid command just to test the correct highlighting of the command name:[0m
|
||||
|
||||
[36m [0m[36minkscape[0m[36m --use-inkscape=v3.0 file[0m
|
||||
[36minkscape[0m[36m --use-inkscape=v3.0 file[0m
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue