/**
* Imperative Commands for OpenTUI renderables and the terminal session:
* `focus`, `blur`, `scrollTo`, `scrollBy`, `measure`, `quit`,
* `writeClipboardText`, `readClipboardText`, and `clearClipboard`.
*
* Commands run through `OpenTui.Host` and `Renderer.Refs`, both framework
* services, so issuing them adds no requirements to a component.
*
* @stability experimental
* @since 4.2.0
*/
import type * as OT from "@opentui/core"
import type * as Context from "effect/Context"
import * as Effect from "effect/Effect"
import * as Option from "effect/Option"
import * as Schema from "effect/Schema"
import * as Command from "effect/ui/Command"
import * as Component from "effect/ui/Component"
import * as Renderer from "effect/ui/Renderer"
import * as OpenTui from "./OpenTui.ts"
/**
* Result of ref Commands that perform an action: whether the ref was rendered.
*
* @stability experimental
* @category models
* @since 4.2.0
*/
export interface Found {
readonly found: boolean
}
const ScrollPositionSchema = Schema.Union([
Schema.Number,
Schema.Struct({ x: Schema.Number, y: Schema.Number })
])
const ClipboardOptionsSchema = Schema.Struct({
selection: Schema.optional(Schema.Literals(["clipboard", "primary"]))
})
const failed = { status: "failed" } as const
const textTypes: readonly [string] = ["text/plain"]
const useClipboard = (
options: OpenTui.ClipboardOptions | undefined,
f: (
clipboard: OT.ClipboardService,
request: {
readonly selection: OpenTui.ClipboardSelection
readonly signal: AbortSignal
}
) => Promise
): Effect.Effect =>
OpenTui.Host.use((host) =>
Effect.promise(async (signal) => {
try {
return await f(host.clipboardService(), {
selection: options?.selection ?? "clipboard",
signal
})
} catch {
return failed
}
})
)
const writeStatus = (
result: OT.ClipboardWriteResult | OT.ClipboardClearResult,
done: Done
): { readonly status: Done | "sent" | "unsupported" | "failed" } => {
const host = result.host.status
if (host === done) return { status: done }
if (result.terminal.status === "attempted") return { status: "sent" }
return result.terminal.status !== "local-failure" &&
(host === "unsupported" || host === "not-attempted")
? { status: "unsupported" }
: failed
}
const withNode = (
ref: Renderer.Ref,
f: (host: Context.Service.Shape, node: N) => A
): Effect.Effect, never, Renderer.Refs | OpenTui.Host> =>
Renderer.Refs.use((refs) =>
Effect.flatMap(refs.lookup(ref), (node) =>
Option.isNone(node)
? Effect.succeed(Option.none())
: OpenTui.Host.useSync((host) => Option.some(f(host, node.value))))
)
const found = (
ref: Renderer.Ref,
f: (host: Context.Service.Shape, node: N) => void
): Effect.Effect =>
Effect.map(
withNode(ref, f),
(done): Found => ({ found: Option.isSome(done) })
)
/**
* The Command definition behind {@link focus}.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const Focus: Command.Factory<
{ readonly ref: Renderer.Ref },
Found
> = Command.make(
"OpenTuiCommand.focus",
{ args: { ref: Renderer.Ref } },
({ ref }) => found(ref, (host, node) => host.focus(node as OT.Renderable))
)
/**
* The Command definition behind {@link blur}.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const Blur: Command.Factory<{ readonly ref: Renderer.Ref }, Found> = Command.make(
"OpenTuiCommand.blur",
{ args: { ref: Renderer.Ref } },
({ ref }) => found(ref, (host, node) => host.blur(node as OT.Renderable))
)
/**
* The Command definition behind {@link scrollTo}.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const ScrollTo: Command.Factory<
{
readonly ref: Renderer.Ref
readonly position: OpenTui.ScrollPosition
},
Found
> = Command.make(
"OpenTuiCommand.scrollTo",
{ args: { ref: Renderer.Ref, position: ScrollPositionSchema } },
({ position, ref }) =>
found(
ref,
(host, node) => host.scrollTo(node as OT.ScrollBoxRenderable, position)
)
)
/**
* The Command definition behind {@link scrollBy}.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const ScrollBy: Command.Factory<
{
readonly ref: Renderer.Ref
readonly delta: OpenTui.ScrollPosition
},
Found
> = Command.make(
"OpenTuiCommand.scrollBy",
{ args: { ref: Renderer.Ref, delta: ScrollPositionSchema } },
({ delta, ref }) =>
found(
ref,
(host, node) => host.scrollBy(node as OT.ScrollBoxRenderable, delta)
)
)
/**
* The Command definition behind {@link measure}.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const Measure: Command.Factory<
{ readonly ref: Renderer.Ref },
Option.Option
> = Command.make(
"OpenTuiCommand.measure",
{ args: { ref: Renderer.Ref } },
({ ref }) => withNode(ref, (host, node) => host.measure(node as OT.Renderable))
)
/**
* The Command definition behind {@link quit}.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const Quit: Command.Factory<{}, Component.Noop> = Command.make(
"OpenTuiCommand.quit",
{ args: {} },
() =>
OpenTui.Host.useSync((host) => {
host.quit()
return Component.noop
})
)
/**
* The Command definition behind {@link writeClipboardText}.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const WriteClipboardText: Command.Factory<
{
readonly text: string
readonly options?: OpenTui.ClipboardOptions | undefined
},
OpenTui.ClipboardWrite
> = Command.make(
"OpenTuiCommand.writeClipboardText",
{
args: {
text: Schema.String,
options: Schema.optional(ClipboardOptionsSchema)
}
},
({ options, text }) =>
useClipboard(options, async (clipboard, request) =>
writeStatus(
await clipboard.writeText(text, {
...request,
destination: "best-available"
}),
"written"
))
)
/**
* The Command definition behind {@link readClipboardText}.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const ReadClipboardText: Command.Factory<
{ readonly options?: OpenTui.ClipboardOptions | undefined },
OpenTui.ClipboardRead
> = Command.make(
"OpenTuiCommand.readClipboardText",
{ args: { options: Schema.optional(ClipboardOptionsSchema) } },
({ options }) =>
useClipboard(options, async (clipboard, request) => {
const result = await clipboard.read({
...request,
preferredTypes: textTypes
})
return result.status === "read"
? {
status: "read",
text: new TextDecoder().decode(result.representation.bytes)
} as const
: result.status === "empty" || result.status === "unsupported"
? { status: result.status }
: failed
})
)
/**
* The Command definition behind {@link clearClipboard}.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const ClearClipboard: Command.Factory<
{ readonly options?: OpenTui.ClipboardOptions | undefined },
OpenTui.ClipboardClear
> = Command.make(
"OpenTuiCommand.clearClipboard",
{ args: { options: Schema.optional(ClipboardOptionsSchema) } },
({ options }) =>
useClipboard(options, async (clipboard, request) =>
writeStatus(
await clipboard.clear({ ...request, destination: "best-available" }),
"cleared"
))
)
/**
* Focuses the renderable named by `ref` after commit. A hidden one, or one
* under a hidden renderable, such as on a covered `RouterStack.Screens`
* screen, stays unfocused.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const focus = (
ref: Renderer.Ref,
f: (result: Found) => Msg
): Command.Mapped, Msg> => Command.map(Focus.make({ ref }), f)
/**
* Blurs the renderable named by `ref` after commit.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const blur = (
ref: Renderer.Ref,
f: (result: Found) => Msg
): Command.Mapped, Msg> => Command.map(Blur.make({ ref }), f)
/**
* Scrolls the `scrollbox` named by `ref` to `position`.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const scrollTo = (
ref: Renderer.Ref,
position: OpenTui.ScrollPosition,
f: (result: Found) => Msg
): Command.Mapped, Msg> =>
Command.map(ScrollTo.make({ ref, position }), f)
/**
* Scrolls the `scrollbox` named by `ref` by `delta`.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const scrollBy = (
ref: Renderer.Ref,
delta: OpenTui.ScrollPosition,
f: (result: Found) => Msg
): Command.Mapped, Msg> => Command.map(ScrollBy.make({ ref, delta }), f)
/**
* Measures the layout bounds of the renderable named by `ref`.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const measure = (
ref: Renderer.Ref,
f: (rect: Option.Option) => Msg
): Command.Mapped<
Command.Issued<
Option.Option,
never,
Option.Option,
"Effect"
>,
Msg
> => Command.map(Measure.make({ ref }), f)
/**
* Destroys the active OpenTUI renderer, ending `OpenTuiHost.run`.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const quit = (): Command.Issued<
Component.Noop,
never,
Component.Noop,
"Effect"
> => Quit.make({})
/**
* Copies `text` to the clipboard.
*
* **Details**
*
* Locally the text goes to the host clipboard, falling back to the terminal
* over OSC 52. Over SSH it goes to the terminal, so it reaches the user's
* machine.
*
* **Example** (Copying a URL)
*
* ```ts
* import { OpenTuiCommand } from "@effect/ui-opentui"
*
* const command = OpenTuiCommand.writeClipboardText(
* "https://effect.website",
* ({ status }) => ({ _tag: "CompletedCopy", status }) as const
* )
* ```
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const writeClipboardText = (
text: string,
f: (result: OpenTui.ClipboardWrite) => Msg,
options?: OpenTui.ClipboardOptions
): Command.Mapped<
Command.Issued<
OpenTui.ClipboardWrite,
never,
OpenTui.ClipboardWrite,
"Effect"
>,
Msg
> =>
Command.map(
WriteClipboardText.make(
options === undefined ? { text } : { text, options }
),
f
)
/**
* Reads text from the clipboard of the machine running the app, so over SSH
* this is the server's clipboard, not the user's.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const readClipboardText = (
f: (result: OpenTui.ClipboardRead) => Msg,
options?: OpenTui.ClipboardOptions
): Command.Mapped<
Command.Issued<
OpenTui.ClipboardRead,
never,
OpenTui.ClipboardRead,
"Effect"
>,
Msg
> =>
Command.map(
ReadClipboardText.make(options === undefined ? {} : { options }),
f
)
/**
* Clears the clipboard, choosing the destination as {@link writeClipboardText}
* does.
*
* @stability experimental
* @category commands
* @since 4.2.0
*/
export const clearClipboard = (
f: (result: OpenTui.ClipboardClear) => Msg,
options?: OpenTui.ClipboardOptions
): Command.Mapped<
Command.Issued<
OpenTui.ClipboardClear,
never,
OpenTui.ClipboardClear,
"Effect"
>,
Msg
> => Command.map(ClearClipboard.make(options === undefined ? {} : { options }), f)