From b30caba358c3b96af2e52d0bcd6c9e34bb230077 Mon Sep 17 00:00:00 2001 From: rhysd Date: Sat, 11 Jun 2022 14:07:14 +0900 Subject: [PATCH] make crossterm optional --- .github/workflows/ci.yml | 8 ++++++-- Cargo.toml | 9 ++++++++- src/input.rs | 3 +++ src/textarea.rs | 2 +- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3872d30..4affff0 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,6 +4,8 @@ on: [push, pull_request] jobs: lint: runs-on: ubuntu-latest + env: + CARGO_TERM_COLOR: always steps: - uses: actions/checkout@v3 - uses: actions-rs/toolchain@v1 @@ -14,6 +16,8 @@ jobs: override: true - uses: Swatinem/rust-cache@v1 - name: rustfmt - run: cargo fmt -- --check --color always + run: cargo fmt -- --check - name: clippy - run: cargo clippy --color always -- -D warnings + run: cargo clippy -- -D warnings + - name: clippy with --no-default-features + run: cargo clippy --no-default-features -- -D warnings diff --git a/Cargo.toml b/Cargo.toml index 4ca401f..c7d493b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,6 +3,13 @@ name = "tui-textarea" version = "0.0.0" edition = "2021" +[features] +default = ["crossterm"] + [dependencies] -crossterm = "0.23.2" +crossterm = { version = "0.23", optional = true } tui = "0.18" + +[[example]] +name = "minimal" +required-features = ["crossterm"] diff --git a/src/input.rs b/src/input.rs index a708706..bb87f26 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "crossterm")] use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; #[derive(Clone, Copy, Debug)] @@ -33,6 +34,7 @@ impl Default for Input { } } +#[cfg(feature = "crossterm")] impl From for Input { fn from(event: Event) -> Self { if let Event::Key(key) = event { @@ -43,6 +45,7 @@ impl From for Input { } } +#[cfg(feature = "crossterm")] impl From for Input { fn from(key: KeyEvent) -> Self { let ctrl = key.modifiers.contains(KeyModifiers::CONTROL); diff --git a/src/textarea.rs b/src/textarea.rs index 7c4b9ea..7ef6562 100644 --- a/src/textarea.rs +++ b/src/textarea.rs @@ -312,7 +312,7 @@ impl<'a> TextArea<'a> { .char_indices() .nth(chars) .map(|(i, _)| i) - .unwrap_or(line[i..].len()); + .unwrap_or_else(|| line[i..].len()); let removed = line[i..i + bytes].to_string(); line.replace_range(i..i + bytes, ""); line.push(' ');