mirror of
https://github.com/nushell/nushell
synced 2024-11-10 07:04:13 +00:00
Cat with wings (#1993)
* WIP - Modified textview to use bat crate * use input_from_bytes_with_name instead of input_file * removed old paging added prettyprint on else blocks duplicated too much code hard coded defaults Co-authored-by: Darren Schroeder <fdncred@hotmail.com>
This commit is contained in:
parent
1b6f94b46c
commit
6914099e28
3 changed files with 391 additions and 241 deletions
322
Cargo.lock
generated
322
Cargo.lock
generated
|
@ -24,6 +24,15 @@ dependencies = [
|
|||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ansi_colours"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d0f302a81afc6a7f4350c04f0ba7cfab529cc009bca3324b3fb5764e6add8b6"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ansi_term"
|
||||
version = "0.11.0"
|
||||
|
@ -42,6 +51,12 @@ dependencies = [
|
|||
"winapi 0.3.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anymap"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "33954243bd79057c2de7338850b85983a44588021f8a5fee574a8888c6de4344"
|
||||
|
||||
[[package]]
|
||||
name = "app_dirs"
|
||||
version = "1.2.1"
|
||||
|
@ -212,6 +227,35 @@ version = "0.12.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "53d1ccbaf7d9ec9537465a97bf19edc1a4e158ecb49fc16178202238c569cc42"
|
||||
|
||||
[[package]]
|
||||
name = "bat"
|
||||
version = "0.15.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "91f17c2d9e1cee447a788a15fa6819c0cb488fb2935e3e8c4e7120e1678b7aa8"
|
||||
dependencies = [
|
||||
"ansi_colours",
|
||||
"ansi_term 0.12.1",
|
||||
"atty",
|
||||
"clap",
|
||||
"console",
|
||||
"content_inspector",
|
||||
"dirs 2.0.2",
|
||||
"encoding",
|
||||
"error-chain",
|
||||
"git2",
|
||||
"globset",
|
||||
"lazy_static 1.4.0",
|
||||
"liquid",
|
||||
"path_abs",
|
||||
"semver 0.9.0",
|
||||
"serde 1.0.110",
|
||||
"serde_yaml",
|
||||
"shell-words",
|
||||
"syntect",
|
||||
"unicode-width",
|
||||
"wild",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "battery"
|
||||
version = "0.7.5"
|
||||
|
@ -433,6 +477,7 @@ dependencies = [
|
|||
"atty",
|
||||
"bitflags",
|
||||
"strsim",
|
||||
"term_size",
|
||||
"textwrap",
|
||||
"unicode-width",
|
||||
"vec_map",
|
||||
|
@ -505,12 +550,38 @@ dependencies = [
|
|||
"yaml-rust",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "console"
|
||||
version = "0.11.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8c0994e656bba7b922d8dd1245db90672ffb701e684e45be58f20719d69abc5a"
|
||||
dependencies = [
|
||||
"encode_unicode",
|
||||
"lazy_static 1.4.0",
|
||||
"libc",
|
||||
"regex",
|
||||
"terminal_size",
|
||||
"termios",
|
||||
"unicode-width",
|
||||
"winapi 0.3.8",
|
||||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "constant_time_eq"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc"
|
||||
|
||||
[[package]]
|
||||
name = "content_inspector"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b7bda66e858c683005a53a9a60c69a4aca7eeaa45d124526e389f7aec8e62f38"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "core-foundation"
|
||||
version = "0.6.4"
|
||||
|
@ -914,6 +985,70 @@ version = "0.3.6"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
|
||||
|
||||
[[package]]
|
||||
name = "encoding"
|
||||
version = "0.2.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6b0d943856b990d12d3b55b359144ff341533e516d94098b1d3fc1ac666d36ec"
|
||||
dependencies = [
|
||||
"encoding-index-japanese",
|
||||
"encoding-index-korean",
|
||||
"encoding-index-simpchinese",
|
||||
"encoding-index-singlebyte",
|
||||
"encoding-index-tradchinese",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding-index-japanese"
|
||||
version = "1.20141219.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "04e8b2ff42e9a05335dbf8b5c6f7567e5591d0d916ccef4e0b1710d32a0d0c91"
|
||||
dependencies = [
|
||||
"encoding_index_tests",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding-index-korean"
|
||||
version = "1.20141219.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4dc33fb8e6bcba213fe2f14275f0963fd16f0a02c878e3095ecfdf5bee529d81"
|
||||
dependencies = [
|
||||
"encoding_index_tests",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding-index-simpchinese"
|
||||
version = "1.20141219.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d87a7194909b9118fc707194baa434a4e3b0fb6a5a757c73c3adb07aa25031f7"
|
||||
dependencies = [
|
||||
"encoding_index_tests",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding-index-singlebyte"
|
||||
version = "1.20141219.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3351d5acffb224af9ca265f435b859c7c01537c0849754d3db3fdf2bfe2ae84a"
|
||||
dependencies = [
|
||||
"encoding_index_tests",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding-index-tradchinese"
|
||||
version = "1.20141219.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fd0e20d5688ce3cab59eb3ef3a2083a5c77bf496cb798dc6fcdb75f323890c18"
|
||||
dependencies = [
|
||||
"encoding_index_tests",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "encoding_index_tests"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a246d82be1c9d791c5dfde9a2bd045fc3cbba3fa2b11ad558f27d01712f00569"
|
||||
|
||||
[[package]]
|
||||
name = "encoding_rs"
|
||||
version = "0.8.23"
|
||||
|
@ -945,6 +1080,15 @@ dependencies = [
|
|||
"serde 1.0.110",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "error-chain"
|
||||
version = "0.12.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d371106cc88ffdfb1eabd7111e432da544f16f3e2d7bf1dfe8bf575f1df045cd"
|
||||
dependencies = [
|
||||
"version_check 0.9.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "failure"
|
||||
version = "0.1.8"
|
||||
|
@ -1334,6 +1478,19 @@ version = "0.3.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
|
||||
|
||||
[[package]]
|
||||
name = "globset"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ad1da430bd7281dde2576f44c84cc3f0f7b475e7202cd503042dff01a8c8120"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"bstr",
|
||||
"fnv",
|
||||
"log",
|
||||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heim"
|
||||
version = "0.1.0-beta.2"
|
||||
|
@ -1740,6 +1897,15 @@ dependencies = [
|
|||
"winapi-build",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kstring"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6382df53100fd22e149030b6634720c94a151076db8d727b3274d7070975d609"
|
||||
dependencies = [
|
||||
"serde 1.0.110",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kv-log-macro"
|
||||
version = "1.0.5"
|
||||
|
@ -1856,6 +2022,64 @@ version = "0.5.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"
|
||||
|
||||
[[package]]
|
||||
name = "liquid"
|
||||
version = "0.20.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "503b7cd741bf1a6c01bfdf697ba13f67e2c8e152920af25596763bb0dbcd6215"
|
||||
dependencies = [
|
||||
"doc-comment",
|
||||
"kstring",
|
||||
"liquid-core",
|
||||
"liquid-derive",
|
||||
"liquid-lib",
|
||||
"serde 1.0.110",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "liquid-core"
|
||||
version = "0.20.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9ea59709d9530bab4e9cc0ece12f20fe4999fdef90f7d89391b0fc9ff563b62b"
|
||||
dependencies = [
|
||||
"anymap",
|
||||
"chrono",
|
||||
"itertools",
|
||||
"kstring",
|
||||
"liquid-derive",
|
||||
"num-traits 0.2.11",
|
||||
"pest",
|
||||
"pest_derive",
|
||||
"serde 1.0.110",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "liquid-derive"
|
||||
version = "0.20.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cfef35f37f019e5dfc550517045078317f5d37afa64cbf246ecde616a7091cb0"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"proc-quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "liquid-lib"
|
||||
version = "0.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7c4aa47dc08fd8c6c8aea70a0da2a98c0f0416d49e8b03c5c46354ef559bee3c"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"itertools",
|
||||
"kstring",
|
||||
"liquid-core",
|
||||
"once_cell",
|
||||
"percent-encoding",
|
||||
"regex",
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.3.4"
|
||||
|
@ -2540,6 +2764,7 @@ name = "nu_plugin_textview"
|
|||
version = "0.15.1"
|
||||
dependencies = [
|
||||
"ansi_term 0.12.1",
|
||||
"bat",
|
||||
"crossterm",
|
||||
"nu-build",
|
||||
"nu-errors",
|
||||
|
@ -2547,6 +2772,7 @@ dependencies = [
|
|||
"nu-protocol",
|
||||
"nu-source",
|
||||
"syntect",
|
||||
"textwrap",
|
||||
"url",
|
||||
]
|
||||
|
||||
|
@ -2686,6 +2912,28 @@ version = "1.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0b631f7e854af39a1739f401cf34a8a013dfe09eac4fa4dba91e9768bd28168d"
|
||||
|
||||
[[package]]
|
||||
name = "onig"
|
||||
version = "6.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bd91ccd8a02fce2f7e8a86655aec67bc6c171e6f8e704118a0e8c4b866a05a8a"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"lazy_static 1.4.0",
|
||||
"libc",
|
||||
"onig_sys",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "onig_sys"
|
||||
version = "69.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3814583fad89f3c60ae0701d80e87e1fd3028741723deda72d0d4a0ecf0cb0db"
|
||||
dependencies = [
|
||||
"cc",
|
||||
"pkg-config",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "opaque-debug"
|
||||
version = "0.2.3"
|
||||
|
@ -2790,6 +3038,15 @@ version = "0.1.1"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a0858af4d9136275541f4eac7be1af70add84cf356d901799b065ac1b8ff6e2f"
|
||||
|
||||
[[package]]
|
||||
name = "path_abs"
|
||||
version = "0.5.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eb6b8e6dede0bf94e9300e669f335ba92d5fc9fc8be7f4b1ca8a05206489388c"
|
||||
dependencies = [
|
||||
"std_prelude",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.1.0"
|
||||
|
@ -3029,6 +3286,30 @@ dependencies = [
|
|||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-quote"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06ea4226882439d07839be9c7f683e13d6d69d9c2fe960d61f637d1e2fa4c081"
|
||||
dependencies = [
|
||||
"proc-macro-hack",
|
||||
"proc-macro2",
|
||||
"proc-quote-impl",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-quote-impl"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7fb3ec628b063cdbcf316e06a8b8c1a541d28fa6c0a8eacd2bfb2b7f49e88aa0"
|
||||
dependencies = [
|
||||
"proc-macro-hack",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ptree"
|
||||
version = "0.2.1"
|
||||
|
@ -3575,6 +3856,12 @@ dependencies = [
|
|||
"opaque-debug",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "shell-words"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b6fa3938c99da4914afedd13bf3d79bcb6c277d1b2c398d23257a304d9e1b074"
|
||||
|
||||
[[package]]
|
||||
name = "shell32-sys"
|
||||
version = "0.1.2"
|
||||
|
@ -3724,6 +4011,12 @@ version = "0.3.4"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7f3eb36b47e512f8f1c9e3d10c2c1965bc992bd9cdb024fa581e2194501c83d3"
|
||||
|
||||
[[package]]
|
||||
name = "std_prelude"
|
||||
version = "0.2.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8207e78455ffdf55661170876f88daf85356e4edd54e0a3dbc79586ca1e50cbe"
|
||||
|
||||
[[package]]
|
||||
name = "strip-ansi-escapes"
|
||||
version = "0.1.0"
|
||||
|
@ -3808,6 +4101,7 @@ dependencies = [
|
|||
"fnv",
|
||||
"lazy_static 1.4.0",
|
||||
"lazycell",
|
||||
"onig",
|
||||
"plist",
|
||||
"regex-syntax",
|
||||
"serde 1.0.110",
|
||||
|
@ -3876,6 +4170,25 @@ dependencies = [
|
|||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "terminal_size"
|
||||
version = "0.1.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8038f95fc7a6f351163f4b964af631bd26c9e828f7db085f2a84aca56f70d13b"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"winapi 0.3.8",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termios"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6f0fcee7b24a25675de40d5bb4de6e41b0df07bc9856295e7e2b3a3600c400c2"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.11.0"
|
||||
|
@ -4323,6 +4636,15 @@ version = "0.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "effc0e4ff8085673ea7b9b2e3c73f6bd4d118810c9009ed8f1e16bd96c331db6"
|
||||
|
||||
[[package]]
|
||||
name = "wild"
|
||||
version = "2.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "035793abb854745033f01a07647a79831eba29ec0be377205f2a25b0aa830020"
|
||||
dependencies = [
|
||||
"glob",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.2.8"
|
||||
|
|
|
@ -14,11 +14,14 @@ nu-plugin = { path = "../nu-plugin", version = "0.15.1" }
|
|||
nu-protocol = { path = "../nu-protocol", version = "0.15.1" }
|
||||
nu-source = { path = "../nu-source", version = "0.15.1" }
|
||||
nu-errors = { path = "../nu-errors", version = "0.15.1" }
|
||||
#nu-cli = { path = "../nu-cli", version = "0.15.1" }
|
||||
|
||||
crossterm = "0.17.5"
|
||||
syntect = { version = "4.2", default-features = false, features = ["default-fancy"]}
|
||||
ansi_term = "0.12.1"
|
||||
url = "2.1.1"
|
||||
bat = "0.15.4"
|
||||
textwrap = {version = "0.11.0", features = ["term_size"]}
|
||||
|
||||
[build-dependencies]
|
||||
nu-build = { version = "0.15.1", path = "../nu-build" }
|
||||
|
|
|
@ -1,23 +1,7 @@
|
|||
use crossterm::{
|
||||
event::{KeyCode, KeyEvent},
|
||||
ExecutableCommand,
|
||||
};
|
||||
|
||||
use nu_protocol::{Primitive, UntaggedValue, Value};
|
||||
use nu_source::AnchorLocation;
|
||||
|
||||
use syntect::easy::HighlightLines;
|
||||
use syntect::highlighting::{Style, ThemeSet};
|
||||
use syntect::parsing::SyntaxSet;
|
||||
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
|
||||
enum DrawCommand {
|
||||
DrawString(Style, String),
|
||||
NextLine,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct TextView;
|
||||
|
||||
|
@ -27,209 +11,14 @@ impl TextView {
|
|||
}
|
||||
}
|
||||
|
||||
fn paint_textview(
|
||||
draw_commands: &[DrawCommand],
|
||||
starting_row: usize,
|
||||
use_color_buffer: bool,
|
||||
) -> usize {
|
||||
let size = crossterm::terminal::size().unwrap_or_else(|_| (80, 24));
|
||||
|
||||
// render
|
||||
let mut pos = 0;
|
||||
let width = size.0 as usize;
|
||||
let height = size.1 as usize - 1;
|
||||
let mut frame_buffer = vec![];
|
||||
|
||||
for command in draw_commands {
|
||||
match command {
|
||||
DrawCommand::DrawString(style, string) => {
|
||||
for chr in string.chars() {
|
||||
if chr == '\t' {
|
||||
for _ in 0..8 {
|
||||
frame_buffer.push((
|
||||
' ',
|
||||
style.foreground.r,
|
||||
style.foreground.g,
|
||||
style.foreground.b,
|
||||
));
|
||||
}
|
||||
pos += 8;
|
||||
} else {
|
||||
frame_buffer.push((
|
||||
chr,
|
||||
style.foreground.r,
|
||||
style.foreground.g,
|
||||
style.foreground.b,
|
||||
));
|
||||
pos += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
DrawCommand::NextLine => {
|
||||
for _ in 0..(width - pos % width) {
|
||||
frame_buffer.push((' ', 0, 0, 0));
|
||||
}
|
||||
pos += width - pos % width;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let num_frame_buffer_rows = frame_buffer.len() / width;
|
||||
let buffer_needs_scrolling = num_frame_buffer_rows > height;
|
||||
|
||||
// display
|
||||
let mut ansi_strings = vec![];
|
||||
let mut normal_chars = vec![];
|
||||
|
||||
for c in
|
||||
&frame_buffer[starting_row * width..std::cmp::min(pos, (starting_row + height) * width)]
|
||||
{
|
||||
if use_color_buffer {
|
||||
ansi_strings.push(ansi_term::Colour::RGB(c.1, c.2, c.3).paint(format!("{}", c.0)));
|
||||
} else {
|
||||
normal_chars.push(c.0);
|
||||
}
|
||||
}
|
||||
|
||||
if buffer_needs_scrolling {
|
||||
let _ = std::io::stdout().execute(crossterm::cursor::MoveTo(0, 0));
|
||||
}
|
||||
|
||||
if use_color_buffer {
|
||||
print!("{}", ansi_term::ANSIStrings(&ansi_strings));
|
||||
} else {
|
||||
let s: String = normal_chars.into_iter().collect();
|
||||
print!("{}", s);
|
||||
}
|
||||
|
||||
if buffer_needs_scrolling {
|
||||
let _ = std::io::stdout().execute(crossterm::cursor::MoveTo(0, size.1));
|
||||
print!(
|
||||
"{}",
|
||||
ansi_term::Colour::Blue.paint("[ESC to quit, arrow keys to move]")
|
||||
);
|
||||
}
|
||||
|
||||
let _ = std::io::stdout().flush();
|
||||
|
||||
num_frame_buffer_rows
|
||||
}
|
||||
|
||||
fn scroll_view_lines_if_needed(draw_commands: Vec<DrawCommand>, use_color_buffer: bool) {
|
||||
let mut starting_row = 0;
|
||||
|
||||
if let Ok(_raw) = crossterm::terminal::enable_raw_mode() {
|
||||
let mut size = crossterm::terminal::size().unwrap_or_else(|_| (80, 24));
|
||||
let height = size.1 as usize - 1;
|
||||
|
||||
let mut max_bottom_line = paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||
|
||||
// Only scroll if needed
|
||||
if max_bottom_line > height as usize {
|
||||
let _ = std::io::stdout().execute(crossterm::cursor::Hide);
|
||||
|
||||
loop {
|
||||
if let Ok(ev) = crossterm::event::read() {
|
||||
if let crossterm::event::Event::Key(KeyEvent { code, modifiers }) = ev {
|
||||
match code {
|
||||
KeyCode::Esc => {
|
||||
break;
|
||||
}
|
||||
KeyCode::Up | KeyCode::Char('k') => {
|
||||
if starting_row > 0 {
|
||||
starting_row -= 1;
|
||||
max_bottom_line = paint_textview(
|
||||
&draw_commands,
|
||||
starting_row,
|
||||
use_color_buffer,
|
||||
);
|
||||
}
|
||||
}
|
||||
KeyCode::Down | KeyCode::Char('j') => {
|
||||
if starting_row < (max_bottom_line - height) {
|
||||
starting_row += 1;
|
||||
}
|
||||
max_bottom_line =
|
||||
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||
}
|
||||
KeyCode::Char('b')
|
||||
if modifiers.contains(crossterm::event::KeyModifiers::CONTROL) =>
|
||||
{
|
||||
starting_row -= std::cmp::min(height, starting_row);
|
||||
max_bottom_line =
|
||||
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||
}
|
||||
KeyCode::PageUp => {
|
||||
starting_row -= std::cmp::min(height, starting_row);
|
||||
max_bottom_line =
|
||||
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||
}
|
||||
KeyCode::Char('f')
|
||||
if modifiers.contains(crossterm::event::KeyModifiers::CONTROL) =>
|
||||
{
|
||||
if starting_row < (max_bottom_line - height) {
|
||||
starting_row += height;
|
||||
|
||||
if starting_row > (max_bottom_line - height) {
|
||||
starting_row = max_bottom_line - height;
|
||||
}
|
||||
}
|
||||
max_bottom_line =
|
||||
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||
}
|
||||
KeyCode::PageDown | KeyCode::Char(' ') => {
|
||||
if starting_row < (max_bottom_line - height) {
|
||||
starting_row += height;
|
||||
|
||||
if starting_row > (max_bottom_line - height) {
|
||||
starting_row = max_bottom_line - height;
|
||||
}
|
||||
}
|
||||
max_bottom_line =
|
||||
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(new_size) = crossterm::terminal::size() {
|
||||
if size != new_size {
|
||||
size = new_size;
|
||||
let _ = std::io::stdout().execute(crossterm::terminal::Clear(
|
||||
crossterm::terminal::ClearType::All,
|
||||
));
|
||||
max_bottom_line =
|
||||
paint_textview(&draw_commands, starting_row, use_color_buffer);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let _ = std::io::stdout().execute(crossterm::cursor::Show);
|
||||
let _ = crossterm::terminal::disable_raw_mode();
|
||||
}
|
||||
|
||||
println!()
|
||||
}
|
||||
|
||||
fn scroll_view(s: &str) {
|
||||
let mut v = vec![];
|
||||
for line in s.lines() {
|
||||
v.push(DrawCommand::DrawString(Style::default(), line.to_string()));
|
||||
v.push(DrawCommand::NextLine);
|
||||
}
|
||||
scroll_view_lines_if_needed(v, false);
|
||||
}
|
||||
|
||||
pub fn view_text_value(value: &Value) {
|
||||
let value_anchor = value.anchor();
|
||||
if let UntaggedValue::Primitive(Primitive::String(ref s)) = &value.value {
|
||||
if let Some(source) = value_anchor {
|
||||
let extension: Option<String> = match source {
|
||||
let file_path: Option<String> = match source {
|
||||
AnchorLocation::File(file) => {
|
||||
let path = Path::new(&file);
|
||||
path.extension().map(|x| x.to_string_lossy().to_string())
|
||||
Some(path.to_string_lossy().to_string())
|
||||
}
|
||||
AnchorLocation::Url(url) => {
|
||||
let url = url::Url::parse(&url);
|
||||
|
@ -237,7 +26,7 @@ pub fn view_text_value(value: &Value) {
|
|||
if let Some(mut segments) = url.path_segments() {
|
||||
if let Some(file) = segments.next_back() {
|
||||
let path = Path::new(file);
|
||||
path.extension().map(|x| x.to_string_lossy().to_string())
|
||||
Some(path.to_string_lossy().to_string())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
|
@ -252,38 +41,74 @@ pub fn view_text_value(value: &Value) {
|
|||
AnchorLocation::Source(_source) => None,
|
||||
};
|
||||
|
||||
match extension {
|
||||
Some(extension) => {
|
||||
// Load these once at the start of your program
|
||||
let ps: SyntaxSet =
|
||||
syntect::dumps::from_binary(include_bytes!("assets/syntaxes.bin"));
|
||||
|
||||
if let Some(syntax) = ps.find_syntax_by_extension(&extension) {
|
||||
let ts: ThemeSet =
|
||||
syntect::dumps::from_binary(include_bytes!("assets/themes.bin"));
|
||||
let mut h = HighlightLines::new(syntax, &ts.themes["OneHalfDark"]);
|
||||
|
||||
let mut v = vec![];
|
||||
for line in s.lines() {
|
||||
let ranges: Vec<(Style, &str)> = h.highlight(line, &ps);
|
||||
|
||||
for range in ranges {
|
||||
v.push(DrawCommand::DrawString(range.0, range.1.to_string()));
|
||||
}
|
||||
|
||||
v.push(DrawCommand::NextLine);
|
||||
}
|
||||
scroll_view_lines_if_needed(v, true);
|
||||
} else {
|
||||
scroll_view(s);
|
||||
}
|
||||
match file_path {
|
||||
Some(file_path) => {
|
||||
// Let bat do it's thing
|
||||
bat::PrettyPrinter::new()
|
||||
.input_from_bytes_with_name(s.as_bytes(), file_path)
|
||||
.term_width(textwrap::termwidth())
|
||||
.tab_width(Some(4))
|
||||
.colored_output(true)
|
||||
.true_color(true)
|
||||
.header(true)
|
||||
.line_numbers(true)
|
||||
.grid(true)
|
||||
.vcs_modification_markers(true)
|
||||
.snip(true)
|
||||
.wrapping_mode(bat::WrappingMode::NoWrapping)
|
||||
.use_italics(true)
|
||||
.paging_mode(bat::PagingMode::QuitIfOneScreen)
|
||||
.pager("less")
|
||||
.line_ranges(bat::line_range::LineRanges::all())
|
||||
.highlight_range(0, 0)
|
||||
.theme("OneHalfDark")
|
||||
.print()
|
||||
.expect("Error with bat PrettyPrint");
|
||||
}
|
||||
_ => {
|
||||
scroll_view(s);
|
||||
bat::PrettyPrinter::new()
|
||||
.input_from_bytes(s.as_bytes())
|
||||
.term_width(textwrap::termwidth())
|
||||
.tab_width(Some(4))
|
||||
.colored_output(true)
|
||||
.true_color(true)
|
||||
.header(true)
|
||||
.line_numbers(true)
|
||||
.grid(true)
|
||||
.vcs_modification_markers(true)
|
||||
.snip(true)
|
||||
.wrapping_mode(bat::WrappingMode::NoWrapping)
|
||||
.use_italics(true)
|
||||
.paging_mode(bat::PagingMode::QuitIfOneScreen)
|
||||
.pager("less")
|
||||
.line_ranges(bat::line_range::LineRanges::all())
|
||||
.highlight_range(0, 0)
|
||||
.theme("OneHalfDark")
|
||||
.print()
|
||||
.expect("Error with bat PrettyPrint");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
scroll_view(s);
|
||||
bat::PrettyPrinter::new()
|
||||
.input_from_bytes(s.as_bytes())
|
||||
.term_width(textwrap::termwidth())
|
||||
.tab_width(Some(4))
|
||||
.colored_output(true)
|
||||
.true_color(true)
|
||||
.header(true)
|
||||
.line_numbers(true)
|
||||
.grid(true)
|
||||
.vcs_modification_markers(true)
|
||||
.snip(true)
|
||||
.wrapping_mode(bat::WrappingMode::NoWrapping)
|
||||
.use_italics(true)
|
||||
.paging_mode(bat::PagingMode::QuitIfOneScreen)
|
||||
.pager("less")
|
||||
.line_ranges(bat::line_range::LineRanges::all())
|
||||
.highlight_range(0, 0)
|
||||
.theme("OneHalfDark")
|
||||
.print()
|
||||
.expect("Error with bat PrettyPrint");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue