Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
termion
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
akitsu-sanae
termion
Commits
17fdd508
Commit
17fdd508
authored
6 years ago
by
Josh Mcguigan
Browse files
Options
Downloads
Patches
Plain Diff
Added async_stdin_until function to be used in cursor_pos
parent
b3e0bbdf
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/async.rs
+25
-0
25 additions, 0 deletions
src/async.rs
src/cursor.rs
+4
-3
4 additions, 3 deletions
src/cursor.rs
with
29 additions
and
3 deletions
src/async.rs
+
25
−
0
View file @
17fdd508
use
std
::
io
::{
self
,
Read
};
use
std
::
sync
::
mpsc
;
use
std
::
thread
;
use
std
::
io
::
BufReader
;
use
std
::
io
::
BufRead
;
use
sys
::
tty
::
get_tty
;
/// Construct an asynchronous handle to the TTY standard input, with a delimiter byte.
///
/// This has the same advantages as async_stdin(), but also allows specifying a delimiter byte. The
/// reader will stop reading after consuming the delimiter byte.
pub
fn
async_stdin_until
(
delimiter
:
u8
)
->
AsyncReader
{
let
(
send
,
recv
)
=
mpsc
::
channel
();
thread
::
spawn
(
move
||
for
i
in
get_tty
()
.unwrap
()
.bytes
()
{
match
i
{
Ok
(
byte
)
=>
{
let
end_of_stream
=
&
byte
==
&
delimiter
;
let
send_error
=
send
.send
(
Ok
(
byte
))
.is_err
();
if
end_of_stream
||
send_error
{
return
;
}
},
Err
(
e
)
=>
{
return
;
}
}
});
AsyncReader
{
recv
:
recv
}
}
/// Construct an asynchronous handle to the TTY standard input.
///
/// This allows you to read from standard input _without blocking_ the current thread.
...
...
This diff is collapsed.
Click to expand it.
src/cursor.rs
+
4
−
3
View file @
17fdd508
...
...
@@ -2,7 +2,7 @@
use
std
::
fmt
;
use
std
::
io
::{
self
,
Write
,
Error
,
ErrorKind
,
Read
};
use
async
::
async_stdin
;
use
async
::
async_stdin
_until
;
use
std
::
time
::{
SystemTime
,
Duration
};
use
raw
::
CONTROL_SEQUENCE_TIMEOUT
;
...
...
@@ -94,7 +94,8 @@ pub trait DetectCursorPos {
impl
<
W
:
Write
>
DetectCursorPos
for
W
{
fn
cursor_pos
(
&
mut
self
)
->
io
::
Result
<
(
u16
,
u16
)
>
{
let
mut
stdin
=
async_stdin
();
let
delimiter
=
b'R'
;
let
mut
stdin
=
async_stdin_until
(
delimiter
);
// Where is the cursor?
// Use `ESC [ 6 n`.
...
...
@@ -108,7 +109,7 @@ impl<W: Write> DetectCursorPos for W {
let
now
=
SystemTime
::
now
();
// Either consume all data up to R or wait for a timeout.
while
buf
[
0
]
!=
b'R'
&&
now
.elapsed
()
.unwrap
()
<
timeout
{
while
buf
[
0
]
!=
delimiter
&&
now
.elapsed
()
.unwrap
()
<
timeout
{
if
stdin
.read
(
&
mut
buf
)
?
>
0
{
read_chars
.push
(
buf
[
0
]);
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment