Skip to content

feat: Add support for masked password prompt#355

Merged
djc merged 1 commit into
console-rs:mainfrom
TomPlanche:feat/masked-password
Jul 8, 2026
Merged

feat: Add support for masked password prompt#355
djc merged 1 commit into
console-rs:mainfrom
TomPlanche:feat/masked-password

Conversation

@TomPlanche

Copy link
Copy Markdown
Contributor

Summary

Adds an optional mask character to the Password prompt. By default the input stays fully hidden (nothing echoed) exactly as before; when a mask is set, one mask character is echoed per typed character.

Resolves #340.

let password = Password::new()
    .with_prompt("Enter password")
    .with_mask('*') // if not set, input stays fully hidden
    .interact()?;

Changes

  • New with_mask(char) builder method on Password, backed by a mask: Option<char> field (None by default).
  • When a mask is set, input is read key-by-key via Term::read_key() and a mask character is echoed for each printable character. Backspace erases the last character; Enter submits. This path mirrors read_secure_line's trailing-newline behaviour so both code paths behave uniformly.
  • When no mask is set, behaviour is unchanged (read_secure_line).
  • Extended examples/password.rs to demonstrate the masked prompt.

Notes

  • Non-breaking: purely additive. The new Option<char> field defaults to None, preserving the current fully-hidden behaviour.
  • Zeroize: read_masked_line returns a String and the caller wraps it in Zeroizing in interact_on, identical to the existing read_secure_line path, so the existing memory-scrubbing guarantee is unchanged.

Comment thread examples/password.rs Outdated
Comment thread src/prompts/password.rs Outdated
Comment thread src/prompts/password.rs Outdated

@djc djc left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks okay modulo one nit. Please squash your commits.

Comment thread src/prompts/password.rs Outdated
@TomPlanche
TomPlanche force-pushed the feat/masked-password branch from 84fee0a to 63e0082 Compare July 8, 2026 10:37
@TomPlanche
TomPlanche force-pushed the feat/masked-password branch from 63e0082 to 53ca34e Compare July 8, 2026 10:39
@djc
djc merged commit 63a2b5a into console-rs:main Jul 8, 2026
9 checks passed
@djc

djc commented Jul 8, 2026

Copy link
Copy Markdown
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: masking password

2 participants