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
10f66540
Commit
10f66540
authored
9 years ago
by
Ticki
Browse files
Options
Downloads
Patches
Plain Diff
Better password input
parent
a19d2e24
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/read.rs
+3
-2
3 additions, 2 deletions
examples/read.rs
src/error.rs
+3
-0
3 additions, 0 deletions
src/error.rs
src/extra.rs
+10
-17
10 additions, 17 deletions
src/extra.rs
with
16 additions
and
19 deletions
examples/read.rs
+
3
−
2
View file @
10f66540
...
...
@@ -6,14 +6,15 @@ use std::io::{Write, stdout, stdin};
fn
main
()
{
let
stdout
=
stdout
();
let
mut
stdout
=
stdout
.lock
();
let
mut
stdin
=
stdin
();
let
stdin
=
stdin
();
let
mut
stdin
=
stdin
.lock
();
stdout
.write
(
b"password: "
)
.unwrap
();
stdout
.flush
()
.unwrap
();
let
pass
=
stdin
.read_passwd
(
&
mut
stdout
);
if
let
Some
(
pass
)
=
pass
{
if
let
Ok
(
Some
(
pass
)
)
=
pass
{
stdout
.write
(
pass
.as_bytes
())
.unwrap
();
stdout
.write
(
b"
\n
"
)
.unwrap
();
}
else
{
...
...
This diff is collapsed.
Click to expand it.
src/error.rs
+
3
−
0
View file @
10f66540
...
...
@@ -12,6 +12,8 @@ pub enum TerminalError {
TermSizeError
,
/// Failed to write to stdout.
StdoutError
,
/// Failed to read from stdin.
StdinError
,
}
impl
TerminalError
{
...
...
@@ -21,6 +23,7 @@ impl TerminalError {
TerminalError
::
SetAttrError
=>
"Failed to set Terminal attribute."
,
TerminalError
::
TermSizeError
=>
"Failed to get terminal size."
,
TerminalError
::
StdoutError
=>
"Failed to write to stdout."
,
TerminalError
::
StdinError
=>
"Failed to read from stdin."
,
}
}
}
...
...
This diff is collapsed.
Click to expand it.
src/extra.rs
+
10
−
17
View file @
10f66540
use
std
::
io
::{
Read
,
Write
};
use
IntoRawMode
;
use
{
IntoRawMode
,
TerminalError
}
;
/// Extension to `Read` trait.
pub
trait
ReadExt
{
/// Read a password.
fn
read_passwd
<
W
:
Write
>
(
&
mut
self
,
writer
:
&
mut
W
)
->
Option
<
String
>
;
fn
read_passwd
<
W
:
Write
>
(
&
mut
self
,
writer
:
&
mut
W
)
->
Result
<
Option
<
String
>
,
TerminalError
>
;
}
impl
<
R
:
Read
>
ReadExt
for
R
{
fn
read_passwd
<
W
:
Write
>
(
&
mut
self
,
writer
:
&
mut
W
)
->
Option
<
String
>
{
let
_raw
=
if
let
Ok
(
x
)
=
writer
.into_raw_mode
()
{
x
}
else
{
return
None
;
};
fn
read_passwd
<
W
:
Write
>
(
&
mut
self
,
writer
:
&
mut
W
)
->
Result
<
Option
<
String
>
,
TerminalError
>
{
let
_raw
=
try
!
(
writer
.into_raw_mode
());
let
mut
string
=
String
::
with_capacity
(
30
);
for
c
in
self
.chars
()
{
match
if
let
Ok
(
c
)
=
c
{
c
}
else
{
return
None
;
}
{
'\x00'
|
'\x03'
|
'\x04'
=>
return
None
,
'\r'
=>
return
Some
(
string
),
b
=>
string
.push
(
b
),
match
c
{
Err
(
_
)
=>
return
Err
(
TerminalError
::
StdinError
),
Ok
(
'\0'
)
|
Ok
(
'\x03'
)
|
Ok
(
'\x04'
)
=>
return
Ok
(
None
),
Ok
(
'\n'
)
|
Ok
(
'\r'
)
=>
return
Ok
(
Some
(
string
)),
Ok
(
c
)
=>
string
.push
(
c
),
}
}
Some
(
string
)
Ok
(
Some
(
string
)
)
}
}
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