ConsoleForge 0.3.0
Elm-architecture TUI framework for .NET 8
Loading...
Searching...
No Matches
ConsoleForge.Core.IComponent Interface Reference

A self-contained sub-program: owns its own state, update logic, and view. More...

Inheritance diagram for ConsoleForge.Core.IComponent:

Additional Inherited Members

Public Member Functions inherited from ConsoleForge.Core.IModel
ICmdInit ()
 Called once at program start.
IWidget View ()
 Produce the root widget for the current model state.
Public Attributes inherited from ConsoleForge.Core.IModel
IModel Model
 Pure update function.

Detailed Description

A self-contained sub-program: owns its own state, update logic, and view.

Implements IModel so it can be run standalone via Program.Run or embedded inside a parent model.

Use IComponent<TResult> when the component needs to signal completion and return a typed result to its parent.

Pattern — define one sealed record per logical screen or reusable interactive widget, implement IComponent, and embed it as a typed field in the parent model:

public sealed record MyPage : IComponent
{
static readonly KeyMap Keys = new KeyMap()
.On(ConsoleKey.Escape, () => new QuitMsg());
public int Counter { get; init; }
public ICmd? Init() => null;
public (IModel Model, ICmd? Cmd) Update(IMsg msg)
{
if (Keys.Handle(msg) is { } action) msg = action;
return msg switch
{
NavUpMsg => (this with { Counter = Counter + 1 }, null),
_ => (this, null),
};
}
public IWidget View() => new TextBlock($"Count: {Counter}");
}
// In the parent model:
record AppModel(MyPage Page, ...) : IModel
{
public (IModel Model, ICmd? Cmd) Update(IMsg msg)
{
var (next, cmd) = Component.Delegate(Page, msg);
return (this with { Page = next }, cmd);
}
}
Factory for creating common command values.
Definition Cmd.cs:5
Declarative input binding map.
Definition KeyMap.cs:28
KeyMap On(ConsoleKey key, Func< IMsg > handler)
Bind a key (any modifiers) to a message factory.
Definition KeyMap.cs:35
A self-contained sub-program: owns its own state, update logic, and view.
Definition IComponent.cs:53
The root interface for all ConsoleForge application models.
Definition IModel.cs:10
IModel Model
Pure update function.
Definition IModel.cs:23
IWidget View()
Produce the root widget for the current model state.
ICmd? Init()
Called once at program start.
Marker interface for all messages flowing through the event loop.
Definition IMsg.cs:7
record QuitMsg
Signals the program loop to exit cleanly.
Definition Messages.cs:6
delegate Task< IMsg > ICmd()
A command: an async function that produces one message when complete.

The documentation for this interface was generated from the following file:
  • src/ConsoleForge/Core/IComponent.cs