ratatui_ruby 1.2.1 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/ratatui_ruby/Cargo.lock +2 -1
- data/ext/ratatui_ruby/Cargo.toml +2 -1
- data/ext/ratatui_ruby/src/events.rs +157 -18
- data/lib/ratatui_ruby/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 765a191883719b12dc911fd0279af0812275ab4abeec564667fee00d413f9dd0
|
|
4
|
+
data.tar.gz: bd58e3e32d9e90f3e3b6c51941900b0f787f056c75051441bf8a663c906c2563
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0a556ffb8388ef0d36e5f0223f25c4e7a97327ac7ebcad2555c0fb605ab5320df795a98ea1646ba1656cbb31d02cc42a246aaa6276894b476140ef36272842d6
|
|
7
|
+
data.tar.gz: 2c0072dc2a5b68ea732ff652617ba583df19f23c3256b6bd4e68abbd7a7b8bbe10096bc2249212236a306d3cfdd9f5862281b90c86968c9df3f4d0dc2dd42958
|
data/ext/ratatui_ruby/Cargo.lock
CHANGED
|
@@ -1059,12 +1059,13 @@ dependencies = [
|
|
|
1059
1059
|
|
|
1060
1060
|
[[package]]
|
|
1061
1061
|
name = "ratatui_ruby"
|
|
1062
|
-
version = "1.2.
|
|
1062
|
+
version = "1.2.2"
|
|
1063
1063
|
dependencies = [
|
|
1064
1064
|
"bumpalo",
|
|
1065
1065
|
"lazy_static",
|
|
1066
1066
|
"magnus",
|
|
1067
1067
|
"ratatui",
|
|
1068
|
+
"rb-sys",
|
|
1068
1069
|
"time",
|
|
1069
1070
|
"unicode-width 0.1.14",
|
|
1070
1071
|
]
|
data/ext/ratatui_ruby/Cargo.toml
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
[package]
|
|
5
5
|
name = "ratatui_ruby"
|
|
6
|
-
version = "1.2.
|
|
6
|
+
version = "1.2.2"
|
|
7
7
|
edition = "2021"
|
|
8
8
|
|
|
9
9
|
[lib]
|
|
@@ -11,6 +11,7 @@ crate-type = ["cdylib", "staticlib"]
|
|
|
11
11
|
|
|
12
12
|
[dependencies]
|
|
13
13
|
magnus = "0.8.2"
|
|
14
|
+
rb-sys = "*"
|
|
14
15
|
ratatui = { version = "0.30", features = ["widget-calendar", "layout-cache", "unstable-rendered-line-info", "palette"] }
|
|
15
16
|
unicode-width = "0.1"
|
|
16
17
|
|
|
@@ -2,7 +2,9 @@
|
|
|
2
2
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
3
3
|
|
|
4
4
|
use magnus::{Error, IntoValue, TryConvert, Value};
|
|
5
|
+
use rb_sys::rb_thread_call_without_gvl;
|
|
5
6
|
use std::cell::RefCell;
|
|
7
|
+
use std::os::raw::c_void;
|
|
6
8
|
|
|
7
9
|
/// Wrapper enum for test events - includes crossterm events and our Sync event.
|
|
8
10
|
#[derive(Debug, Clone)]
|
|
@@ -314,6 +316,92 @@ pub fn clear_events() {
|
|
|
314
316
|
EVENT_QUEUE.with(|q| q.borrow_mut().clear());
|
|
315
317
|
}
|
|
316
318
|
|
|
319
|
+
/// Result of polling crossterm from outside the GVL.
|
|
320
|
+
///
|
|
321
|
+
/// The blocking I/O (poll + read) happens without the Ruby GVL held,
|
|
322
|
+
/// so other Ruby threads can run concurrently. This enum carries the
|
|
323
|
+
/// result back across the GVL boundary for Ruby object construction.
|
|
324
|
+
#[derive(Debug)]
|
|
325
|
+
enum PollResult {
|
|
326
|
+
/// A crossterm event was read successfully.
|
|
327
|
+
Event(ratatui::crossterm::event::Event),
|
|
328
|
+
/// The timeout expired with no event available.
|
|
329
|
+
NoEvent,
|
|
330
|
+
/// An I/O error occurred during poll or read.
|
|
331
|
+
Error(String),
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/// Data passed to the GVL-free polling callback.
|
|
335
|
+
///
|
|
336
|
+
/// The `timeout` field controls the poll behavior:
|
|
337
|
+
/// - `Some(duration)`: poll with a timeout, return `NoEvent` if it expires.
|
|
338
|
+
/// - `None`: block indefinitely until an event arrives.
|
|
339
|
+
struct PollData {
|
|
340
|
+
timeout: Option<std::time::Duration>,
|
|
341
|
+
result: PollResult,
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/// Polls crossterm for events without the Ruby GVL held.
|
|
345
|
+
///
|
|
346
|
+
/// This is the work function passed to `rb_thread_call_without_gvl`.
|
|
347
|
+
/// It performs blocking I/O that would otherwise starve Ruby threads.
|
|
348
|
+
extern "C" fn poll_without_gvl(data: *mut c_void) -> *mut c_void {
|
|
349
|
+
// SAFETY: `data` is a valid pointer to `PollData`, passed by
|
|
350
|
+
// `poll_crossterm_without_gvl` via `rb_thread_call_without_gvl`. The
|
|
351
|
+
// pointer remains valid for the duration of this callback because the
|
|
352
|
+
// caller owns the `PollData` on the stack and waits for us to return.
|
|
353
|
+
let data = unsafe { &mut *data.cast::<PollData>() };
|
|
354
|
+
|
|
355
|
+
data.result = match data.timeout {
|
|
356
|
+
Some(duration) => match ratatui::crossterm::event::poll(duration) {
|
|
357
|
+
Ok(true) => match ratatui::crossterm::event::read() {
|
|
358
|
+
Ok(event) => PollResult::Event(event),
|
|
359
|
+
Err(e) => PollResult::Error(e.to_string()),
|
|
360
|
+
},
|
|
361
|
+
Ok(false) => PollResult::NoEvent,
|
|
362
|
+
Err(e) => PollResult::Error(e.to_string()),
|
|
363
|
+
},
|
|
364
|
+
None => match ratatui::crossterm::event::read() {
|
|
365
|
+
Ok(event) => PollResult::Event(event),
|
|
366
|
+
Err(e) => PollResult::Error(e.to_string()),
|
|
367
|
+
},
|
|
368
|
+
};
|
|
369
|
+
|
|
370
|
+
std::ptr::null_mut()
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/// Polls crossterm with the GVL released, then converts the result
|
|
374
|
+
/// to a Ruby value after reacquiring the GVL.
|
|
375
|
+
fn poll_crossterm_without_gvl(
|
|
376
|
+
ruby: &magnus::Ruby,
|
|
377
|
+
timeout: Option<std::time::Duration>,
|
|
378
|
+
) -> Result<Value, Error> {
|
|
379
|
+
let mut data = PollData {
|
|
380
|
+
timeout,
|
|
381
|
+
result: PollResult::NoEvent,
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
// SAFETY: `data` is a valid stack-local `PollData` whose lifetime
|
|
385
|
+
// spans this entire call. `poll_without_gvl` only reads/writes
|
|
386
|
+
// through the pointer while `rb_thread_call_without_gvl` blocks,
|
|
387
|
+
// and we don't access `data` again until that function returns.
|
|
388
|
+
unsafe {
|
|
389
|
+
rb_thread_call_without_gvl(
|
|
390
|
+
Some(poll_without_gvl),
|
|
391
|
+
(&raw mut data).cast::<c_void>(),
|
|
392
|
+
None,
|
|
393
|
+
std::ptr::null_mut(),
|
|
394
|
+
);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
// GVL is now re-held — safe to create Ruby objects.
|
|
398
|
+
match data.result {
|
|
399
|
+
PollResult::Event(event) => handle_crossterm_event(event),
|
|
400
|
+
PollResult::NoEvent => Ok(ruby.qnil().into_value_with(ruby)),
|
|
401
|
+
PollResult::Error(msg) => Err(Error::new(ruby.exception_runtime_error(), msg)),
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
|
|
317
405
|
pub fn poll_event(ruby: &magnus::Ruby, timeout_val: Option<f64>) -> Result<Value, Error> {
|
|
318
406
|
let event = EVENT_QUEUE.with(|q| {
|
|
319
407
|
let mut queue = q.borrow_mut();
|
|
@@ -334,24 +422,8 @@ pub fn poll_event(ruby: &magnus::Ruby, timeout_val: Option<f64>) -> Result<Value
|
|
|
334
422
|
return Ok(ruby.qnil().into_value_with(ruby));
|
|
335
423
|
}
|
|
336
424
|
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
let duration = std::time::Duration::from_secs_f64(secs);
|
|
340
|
-
if ratatui::crossterm::event::poll(duration)
|
|
341
|
-
.map_err(|e| Error::new(ruby.exception_runtime_error(), e.to_string()))?
|
|
342
|
-
{
|
|
343
|
-
let event = ratatui::crossterm::event::read()
|
|
344
|
-
.map_err(|e| Error::new(ruby.exception_runtime_error(), e.to_string()))?;
|
|
345
|
-
handle_crossterm_event(event)
|
|
346
|
-
} else {
|
|
347
|
-
Ok(ruby.qnil().into_value_with(ruby))
|
|
348
|
-
}
|
|
349
|
-
} else {
|
|
350
|
-
// Blocking: wait indefinitely for an event
|
|
351
|
-
let event = ratatui::crossterm::event::read()
|
|
352
|
-
.map_err(|e| Error::new(ruby.exception_runtime_error(), e.to_string()))?;
|
|
353
|
-
handle_crossterm_event(event)
|
|
354
|
-
}
|
|
425
|
+
let timeout = timeout_val.map(std::time::Duration::from_secs_f64);
|
|
426
|
+
poll_crossterm_without_gvl(ruby, timeout)
|
|
355
427
|
}
|
|
356
428
|
|
|
357
429
|
fn handle_test_event(event: TestEvent) -> Result<Value, Error> {
|
|
@@ -559,3 +631,70 @@ fn handle_focus_event(event_type: &str) -> Result<Value, Error> {
|
|
|
559
631
|
hash.aset(ruby.to_symbol("type"), ruby.to_symbol(event_type))?;
|
|
560
632
|
Ok(hash.into_value_with(&ruby))
|
|
561
633
|
}
|
|
634
|
+
|
|
635
|
+
#[cfg(test)]
|
|
636
|
+
mod tests {
|
|
637
|
+
use super::*;
|
|
638
|
+
|
|
639
|
+
#[test]
|
|
640
|
+
fn poll_data_defaults_to_no_event() {
|
|
641
|
+
let data = PollData {
|
|
642
|
+
timeout: Some(std::time::Duration::from_millis(0)),
|
|
643
|
+
result: PollResult::NoEvent,
|
|
644
|
+
};
|
|
645
|
+
assert!(matches!(data.result, PollResult::NoEvent));
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
#[test]
|
|
649
|
+
fn poll_data_accepts_none_timeout_for_indefinite_blocking() {
|
|
650
|
+
let data = PollData {
|
|
651
|
+
timeout: None,
|
|
652
|
+
result: PollResult::NoEvent,
|
|
653
|
+
};
|
|
654
|
+
assert!(data.timeout.is_none());
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
#[test]
|
|
658
|
+
fn poll_result_error_preserves_message() {
|
|
659
|
+
let result = PollResult::Error("connection reset".to_string());
|
|
660
|
+
match result {
|
|
661
|
+
PollResult::Error(msg) => assert_eq!(msg, "connection reset"),
|
|
662
|
+
_ => panic!("Expected PollResult::Error"),
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
#[test]
|
|
667
|
+
fn poll_result_event_wraps_crossterm_event() {
|
|
668
|
+
let key_event =
|
|
669
|
+
ratatui::crossterm::event::Event::Key(ratatui::crossterm::event::KeyEvent::new(
|
|
670
|
+
ratatui::crossterm::event::KeyCode::Char('q'),
|
|
671
|
+
ratatui::crossterm::event::KeyModifiers::empty(),
|
|
672
|
+
));
|
|
673
|
+
let result = PollResult::Event(key_event.clone());
|
|
674
|
+
match result {
|
|
675
|
+
PollResult::Event(e) => assert_eq!(format!("{e:?}"), format!("{key_event:?}")),
|
|
676
|
+
_ => panic!("Expected PollResult::Event"),
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
#[test]
|
|
681
|
+
fn poll_without_gvl_returns_no_event_on_zero_timeout() {
|
|
682
|
+
// With a zero-duration timeout, poll returns immediately with no event.
|
|
683
|
+
// In headless environments (CI), crossterm may return an Error because
|
|
684
|
+
// there is no terminal to read from — that's also a valid outcome.
|
|
685
|
+
let mut data = PollData {
|
|
686
|
+
timeout: Some(std::time::Duration::from_millis(0)),
|
|
687
|
+
result: PollResult::Error("sentinel — should be overwritten".to_string()),
|
|
688
|
+
};
|
|
689
|
+
|
|
690
|
+
poll_without_gvl(&mut data as *mut PollData as *mut c_void);
|
|
691
|
+
|
|
692
|
+
// The callback must have overwritten the sentinel value.
|
|
693
|
+
match &data.result {
|
|
694
|
+
PollResult::Error(msg) if msg == "sentinel — should be overwritten" => {
|
|
695
|
+
panic!("poll_without_gvl did not write to data.result")
|
|
696
|
+
}
|
|
697
|
+
_ => {} // NoEvent, Event, or a *different* Error are all fine.
|
|
698
|
+
}
|
|
699
|
+
}
|
|
700
|
+
}
|
data/lib/ratatui_ruby/version.rb
CHANGED