Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Apps import the public packages; `internal/` holds the implementation, which onl
| `base/textview`, `base/textview/highlight` | `nsh-base`'s `text_view`, `highlight` | `TextView`: Markdown, HTML or the app's blocks, and its code highlighting ([text view](docs/architecture/textview.md)) |
| `base/formula` | — (MyGo's `contrib/mygo` adapter of Untie) | `View`: TeX and Typst math laid out by Untie and drawn as canvas shapes in New Computer Modern Math, and `RenderMath` for `TextView` ([formulas](docs/architecture/components.md#formulas)) |
| `base/diagram` | — (limu's `limumygo`, its MyGo adapter) | `View`: Mermaid diagrams read and laid out by limu and drawn on a canvas in the platform's font, and `RenderDiagram` for `TextView` ([diagrams](docs/architecture/components.md#diagrams)) |
| `base/terminal`, `tools/libghostty` | — (Ghostty's libghostty-vt) | `Terminal` and its `View`: a program in a pseudo-terminal with libghostty-vt loaded through purego, and the command building the library ([terminal](docs/architecture/terminal.md)) |
| `base/terminal`, `tools/libghostty` | — (Ghostty's libghostty-vt) | `Terminal` and its `View`: local programs and remote byte streams with libghostty-vt loaded through purego or instantiated as browser WebAssembly, and the command building the library ([terminal](docs/architecture/terminal.md)) |
| `base/editor`, `base/diff` | `nsh-base`'s `editor`, `diff` | `Editor`, a code editor, and `DiffView`, the changes between texts or of a patch ([editor](docs/architecture/editor.md)) |
| `base/richeditor`, `ui/richeditor` | `nsh-base`'s `rich_editor` | `RichEditor`, a ProseMirror-style document with Tiptap's extensions, and its styled form ([rich editor](docs/architecture/richeditor.md)) |
| `base/internal/*` | `nsh-base`'s `syntax`, `window` | What `base`'s packages share: highlighting, the rows a virtualized view makes, part styles and sheets |
Expand Down
2 changes: 2 additions & 0 deletions NOTICE
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ https://github.com/egoist/mygo, Copyright (c) 2026 MyGo contributors, licensed
under the MIT License. base/terminal/internal/library/lib holds libghostty-vt
built from Ghostty, https://github.com/ghostty-org/ghostty, Copyright (c) 2024
Mitchell Hashimoto, Ghostty contributors, licensed under the MIT License.
The browser build also links Zig's standard library and Wuffs; their MIT
notices are retained in base/terminal/internal/library/LICENSE.
base/terminal/ghostty_themes.go holds iTerm2-Color-Schemes' themes as Ghostty
ships them, under the MIT License its header gives.

Expand Down
18 changes: 18 additions & 0 deletions base/terminal/browser_js_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//go:build js && !nsgominiprogram && !nsgoharmony

package terminal

import (
"strings"
"testing"
)

func TestBrowserNeedsRemoteConnection(t *testing.T) {
term, err := New(Options{})
if term != nil {
term.Close()
}
if err == nil || !strings.Contains(err.Error(), "give Options.Conn a remote session") {
t.Fatalf("a browser's local program request: %v", err)
}
}
16 changes: 14 additions & 2 deletions base/terminal/ghostty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package terminal
import (
"os"
"path/filepath"
"runtime"
"slices"
"strings"
"testing"
Expand Down Expand Up @@ -58,8 +59,11 @@ font-family = "Iosevka"
}

func TestGhosttyTheme(t *testing.T) {
config := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", config)
config := ""
if runtime.GOOS != "js" {
config = t.TempDir()
t.Setenv("XDG_CONFIG_HOME", config)
}
if len(ghosttyThemes) < 500 || !slices.IsSortedFunc(ghosttyThemes[:], func(a, b ghosttyTheme) int { return strings.Compare(a.name, b.name) }) {
t.Fatalf("%d themes, sorted: want hundreds", len(ghosttyThemes))
}
Expand All @@ -83,6 +87,14 @@ func TestGhosttyTheme(t *testing.T) {
}
}

// User files are native; the browser uses the bundled catalogs.
if runtime.GOOS == "js" {
if names := GhosttyThemes(); !slices.Contains(names, "Catppuccin Latte") || len(names) != len(ghosttyThemes) {
t.Errorf("browser theme catalog: %d names", len(names))
}
return
}

// The user's themes come first, as in Ghostty.
dir := filepath.Join(config, "ghostty", "themes")
os.MkdirAll(dir, 0o755)
Expand Down
75 changes: 75 additions & 0 deletions base/terminal/internal/library/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
Ghostty

MIT License

Copyright (c) 2024 Mitchell Hashimoto, Ghostty contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Zig standard library

The MIT License (Expat)

Copyright (c) Zig contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Wuffs

Copyright 2023 The Wuffs Authors

Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
8 changes: 8 additions & 0 deletions base/terminal/internal/library/assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package library

import _ "embed"

// Keep the prebuilt library's notices when apps vendor this package.
//
//go:embed LICENSE
var _ string
Binary file not shown.
2 changes: 2 additions & 0 deletions base/terminal/internal/library/library.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !js || nsgominiprogram || nsgoharmony

// Package library finds libghostty-vt for the terminal: the file
// $NSGO_GHOSTTY_VT names, the one the nsgo tool put into the app, or on
// desktops the build for the platform the package embeds, written once
Expand Down
14 changes: 14 additions & 0 deletions base/terminal/internal/library/library_js.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//go:build js && !nsgominiprogram && !nsgoharmony

package library

import _ "embed"

//go:embed lib/js-wasm/ghostty-vt.wasm
var wasm []byte

// Find returns the embedded browser module's identity; it needs no file system.
func Find() (string, error) { return "embedded:ghostty-vt.wasm", nil }

// Wasm is the pinned WebAssembly library, used by the browser VT binding.
func Wasm() []byte { return wasm }
63 changes: 63 additions & 0 deletions base/terminal/internal/vt/abi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package vt

import "encoding/binary"

func abiSize(m manifest, name string) uintptr {
switch name {
case "pointer":
return uintptr(m.ABI.PointerSize)
case "usize", "isize":
return uintptr(m.ABI.UsizeSize)
case "bool", "u8", "i8":
return 1
case "u16", "i16":
return 2
case "u32", "i32", "f32":
return 4
case "u64", "i64", "f64":
return 8
}
return m.Types[name].Size
}

// convertABI copies values, never addresses Go memory from guest pointers. A pointer remains an opaque guest address until an explicit memory access.
func convertABI(dst, src []byte, name string, to, from manifest) {
t, ok := from.Types[name]
if !ok || t.Kind != "struct" {
if ok && t.Kind == "alias" {
convertABI(dst, src, t.Type, to, from)
return
}
clear(dst)
copy(dst, src)
return
}
for field, f := range t.Fields {
d := to.Types[name].Fields[field]
in, out := src[f.Offset:f.Offset+f.Size], dst[d.Offset:d.Offset+d.Size]
if field == "size" && unsignedABI(in) != 0 {
putUnsignedABI(out, uint64(len(dst)))
continue
}
if f.Type == "array" {
a, b := abiSize(from, f.Elem), abiSize(to, d.Elem)
for i := range f.Count {
convertABI(out[i*b:(i+1)*b], in[i*a:(i+1)*a], f.Elem, to, from)
}
} else {
convertABI(out, in, f.Type, to, from)
}
}
}

func unsignedABI(b []byte) uint64 {
var buf [8]byte
copy(buf[:], b)
return binary.LittleEndian.Uint64(buf[:])
}

func putUnsignedABI(b []byte, value uint64) {
var buf [8]byte
binary.LittleEndian.PutUint64(buf[:], value)
copy(b, buf[:])
}
1 change: 1 addition & 0 deletions base/terminal/internal/vt/abi64.json

Large diffs are not rendered by default.

29 changes: 29 additions & 0 deletions base/terminal/internal/vt/abi_generate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//go:build !js

package vt

import (
"encoding/json"
"os"
"testing"
)

// TestWriteWasmHostABI records the 64-bit C ABI the shared Go structs mirror. The browser uses it to marshal the separate 32-bit guest ABI. Rebuild it with the pinned native library whenever the binding's commit changes.
func TestWriteWasmHostABI(t *testing.T) {
path := os.Getenv("NSGO_GHOSTTY_ABI_OUT")
if path == "" {
t.Skip("set NSGO_GHOSTTY_ABI_OUT on a native 64-bit target")
}
loadLib(t)
var value any
if err := json.Unmarshal([]byte(cStringAt(call(fnTypeJSON))), &value); err != nil {
t.Fatal(err)
}
data, err := json.Marshal(value)
if err != nil {
t.Fatal(err)
}
if err := os.WriteFile(path, append(data, '\n'), 0o644); err != nil {
t.Fatal(err)
}
}
66 changes: 66 additions & 0 deletions base/terminal/internal/vt/abi_js_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//go:build js && !nsgominiprogram && !nsgoharmony

package vt

import (
"bytes"
"encoding/json"
"testing"
"unsafe"
)

func TestBrowserMemoryABI(t *testing.T) {
before := wasm
defer func() { wasm = before }()
if _, err := open("embedded:ghostty-vt.wasm"); err != nil {
t.Fatal(err)
}
p := uintptr(uint32(wasm.exports.Get("ghostty_type_json").Invoke().Int()))
var m manifest
if err := json.Unmarshal([]byte(foreignCString(p)), &m); err != nil {
t.Fatal(err)
}
if err := checkManifest(m); err != nil {
t.Fatal(err)
}
if wasm.guest.ABI.PointerSize != 4 || wasm.host.ABI.PointerSize != int(unsafe.Sizeof(uintptr(0))) {
t.Fatal("guest and host pointer widths were not distinguished")
}
old := wasm.exports.Get("memory").Get("buffer")
const size = 32 << 20
allocation := uintptr(uint32(wasm.exports.Get("ghostty_wasm_alloc").Invoke(size).Int()))
if allocation == 0 {
t.Fatal("allocation failed")
}
defer wasm.exports.Get("ghostty_wasm_free").Invoke(int(allocation), size)
if old.Equal(wasm.exports.Get("memory").Get("buffer")) {
t.Fatal("control allocation did not grow guest memory")
}
want := []byte("memory after growth")
writeForeignBytes(allocation, want)
if got := foreignBytes(allocation, uintptr(len(want))); !bytes.Equal(got, want) {
t.Fatalf("after guest memory growth: %q", got)
}
}

func TestBrowserPromptBoolABI(t *testing.T) {
loadLib(t)
term, err := NewTerminal(20, 5, Effects{})
if err != nil {
t.Fatal(err)
}
defer term.Free()
for _, test := range []struct {
input string
want bool
}{{"", false}, {"\x1b]133;A\x07$ \x1b]133;B\x07", true}, {"\x1b[?1049h", false}} {
term.Write([]byte(test.input))
var got bool
if err := result(call(fnTerminalGet, term.h, 39, uintptr(unsafe.Pointer(&got)))); err != nil {
t.Fatal(err)
}
if got != test.want {
t.Fatalf("cursor at prompt after %q: %v, want %v", test.input, got, test.want)
}
}
}
4 changes: 3 additions & 1 deletion base/terminal/internal/vt/ffi.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ func call(fn uintptr, args ...uintptr) uintptr {

func registerFunc(fptr any, addr uintptr) { purego.RegisterFunc(fptr, addr) }

func newCallback(fn any) uintptr { return purego.NewCallback(fn) }
func newCallback(_ string, fn any) uintptr { return purego.NewCallback(fn) }

func setupPlatform() {}
Loading
Loading