Skip to content

Commit

Permalink
Don't hide cursor if rendering cancellation
Browse files Browse the repository at this point in the history
  • Loading branch information
keynmol committed Nov 30, 2024
1 parent ed1f358 commit b1c9397
Show file tree
Hide file tree
Showing 9 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions modules/cats-effect/src/main/scala/PromptsIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ class PromptsIO private (
protected val terminal: Terminal,
protected val theme: Theme,
) extends AutoCloseable:
protected lazy val inputProvider = InputProvider(out)
protected lazy val inputProvider = InputProvider(terminal)

def io[A](
prompt: Prompt[A],
): IO[Completion[A]] =
val inputProvider = InputProvider(out)
val inputProvider = InputProvider(terminal)
val framework = prompt.framework(terminal, out, theme)

// TODO: provide native CE interface here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ private trait InputProviderPlatform:
): Future[Completion[Result]]

private trait InputProviderCompanionPlatform:
def apply(o: Output): InputProvider = InputProviderImpl(o)
def apply(o: Terminal): InputProvider = InputProviderImpl(o)

end InputProviderCompanionPlatform
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/InputProvider.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package cue4s
private[cue4s] abstract class Handler[Result]:
def apply(ev: Event): Next[Result]

private[cue4s] abstract class InputProvider(protected val output: Output)
private[cue4s] abstract class InputProvider(protected val terminal: Terminal)
extends AutoCloseable,
InputProviderPlatform

Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/PromptFramework.scala
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ private[cue4s] trait PromptFramework[Result](terminal: Terminal, out: Output):

final def printPrompt() =
import terminal.*
cursorHide()
if currentStatus() != Status.Canceled then cursorHide()
rendering.last match
case None =>
// initial print
Expand Down
2 changes: 1 addition & 1 deletion modules/core/src/main/scala/Prompts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Prompts private (
) extends AutoCloseable
with PromptsPlatform:

protected lazy val inputProvider = InputProvider(out)
protected lazy val inputProvider = InputProvider(terminal)

override def close(): Unit =
terminal.cursorShow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ trait InputProviderPlatform:
)(using ExecutionContext): Future[Completion[Result]]

trait InputProviderCompanionPlatform:
def apply(o: Output): InputProvider = InputProviderImpl(o)
def apply(o: Terminal): InputProvider = InputProviderImpl(o)

end InputProviderCompanionPlatform
3 changes: 2 additions & 1 deletion modules/core/src/main/scalajs/InputProviderImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import cue4s.CharCollector.decode

import scalajs.js

private class InputProviderImpl(o: Output)
private class InputProviderImpl(o: Terminal)
extends InputProvider(o),
InputProviderPlatform:
override def evaluateFuture[Result](
Expand Down Expand Up @@ -74,6 +74,7 @@ private class InputProviderImpl(o: Output)
def handle(key: Key) =
if key.name == "c" && key.ctrl then
handler(Event.Interrupt)
o.cursorShow()
close(Completion.Fail(CompletionError.Interrupted))
else
key.sequence
Expand Down
4 changes: 3 additions & 1 deletion modules/core/src/main/scalajvm/InputProviderImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import scala.util.boundary
import boundary.break
import CharCollector.*

private class InputProviderImpl(o: Output)
private class InputProviderImpl(o: Terminal)
extends InputProvider(o),
InputProviderPlatform:

Expand All @@ -39,6 +39,7 @@ private class InputProviderImpl(o: Output)

val hook = () =>
handler(Event.Interrupt)
o.cursorShow()
cancellation.complete(Success(Completion.interrupted))
()

Expand Down Expand Up @@ -77,6 +78,7 @@ private class InputProviderImpl(o: Output)
case Next.Error(msg) => break(Completion.error(msg))

hook = Some: () =>
o.cursorShow()
whatNext(handler(Event.Interrupt))

if !asyncHookSet then hook.foreach(InputProviderImpl.addShutdownHook)
Expand Down
4 changes: 3 additions & 1 deletion modules/core/src/main/scalanative/InputProviderImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import scalanative.posix.termios.*
import boundary.break
import CharCollector.*

private class InputProviderImpl(o: Output)
private class InputProviderImpl(o: Terminal)
extends InputProvider(o),
InputProviderPlatform:

Expand All @@ -37,6 +37,7 @@ private class InputProviderImpl(o: Output)
) =
val hook = () =>
handler(Event.Interrupt)
o.cursorShow()
close()

InputProviderImpl.addShutdownHook(hook)
Expand Down Expand Up @@ -79,6 +80,7 @@ private class InputProviderImpl(o: Output)

val hook = () =>
handler(Event.Interrupt)
o.cursorShow()
close()

if !asyncHookSet then InputProviderImpl.addShutdownHook(hook)
Expand Down

0 comments on commit b1c9397

Please sign in to comment.