Files
inbuxa-installer/internal/tui/run.go
T
jcoffey-dev bdc54f11c8 The interface: inbuxa on its own, six screens, the plan before anything
`inbuxa` with no arguments (or `inbuxa install` with no options) opens the
installer spec's interface in the terminal: what it found on this machine,
the names, which components go here and how, what answers on 80 and 443,
the whole plan, and the install. It fills in the same plan.Options the
flags do, builds the plan with plan.Build and installs with apply.Run: it
is a way of choosing, not a second installer. The plan screen shows the
command that does the same without it, and nothing happens until a "y".

- Components: what the machine can't do is greyed out with the reason
  beside it; what it lacks and the installer can add (docker, compose) is
  offered and added, with --install-deps in the command. The defaults are
  the flags' defaults. Front ends without a server here point to join.
- Reach offers Caddy or your own web server. --proxy snippets is left out:
  the plan describes it, but nothing writes those files yet (now said in
  the README).
- The install screen ticks off apply's steps with its output underneath,
  asks before stopping a running install, and ends on the credentials and
  addresses, printed again once the terminal is given back.
- Without root it refuses at the plan, saying why. Without a terminal it
  says which flags do the same, exit 2. A window under 60x16 says so.

The terminal layer is ours and the standard library's (installer spec §8,
decision 1): raw mode, size and SIGWINCH through syscall, key decoding,
full redraws, NO_COLOR honored, the terminal restored on every way out.

Also: the survey's text moved to plan.SurveyText, which the first screen
and `inbuxa survey` share; and plan.Available now says host installs of the
console and the webmail aren't built, so --console host is refused at the
plan instead of after it.

Tests: key decoding, fitting and wrapping, and the wizard driven by keys
against an installer that changes nothing (defaults, loopback, the server
on the host, refusals, root and the y, stopping, a machine without docker,
a small window), under -race. e2e/cases/interface.sh drives the binary in a
pseudo-terminal on a fresh Debian 13 lab through a whole loopback install:
22/22. install-local, install-host, install-public, status-export and
topology pass unchanged.
2026-10-12 09:11:28 -07:00

110 lines
2.5 KiB
Go

// SPDX-FileCopyrightText: 2026 Coffey Labs LLC
// SPDX-License-Identifier: AGPL-3.0-or-later
package tui
import (
"fmt"
"io"
"os"
"os/signal"
"strings"
)
// Model is what the terminal shows and what keys do to it.
type Model interface {
// Update handles a key and says whether the program should end.
Update(Key) (quit bool)
// View is the whole screen, as lines; Run fits them to the window.
View(w, h int) []string
}
// Ticker is a Model that changes on its own -- an install making progress --
// and says so on the channel, so the screen is redrawn without a key.
type Ticker interface {
Changed() <-chan struct{}
}
const (
altScreenOn = "\x1b[?1049h"
altScreenOff = "\x1b[?1049l"
cursorHide = "\x1b[?25l"
cursorShow = "\x1b[?25h"
)
// Run gives the terminal to m until m says to quit. The terminal is put back
// as it was however Run ends, a panic included: a mail-server installer that
// leaves a terminal in raw mode has failed at the one thing it was trusted
// with first.
func Run(m Model, in, out *os.File) (err error) {
restore, err := rawMode(in)
if err != nil {
return fmt.Errorf("the terminal would not switch to raw mode: %w", err)
}
io.WriteString(out, altScreenOn+cursorHide)
defer func() {
io.WriteString(out, "\x1b[0m"+cursorShow+altScreenOff)
restore()
if r := recover(); r != nil {
err = fmt.Errorf("the interface stopped: %v", r)
}
}()
keys := make(chan []Key)
go func() {
buf := make([]byte, 256)
for {
n, err := in.Read(buf)
if err != nil {
close(keys)
return
}
keys <- decode(buf[:n])
}
}()
resized := make(chan os.Signal, 1)
signal.Notify(resized, resizeSignal)
defer signal.Stop(resized)
var changed <-chan struct{}
if t, ok := m.(Ticker); ok {
changed = t.Changed()
}
for {
draw(m, out)
select {
case ks, ok := <-keys:
if !ok {
return nil
}
for _, k := range ks {
if m.Update(k) {
return nil
}
}
case <-resized:
case <-changed:
}
}
}
// draw paints the whole frame. Every line is rewritten and the rest of each
// line cleared, which is plenty for six screens of text and leaves no
// half-drawn state to get wrong.
func draw(m Model, out io.Writer) {
w, h := size(os.Stdout)
lines := m.View(w, h)
var b strings.Builder
b.WriteString("\x1b[H")
for i := 0; i < h; i++ {
if i < len(lines) {
b.WriteString(Fit(lines[i], w))
}
b.WriteString("\x1b[0m\x1b[K")
if i < h-1 {
b.WriteString("\r\n")
}
}
io.WriteString(out, b.String())
}