#!/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_exampleA command palette's state can run without a window or terminal.
command_palette_example;
import (package) stdstd.(module) std.stdioCategory 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:
The lowest layer is the operating system layer. The two main schemes are Windows and Posix.
C's stdio.h which unifies the two operating system schemes.
std.stdio, this module, unifies the various stdio.h implementations into
a high level package for D programs.
Source
std/stdio.d
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);
}
}
writeln;
import (package) sparklessparkles.(package) sparkles.uiui.(module) sparkles.ui.command_paletteDisplay-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.PaletteEffectKindAn 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.PaletteInvocationThe originating surface and its focus identity, owned and validated by the host.
PaletteInvocation, (enum) sparkles.ui.command_palette.PaletteStatusFailure leaves the last usable palette state intact.
PaletteStatus;
enum (enum) command_palette_example.CommandCommand { (enum value) command_palette_example.Command.none = 0none, (enum value) command_palette_example.Command.save = 1save, (enum value) command_palette_example.Command.open = 2open }
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.CommandCommand;
struct (struct) command_palette_example.ContextContext
{
bool bool command_palette_example.Context.available(sparkles.ui.command_palette.PaletteCommand!(Command) command, ulong target) const pure nothrow @nogc @safeavailable((alias) command_palette_example.Id = sparkles.ui.command_palette.PaletteCommand!(Command)Id (parameter) sparkles.ui.command_palette.PaletteCommand!(Command) commandcommand, ulong (parameter) ulong targettarget) const @safe pure nothrow @nogc
=> (parameter) ulong targettarget == 42;
}
void void D main() @safemain() @safe
{
CommandPalette!((enum) command_palette_example.CommandCommand, 4) (local variable) _error_ palettepalette;
(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.CommandCommand[2] (local variable) sparkles.ui.command_palette.PaletteEntry!(Command)[2] commandscommands = [
(template instance) sparkles.ui.command_palette.PaletteEntry!(command_palette_example.Command)PaletteEntry!(enum) command_palette_example.CommandCommand((struct) sparkles.ui.command_palette.PaletteCommand!(Command)Id((enum) command_palette_example.CommandCommand.(enum value) command_palette_example.Command.save = 1save), "Save document", "File"),
(template instance) sparkles.ui.command_palette.PaletteEntry!(command_palette_example.Command)PaletteEntry!(enum) command_palette_example.CommandCommand((struct) sparkles.ui.command_palette.PaletteCommand!(Command)Id((enum) command_palette_example.CommandCommand.(enum value) command_palette_example.Command.open = 2open), "Open file", "File"),
];
const (local variable) const(_error_) installedinstalled = palette.catalog(commands[]);
assert(installed == (enum) sparkles.ui.command_palette.PaletteStatusFailure leaves the last usable palette state intact.
PaletteStatus.ok);
const (local variable) const(_error_) openingopening = palette.open(PaletteInvocation(42, 17));
const (local variable) const(_error_) searchedsearched = palette.search(opening.ticket, "save", Context.init);
assert(searched == (enum) sparkles.ui.command_palette.PaletteStatusFailure leaves the last usable palette state intact.
PaletteStatus.ok);
const (local variable) const(_error_) effecteffect = palette.accept(opening.ticket, Context.init);
assert(effect.kind == (enum) sparkles.ui.command_palette.PaletteEffectKindAn 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.commandcommand.operation, " on target ", effect.invocation.(field) _error_ effect.invocation.targettarget);
assert(palette.accept(opening.ticket, Context.init).kind == (enum) sparkles.ui.command_palette.PaletteEffectKindAn observable transition; commands are effects, never called by the palette.
PaletteEffectKind.(enum value) sparkles.ui.command_palette.PaletteEffectKind.none = cast(ubyte)0unone);
}