diff options
| author | bptato <nincsnevem662@gmail.com> | 2022-07-31 09:36:35 +0200 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2022-07-31 10:55:40 +0200 |
| commit | b1c8e5edc277c84962955f1edbd8237adb2f5bc8 (patch) | |
| tree | b0d7a9101342d9469c0b8048dbbc00395c0df2fc /src | |
| parent | 8bc1392c463fbbfd99ae368b9e99dfcb97c52149 (diff) | |
| download | chawan-b1c8e5edc277c84962955f1edbd8237adb2f5bc8.tar.gz | |
Hide passwords while typing
Diffstat (limited to 'src')
| -rw-r--r-- | src/client.nim | 6 | ||||
| -rw-r--r-- | src/io/buffer.nim | 4 | ||||
| -rw-r--r-- | src/io/lineedit.nim | 27 |
3 files changed, 24 insertions, 13 deletions
diff --git a/src/client.nim b/src/client.nim index 480007f1..8cda16ec 100644 --- a/src/client.nim +++ b/src/client.nim @@ -345,7 +345,7 @@ proc isearch(client: Client) = if mark != nil: client.buffer.removeMark(my, mark) - let status = readLine("/", iput, client.buffer.width, {}, (proc(state: var LineState): bool = + let status = readLine("/", iput, client.buffer.width, {}, false, (proc(state: var LineState): bool = del_mark let regex = compileSearchRegex($state.news) client.buffer.cpos = cpos @@ -381,7 +381,7 @@ proc isearchBack(client: Client) = template del_mark() = if mark != nil: client.buffer.removeMark(my, mark) - let status = readLine("?", iput, client.buffer.width, {}, (proc(state: var LineState): bool = + let status = readLine("?", iput, client.buffer.width, {}, false, (proc(state: var LineState): bool = del_mark let regex = compileSearchRegex($state.news) client.buffer.cpos = cpos @@ -481,7 +481,7 @@ proc checkAuth(client: Client) = return client.statusMode() var password = "" - let pstatus = readLine("Password: ", password, client.buffer.width) + let pstatus = readLine("Password: ", password, client.buffer.width, hide = true) if not pstatus: client.needsauth = false return diff --git a/src/io/buffer.nim b/src/io/buffer.nim index 539e2f30..f627ba42 100644 --- a/src/io/buffer.nim +++ b/src/io/buffer.nim @@ -1159,7 +1159,7 @@ proc click*(buffer: Buffer): Option[ClickAction] = var value = input.value print(HVP(buffer.height + 1, 1)) print(EL()) - let status = readLine("TEXT: ", value, buffer.width, {'\r', '\n'}) + let status = readLine("TEXT: ", value, buffer.width, {'\r', '\n'}, input.inputType == INPUT_PASSWORD) if status: input.value = value input.invalid = true @@ -1215,7 +1215,7 @@ proc drawBuffer*(buffer: Buffer) = var outstr = "" #TODO TODO TODO renderhtml has broken format outputting #assert f.pos < line.str.width(), "fpos " & $f.pos & "\nstr" & line.str & "\n" - while x < f.pos: + while x < f.pos and i < line.str.len: #TODO x < f.pos should be enough var r: Rune fastRuneAt(line.str, i, r) outstr &= r diff --git a/src/io/lineedit.nim b/src/io/lineedit.nim index 64a1358c..5c756625 100644 --- a/src/io/lineedit.nim +++ b/src/io/lineedit.nim @@ -20,7 +20,8 @@ type LineState* = object maxlen: int displen: int disallowed: set[char] - callback: proc(state: var LineState): bool {.closure.} + hide: bool + callback: proc(state: var LineState): bool func lwidth(r: Rune): int = if r.isControlChar(): @@ -84,7 +85,10 @@ proc redraw(state: var LineState) = state.begin() let os = state.news.substr(state.shift, state.shift + state.displen) - printesc($os) + if state.hide: + printesc('*'.repeat(os.lwidth())) + else: + printesc($os) state.space(max(state.maxlen - state.minlen - os.lwidth(), 0)) state.begin() @@ -117,7 +121,10 @@ proc insertCharseq(state: var LineState, cs: var seq[Rune], disallowed: set[char if state.cursor >= state.news.len and state.news.lwidth(state.shift, state.cursor) + cs.lwidth() < state.displen: state.news &= cs state.cursor += cs.len - printesc($cs) + if state.hide: + printesc('*'.repeat(cs.lwidth())) + else: + printesc($cs) else: state.news.insert(cs, state.cursor) state.cursor += cs.len @@ -125,7 +132,10 @@ proc insertCharseq(state: var LineState, cs: var seq[Rune], disallowed: set[char proc readLine(state: var LineState): bool = printesc(state.prompt) - printesc(state.current) + if state.hide: + printesc('*'.repeat(state.current.lwidth())) + else: + printesc(state.current) while true: if not state.feedNext: @@ -259,8 +269,8 @@ proc readLine(state: var LineState): bool = state.feedNext = true proc readLine*(prompt: string, current: var string, termwidth: int, - disallowed: set[char], - callback: proc(state: var LineState): bool {.closure.}): bool = + disallowed: set[char], hide: bool, + callback: proc(state: var LineState): bool): bool = var state: LineState state.prompt = prompt @@ -272,6 +282,7 @@ proc readLine*(prompt: string, current: var string, termwidth: int, state.displen = state.maxlen - 1 state.disallowed = disallowed state.callback = callback + state.hide = hide if state.readLine(): current = $state.news @@ -279,5 +290,5 @@ proc readLine*(prompt: string, current: var string, termwidth: int, return false proc readLine*(prompt: string, current: var string, termwidth: int, - disallowed: set[char] = {}): bool = - readLine(prompt, current, termwidth, disallowed, (proc(state: var LineState): bool = false)) + disallowed: set[char] = {}, hide = false): bool = + readLine(prompt, current, termwidth, disallowed, hide, (proc(state: var LineState): bool = false)) |