Skip to content

Commit 88fa1fb

Browse files
committed
Upgrade crossterm and add value_and_reset()
Closes: #32 Closes: #31
1 parent c38ec5f commit 88fa1fb

File tree

6 files changed

+44
-40
lines changed

6 files changed

+44
-40
lines changed

.github/workflows/ci.yml

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,24 @@ name: Continuous Integration
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88

99
env:
1010
CARGO_TERM_COLOR: always
1111

1212
jobs:
1313
build:
14-
1514
runs-on: ubuntu-latest
1615

1716
steps:
18-
- uses: actions/checkout@v2
19-
- name: Build
20-
run: cargo build --verbose
21-
- name: Run tests
22-
run: cargo test --verbose
23-
- name: Run lint
24-
run: cargo clippy --verbose
25-
- name: Run fmt
26-
run: cargo fmt --verbose
17+
- uses: actions/checkout@v2
18+
- name: Build
19+
run: cargo build
20+
- name: Run tests
21+
run: cargo test --all-features
22+
- name: Run lint
23+
run: cargo clippy
24+
- name: Run fmt
25+
run: cargo fmt

Cargo.toml

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "tui-input"
3-
version = "0.11.1"
3+
version = "0.12.0"
44
edition = "2021"
55
authors = ["Arijit Basu <[email protected]>"]
66
description = "TUI input library supporting multiple backends"
@@ -14,18 +14,15 @@ autoexamples = true
1414

1515
[features]
1616
default = ["crossterm"]
17-
crossterm = ["dep:ratatui"]
18-
serde = ["dep:serde"]
19-
termion = ["dep:termion"]
2017

2118
[dependencies]
22-
ratatui = { version = "0.29", optional = true }
19+
unicode-width = "0.2.0"
2320
serde = { version = "1.0.218", optional = true, features = ["derive"] }
21+
crossterm = { version = "0.29.0", optional = true }
2422
termion = { version = "4.0.4", optional = true }
25-
unicode-width = "0.2.0"
2623

2724
[dev-dependencies]
28-
crossterm = { version = "0.28.1" }
25+
ratatui = { version = "0.30.0-alpha.2", features = ["crossterm"] }
2926

3027
[[example]]
3128
name = "crossterm_input"

examples/ratatui_input.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ impl App {
7070
}
7171

7272
fn push_message(&mut self) {
73-
self.messages.push(self.input.value().into());
74-
self.input.reset();
73+
self.messages.push(self.input.value_and_reset());
7574
}
7675

7776
fn render(&self, frame: &mut Frame) {

src/backend/crossterm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::{Input, InputRequest, StateChanged};
2-
use ratatui::crossterm::event::{
2+
use crossterm::event::{
33
Event as CrosstermEvent, KeyCode, KeyEvent, KeyEventKind, KeyModifiers,
44
};
5-
use ratatui::crossterm::{
5+
use crossterm::{
66
cursor::MoveTo,
77
queue,
88
style::{Attribute as CAttribute, Print, SetAttribute},
@@ -120,7 +120,7 @@ impl EventHandler for Input {
120120

121121
#[cfg(test)]
122122
mod tests {
123-
use ratatui::crossterm::event::{KeyEventKind, KeyEventState};
123+
use crossterm::event::{KeyEventKind, KeyEventState};
124124

125125
use super::*;
126126

src/input.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
//! Core logic for handling input.
2+
//!
3+
//! # Example: Without any backend
4+
//!
5+
//! ```
6+
//! use tui_input::{Input, InputRequest, StateChanged};
7+
//!
8+
//! let mut input: Input = "Hello Worl".into();
9+
//!
10+
//! let req = InputRequest::InsertChar('d');
11+
//! let resp = input.handle(req);
12+
//!
13+
//! assert_eq!(resp, Some(StateChanged { value: true, cursor: true }));
14+
//! assert_eq!(input.cursor(), 11);
15+
//! assert_eq!(input.to_string(), "Hello World");
16+
//! ```
17+
118
/// Input requests are used to change the input state.
219
///
320
/// Different backends can be used to convert events into requests.
@@ -77,6 +94,13 @@ impl Input {
7794
self.value = Default::default();
7895
}
7996

97+
// Reset the cursor and value to default, returning the previous value
98+
pub fn value_and_reset(&mut self) -> String {
99+
let val = self.value.clone();
100+
self.reset();
101+
val
102+
}
103+
80104
/// Handle request and emit response.
81105
pub fn handle(&mut self, req: InputRequest) -> InputResponse {
82106
use InputRequest::*;

src/lib.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
11
//! TUI input library supporting multiple backends.
22
//!
3-
//! # Example: Without any backend
4-
//!
5-
//! ```
6-
//! use tui_input::{Input, InputRequest, StateChanged};
7-
//!
8-
//! let mut input: Input = "Hello Worl".into();
9-
//!
10-
//! let req = InputRequest::InsertChar('d');
11-
//! let resp = input.handle(req);
12-
//!
13-
//! assert_eq!(resp, Some(StateChanged { value: true, cursor: true }));
14-
//! assert_eq!(input.cursor(), 11);
15-
//! assert_eq!(input.to_string(), "Hello World");
16-
//! ```
17-
//!
18-
//! See other examples in the [GitHub repo](https://github.com/sayanarijit/tui-input/tree/main/examples).
3+
//! See examples in the [GitHub repo](https://github.com/sayanarijit/tui-input/tree/main/examples).
194
205
mod input;
216

0 commit comments

Comments
 (0)