mirror of
https://github.com/denisidoro/navi
synced 2024-11-24 20:43:06 +00:00
Use crossterm instead of termion and terminal_width
This commit is contained in:
parent
78c99dcb4b
commit
243a2c8472
4 changed files with 166 additions and 102 deletions
165
Cargo.lock
generated
165
Cargo.lock
generated
|
@ -123,6 +123,31 @@ dependencies = [
|
|||
"lazy_static",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossterm"
|
||||
version = "0.19.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7c36c10130df424b2f3552fcc2ddcd9b28a27b1e54b358b45874f88d1ca6888c"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"crossterm_winapi",
|
||||
"lazy_static",
|
||||
"libc",
|
||||
"mio",
|
||||
"parking_lot",
|
||||
"signal-hook",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "crossterm_winapi"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0da8964ace4d3e4a044fd027919b2237000b24315a37c916f61809f1ff2140b9"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "directories-next"
|
||||
version = "2.0.0"
|
||||
|
@ -205,6 +230,15 @@ dependencies = [
|
|||
"hashbrown",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "instant"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "61124eeebbd69b8190558df225adf7e4caafce0d743919e5d6b19652314ec5ec"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
|
@ -217,6 +251,15 @@ version = "0.2.92"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "56d855069fafbb9b344c0f962150cd2c1187975cb1c22c1522c240d8c4986714"
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5a3c91c24eae6777794bb1997ad98bbb87daf92890acab859f7eaa4320333176"
|
||||
dependencies = [
|
||||
"scopeguard",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.14"
|
||||
|
@ -241,12 +284,35 @@ dependencies = [
|
|||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mio"
|
||||
version = "0.7.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cf80d3e903b34e0bd7282b218398aec54e082c840d9baf8339e0080a0c542956"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"log",
|
||||
"miow",
|
||||
"ntapi",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miow"
|
||||
version = "0.3.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9f1c5b025cda876f66ef43a113f91ebc9f4ccef34843000e0adf6ebbab84e21"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "navi"
|
||||
version = "2.14.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"crossterm",
|
||||
"directories-next",
|
||||
"edit",
|
||||
"lazy_static",
|
||||
|
@ -254,12 +320,19 @@ dependencies = [
|
|||
"remove_dir_all 0.7.0",
|
||||
"shellwords",
|
||||
"strip-ansi-escapes",
|
||||
"terminal_size",
|
||||
"termion",
|
||||
"thiserror",
|
||||
"walkdir",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ntapi"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num_cpus"
|
||||
version = "1.13.0"
|
||||
|
@ -270,18 +343,37 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "numtoa"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef"
|
||||
|
||||
[[package]]
|
||||
name = "os_str_bytes"
|
||||
version = "2.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85"
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot"
|
||||
version = "0.11.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d7744ac029df22dca6284efe4e898991d28e3085c706c972bcd7da4a27a15eb"
|
||||
dependencies = [
|
||||
"instant",
|
||||
"lock_api",
|
||||
"parking_lot_core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "parking_lot_core"
|
||||
version = "0.8.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fa7a782938e745763fe6907fc6ba86946d72f49fe7e21de074e08128a99fb018"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"instant",
|
||||
"libc",
|
||||
"redox_syscall",
|
||||
"smallvec",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ppv-lite86"
|
||||
version = "0.2.10"
|
||||
|
@ -404,15 +496,6 @@ dependencies = [
|
|||
"bitflags",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_termios"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8440d8acb4fd3d277125b4bd01a6f38aee8d814b3b5fc09b3f2b825d37d3fe8f"
|
||||
dependencies = [
|
||||
"redox_syscall",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "redox_users"
|
||||
version = "0.4.0"
|
||||
|
@ -487,6 +570,32 @@ dependencies = [
|
|||
"regex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook"
|
||||
version = "0.1.17"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7e31d442c16f047a671b5a71e2161d6e68814012b7f5379d269ebd915fac2729"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"mio",
|
||||
"signal-hook-registry",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "signal-hook-registry"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "16f1d0fef1604ba8f7a073c7e701f213e056707210e9020af4528e0101ce11a6"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "smallvec"
|
||||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e"
|
||||
|
||||
[[package]]
|
||||
name = "strip-ansi-escapes"
|
||||
version = "0.1.0"
|
||||
|
@ -536,28 +645,6 @@ dependencies = [
|
|||
"winapi-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "terminal_size"
|
||||
version = "0.1.16"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "86ca8ced750734db02076f44132d802af0b33b09942331f4459dde8636fd2406"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "termion"
|
||||
version = "1.5.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "077185e2eac69c3f8379a4298e1e07cd36beb962290d4a51199acf0fdc10607e"
|
||||
dependencies = [
|
||||
"libc",
|
||||
"numtoa",
|
||||
"redox_syscall",
|
||||
"redox_termios",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.12.1"
|
||||
|
|
|
@ -17,10 +17,9 @@ travis-ci = { repository = "denisidoro/navi", branch = "master" }
|
|||
[dependencies]
|
||||
regex = "1.4.3"
|
||||
clap = "3.0.0-beta.2"
|
||||
termion = "1.5.6"
|
||||
crossterm = "0.19.0"
|
||||
lazy_static = "1.4.0"
|
||||
directories-next = "2.0.0"
|
||||
terminal_size = "0.1.16"
|
||||
walkdir = "2.3.1"
|
||||
shellwords = "1.1.0"
|
||||
anyhow = "1.0.38"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use terminal_size::{terminal_size, terminal_size_using_fd, Height, Width};
|
||||
pub use termion::color;
|
||||
pub use crossterm::style;
|
||||
use crossterm::terminal;
|
||||
|
||||
const FALLBACK_WIDTH: u16 = 80;
|
||||
|
||||
|
@ -39,30 +39,14 @@ fn width_with_shell_out() -> u16 {
|
|||
}
|
||||
}
|
||||
|
||||
fn width_with_fd() -> u16 {
|
||||
use std::fs;
|
||||
use std::os::unix::io::AsRawFd;
|
||||
|
||||
let file = fs::File::open("/dev/tty");
|
||||
|
||||
if let Ok(f) = file {
|
||||
let size = terminal_size_using_fd(f.as_raw_fd());
|
||||
|
||||
if let Some((Width(w), Height(_))) = size {
|
||||
w
|
||||
} else {
|
||||
width_with_shell_out()
|
||||
}
|
||||
pub fn width() -> u16 {
|
||||
if let Ok((w, _)) = terminal::size() {
|
||||
w
|
||||
} else {
|
||||
width_with_shell_out()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn width() -> u16 {
|
||||
let size = terminal_size();
|
||||
if let Some((Width(w), Height(_))) = size {
|
||||
w
|
||||
} else {
|
||||
width_with_fd()
|
||||
}
|
||||
pub fn parse_ansi(ansi: &str) -> Option<style::Color> {
|
||||
style::Color::parse_ansi(&format!("5;{}", ansi))
|
||||
}
|
||||
|
|
|
@ -1,7 +1,8 @@
|
|||
use crate::env_vars;
|
||||
use crate::finder;
|
||||
use crate::structures::item::Item;
|
||||
use crate::terminal::{self, color};
|
||||
use crate::terminal;
|
||||
use crate::terminal::style::{style, Color};
|
||||
use crate::writer;
|
||||
use std::cmp::max;
|
||||
use std::collections::HashSet;
|
||||
|
@ -18,26 +19,30 @@ pub fn parse_env_var<T: FromStr>(varname: &str) -> Option<T> {
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_ansi(varname: &str, default: Color) -> Color {
|
||||
let value: Option<String> = parse_env_var(varname);
|
||||
if let Some(v) = value {
|
||||
if let Some(a) = terminal::parse_ansi(&v) {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
default
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
pub static ref TAG_COLOR: color::AnsiValue =
|
||||
color::AnsiValue(parse_env_var(env_vars::TAG_COLOR).unwrap_or(14));
|
||||
pub static ref COMMENT_COLOR: color::AnsiValue =
|
||||
color::AnsiValue(parse_env_var(env_vars::COMMENT_COLOR).unwrap_or(4));
|
||||
pub static ref SNIPPET_COLOR: color::AnsiValue =
|
||||
color::AnsiValue(parse_env_var(env_vars::SNIPPET_COLOR).unwrap_or(7));
|
||||
pub static ref TAG_COLOR: Color = parse_ansi(env_vars::TAG_COLOR, Color::Cyan);
|
||||
pub static ref COMMENT_COLOR: Color = parse_ansi(env_vars::COMMENT_COLOR, Color::Blue);
|
||||
pub static ref SNIPPET_COLOR: Color = parse_ansi(env_vars::SNIPPET_COLOR, Color::White);
|
||||
pub static ref TAG_WIDTH_PERCENTAGE: u16 = parse_env_var(env_vars::TAG_WIDTH).unwrap_or(20);
|
||||
pub static ref COMMENT_WIDTH_PERCENTAGE: u16 = parse_env_var(env_vars::COMMENT_WIDTH).unwrap_or(40);
|
||||
}
|
||||
|
||||
pub fn preview(comment: &str, tags: &str, snippet: &str) {
|
||||
println!(
|
||||
"{comment_color}{comment} {tag_color}{tags} \n{snippet_color}{snippet}",
|
||||
comment = comment.to_string(),
|
||||
tags = format!("[{}]", tags),
|
||||
snippet = writer::fix_newlines(snippet),
|
||||
comment_color = color::Fg(*COMMENT_COLOR),
|
||||
tag_color = color::Fg(*TAG_COLOR),
|
||||
snippet_color = color::Fg(*SNIPPET_COLOR),
|
||||
"{comment} {tags} \n{snippet}",
|
||||
comment = style(comment).with(*COMMENT_COLOR),
|
||||
tags = style(format!("[{}]", tags)).with(*TAG_COLOR),
|
||||
snippet = style(writer::fix_newlines(snippet)).with(*SNIPPET_COLOR),
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -57,9 +62,8 @@ pub fn preview_var(selection: &str, query: &str, variable: &str) {
|
|||
let delimiter = env::var(env_vars::PREVIEW_DELIMITER).ok();
|
||||
let map = env::var(env_vars::PREVIEW_MAP).ok();
|
||||
|
||||
let reset = color::Fg(color::Reset);
|
||||
let active_color = color::Fg(*TAG_COLOR);
|
||||
let inactive_color = color::Fg(*COMMENT_COLOR);
|
||||
let active_color = *TAG_COLOR;
|
||||
let inactive_color = *COMMENT_COLOR;
|
||||
|
||||
let mut colored_snippet = String::from(snippet);
|
||||
let mut visited_vars: HashSet<&str> = HashSet::new();
|
||||
|
@ -67,12 +71,9 @@ pub fn preview_var(selection: &str, query: &str, variable: &str) {
|
|||
let mut variables = String::from("");
|
||||
|
||||
println!(
|
||||
"{comment_color}{comment} {tag_color}{tags}{reset}",
|
||||
comment = comment,
|
||||
tags = format!("[{}]", tags),
|
||||
comment_color = color::Fg(*COMMENT_COLOR),
|
||||
tag_color = color::Fg(*TAG_COLOR),
|
||||
reset = reset,
|
||||
"{comment} {tags}",
|
||||
comment = style(comment).with(*COMMENT_COLOR),
|
||||
tags = style(format!("[{}]", tags)).with(*TAG_COLOR),
|
||||
);
|
||||
|
||||
let bracketed_current_variable = format!("<{}>", variable);
|
||||
|
@ -110,20 +111,16 @@ pub fn preview_var(selection: &str, query: &str, variable: &str) {
|
|||
};
|
||||
|
||||
let replacement = format!(
|
||||
"{color}{variable}{reset}",
|
||||
color = variable_color,
|
||||
variable = bracketed_variable_name,
|
||||
reset = reset
|
||||
"{variable}",
|
||||
variable = style(bracketed_variable_name).with(variable_color),
|
||||
);
|
||||
|
||||
colored_snippet = colored_snippet.replace(bracketed_variable_name, &replacement);
|
||||
|
||||
variables = format!(
|
||||
"{variables}\n{color}{variable}{reset} = {value}",
|
||||
"{variables}\n{variable} = {value}",
|
||||
variables = variables,
|
||||
color = variable_color,
|
||||
variable = variable_name,
|
||||
reset = reset,
|
||||
variable = style(variable_name).with(variable_color),
|
||||
value = finder::process(value, column, delimiter.as_deref(), map.clone())
|
||||
.expect("Unable to process value"),
|
||||
);
|
||||
|
@ -166,13 +163,10 @@ impl Writer {
|
|||
impl writer::Writer for Writer {
|
||||
fn write(&mut self, item: Item) -> String {
|
||||
format!(
|
||||
"{tag_color}{tags_short}{delimiter}{comment_color}{comment_short}{delimiter}{snippet_color}{snippet_short}{delimiter}{tags}{delimiter}{comment}{delimiter}{snippet}{delimiter}{file_index}{delimiter}\n",
|
||||
tags_short = limit_str(item.tags, self.tag_width),
|
||||
comment_short = limit_str(item.comment, self.comment_width),
|
||||
snippet_short = writer::fix_newlines(item.snippet),
|
||||
comment_color = color::Fg(*COMMENT_COLOR),
|
||||
tag_color = color::Fg(*TAG_COLOR),
|
||||
snippet_color = color::Fg(*SNIPPET_COLOR),
|
||||
"{tags_short}{delimiter}{comment_short}{delimiter}{snippet_short}{delimiter}{tags}{delimiter}{comment}{delimiter}{snippet}{delimiter}{file_index}{delimiter}\n",
|
||||
tags_short = style(limit_str(item.tags, self.tag_width)).with(*TAG_COLOR),
|
||||
comment_short = style(limit_str(item.comment, self.comment_width)).with(*COMMENT_COLOR),
|
||||
snippet_short = style(writer::fix_newlines(item.snippet)).with(*SNIPPET_COLOR),
|
||||
tags = item.tags,
|
||||
comment = item.comment,
|
||||
delimiter = writer::DELIMITER,
|
||||
|
|
Loading…
Reference in a new issue