command-palette.dhover×54all
#!/usr/bin/env dub
/+ dub.sdl:
name "shared-command-palette"
targetPath "build"
dependency "sparkles:ui" path="../../.."
buildType "checked" {
    buildOptions "optimize" "inline" "debugInfo"
}
+/
/** A command palette's state can run without a window or terminal. */
module 
(module) command_palette_example

A command palette's state can run without a window or terminal.

command_palette_example
;
import
(package) std
std
.
(module) std.stdio
Category Symbols
File handles _popen File isFileHandle openNetwork stderr stdin stdout
Reading chunks lines readf readfln readln
Writing toFile write writef writefln writeln
Misc KeepTerminator LockType StdioException

Standard I/O functions that extend core.stdc.stdio. core.stdc.stdio is publically imported when importing std.stdio.

There are three layers of I/O:

  1. The lowest layer is the operating system layer. The two main schemes are Windows and Posix.

  2. C's stdio.h which unifies the two operating system schemes.

  3. std.stdio, this module, unifies the various stdio.h implementations into a high level package for D programs.

Source

std/stdio.d

@copyrightCopyright The D Language Foundation 2007-.@licenseBoost License 1.0.@authorsWalter Bright, Andrei Alexandrescu, Alex Rønne Petersen
stdio
:
(alias template) command_palette_example.writeln = std.stdio.writeln(T...)(T args)

Equivalent to write(args, '\n'). Calling writeln without arguments is valid and just prints a newline to the standard output.

Example

Reads stdin and writes it to stdout with an argument counter.

import std.stdio;

void main()
{
    string line;

    for (size_t count = 0; (line = readln) !is null; count++)
    {
         writeln("Input ", count, ": ", line);
    }
}
@paramargs the items to write to stdout@throwsIn case of an I/O error, throws an StdioException.
writeln
;
import
(package) sparkles
sparkles
.
(package) sparkles.ui
ui
.
(module) sparkles.ui.command_palette

Display-independent command-palette behavior and effects.

command_palette
:
(alias struct) command_palette_example.CommandPalette = sparkles.ui.command_palette.CommandPalette(Cmd, ulong Capacity = 512, ulong QueryBytes = 256) if (is(Cmd == enum) && (Capacity > 0) && (QueryBytes > 0))

A host-independent palette. The host performs effects and validates focus.

CommandPalette
,
(alias struct) command_palette_example.PaletteCommand = sparkles.ui.command_palette.PaletteCommand(Cmd) if (is(Cmd == enum))

A command identity includes its argument, independently of its shortcuts.

PaletteCommand
,
(enum) sparkles.ui.command_palette.PaletteEffectKind

An observable transition; commands are effects, never called by the palette.

PaletteEffectKind
,
(alias struct) command_palette_example.PaletteEntry = sparkles.ui.command_palette.PaletteEntry(Cmd) if (is(Cmd == enum))

Immutable display metadata for a command, including commands without a binding.

PaletteEntry
,
(struct) sparkles.ui.command_palette.PaletteInvocation

The originating surface and its focus identity, owned and validated by the host.

PaletteInvocation
,
(enum) sparkles.ui.command_palette.PaletteStatus

Failure leaves the last usable palette state intact.

PaletteStatus
;
enum
(enum) command_palette_example.Command
Command
{
(enum value) command_palette_example.Command.none = 0
none
,
(enum value) command_palette_example.Command.save = 1
save
,
(enum value) command_palette_example.Command.open = 2
open
}
alias
(alias) command_palette_example.Id = sparkles.ui.command_palette.PaletteCommand!(Command)
Id
=
(struct) sparkles.ui.command_palette.PaletteCommand!(command_palette_example.Command)

A command identity includes its argument, independently of its shortcuts.

PaletteCommand
!
(enum) command_palette_example.Command
Command
;
struct
(struct) command_palette_example.Context
Context
{ bool
bool command_palette_example.Context.available(sparkles.ui.command_palette.PaletteCommand!(Command) command, ulong target) const pure nothrow @nogc @safe
available
(
(alias) command_palette_example.Id = sparkles.ui.command_palette.PaletteCommand!(Command)
Id
(parameter) sparkles.ui.command_palette.PaletteCommand!(Command) command
command
, ulong
(parameter) ulong target
target
) const @safe pure nothrow @nogc
=>
(parameter) ulong target
target
== 42;
} void
void D main() @safe
main
() @safe
{ CommandPalette!(
(enum) command_palette_example.Command
Command
, 4)
(local variable) _error_ palette
palette
;
(struct) sparkles.ui.command_palette.PaletteEntry!(command_palette_example.Command)

Immutable display metadata for a command, including commands without a binding.

PaletteEntry
!
(enum) command_palette_example.Command
Command
[2]
(local variable) sparkles.ui.command_palette.PaletteEntry!(Command)[2] commands
commands
= [
(template instance) sparkles.ui.command_palette.PaletteEntry!(command_palette_example.Command)
PaletteEntry
!
(enum) command_palette_example.Command
Command
(
(struct) sparkles.ui.command_palette.PaletteCommand!(Command)
Id
(
(enum) command_palette_example.Command
Command
.
(enum value) command_palette_example.Command.save = 1
save
), "Save document", "File"),
(template instance) sparkles.ui.command_palette.PaletteEntry!(command_palette_example.Command)
PaletteEntry
!
(enum) command_palette_example.Command
Command
(
(struct) sparkles.ui.command_palette.PaletteCommand!(Command)
Id
(
(enum) command_palette_example.Command
Command
.
(enum value) command_palette_example.Command.open = 2
open
), "Open file", "File"),
]; const
(local variable) const(_error_) installed
installed
= palette.catalog(commands[]);
assert(installed ==
(enum) sparkles.ui.command_palette.PaletteStatus

Failure leaves the last usable palette state intact.

PaletteStatus
.ok);
const
(local variable) const(_error_) opening
opening
= palette.open(PaletteInvocation(42, 17));
const
(local variable) const(_error_) searched
searched
= palette.search(opening.ticket, "save", Context.init);
assert(searched ==
(enum) sparkles.ui.command_palette.PaletteStatus

Failure leaves the last usable palette state intact.

PaletteStatus
.ok);
const
(local variable) const(_error_) effect
effect
= palette.accept(opening.ticket, Context.init);
assert(effect.kind ==
(enum) sparkles.ui.command_palette.PaletteEffectKind

An observable transition; commands are effects, never called by the palette.

PaletteEffectKind
.execute);
(template function) std.stdio.writeln(T...)(T args)
writeln
(effect.
(field) _error_ effect.command
command
.operation, " on target ", effect.invocation.
(field) _error_ effect.invocation.target
target
);
assert(palette.accept(opening.ticket, Context.init).kind ==
(enum) sparkles.ui.command_palette.PaletteEffectKind

An observable transition; commands are effects, never called by the palette.

PaletteEffectKind
.
(enum value) sparkles.ui.command_palette.PaletteEffectKind.none = cast(ubyte)0u
none
);
}