Skip to content

Commit

Permalink
Insert trailing colons to avoid cursor jumps (#3)
Browse files Browse the repository at this point in the history
* Insert trailing colons when needed

* .

* rename
  • Loading branch information
Shingyx authored Mar 31, 2024
1 parent a543ff7 commit 13deaee
Showing 1 changed file with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,26 @@ class MacAddressTextWatcher : TextWatcher {
private fun formatMacAddress(text: String): String {
val builder = StringBuilder()
var digits = 0
var insertTrailingColon = false
for (char in text) {
if (char.isLetterOrDigit()) {
if (digits % 2 == 0 && digits > 0 && digits < 12) {
if (allowSeparator(digits)) {
builder.append(':')
insertTrailingColon = false
}
builder.append(char)
digits++
} else if (char == ':') {
insertTrailingColon = true
}
}
if (text.lastOrNull() == ':') {
if ((insertTrailingColon || text.lastOrNull() == ':') && allowSeparator(digits)) {
builder.append(':')
}
return builder.toString()
}

private fun allowSeparator(digits: Int): Boolean {
return digits % 2 == 0 && digits in 1 until 12
}
}

0 comments on commit 13deaee

Please sign in to comment.