1
0
Fork 0

Compare commits

...

No commits in common. "27b213649e07ee7ba35838b58a251b3dca163aac" and "6fe4fae063be562b21034f77d7d74c28f8916b9c" have entirely different histories.

8 changed files with 300 additions and 187 deletions

19
LICENSE 100644
View File

@ -0,0 +1,19 @@
Copyright (c) 2023 John Olheiser
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.

7
README.md 100644
View File

@ -0,0 +1,7 @@
# nixpl
A ver basic side-by-side nix repl.
## License
[MIT](LICENSE)

26
flake.lock 100644
View File

@ -0,0 +1,26 @@
{
"nodes": {
"nixpkgs": {
"locked": {
"lastModified": 1703192751,
"narHash": "sha256-ZIQ84AWlIHRgZe5KDcaw303MBkGSMEVlFxMiqjMVkbI=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "30cd89625c46c801946da2c5d249dc5c0cb37da5",
"type": "github"
},
"original": {
"owner": "nixos",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"nixpkgs": "nixpkgs"
}
}
},
"root": "root",
"version": 7
}

34
flake.nix 100644
View File

@ -0,0 +1,34 @@
{
description = "Nix side-by-side repl";
inputs.nixpkgs.url = "github:nixos/nixpkgs";
outputs = {
self,
nixpkgs,
}: let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
in {
packages.${system}.default = pkgs.buildGoModule {
pname = "nixpl";
version = "0.1.0";
src = ./.;
vendorHash = "sha256-uBp+x6UjVJUhzOnWngHPkkWUjGEVkYWfIrZhUVVkxPo=";
meta = with pkgs.lib; {
description = "Nix side-by-side repl";
homepage = "https://git.jojodev.com/jolheiser/nixpl";
maintainers = with maintainers; [jolheiser];
mainProgram = "nixpl";
};
};
devShells.${system}.default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [
go
gopls
alejandra
];
};
};
}

5
go.mod
View File

@ -3,14 +3,13 @@ module go.jolheiser.com/nixpl
go 1.21.4
require (
git.sr.ht/~rockorager/tcell-term v0.10.0
github.com/fsnotify/fsnotify v1.7.0
github.com/gdamore/tcell/v2 v2.6.1-0.20231203215052-2917c3801e73
github.com/rivo/tview v0.0.0-20231206124440-5f078138442e
)
require (
git.sr.ht/~rockorager/tcell-term v0.10.0 // indirect
github.com/creack/pty v1.1.17 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect

6
go.sum
View File

@ -3,6 +3,7 @@ git.sr.ht/~rockorager/tcell-term v0.10.0/go.mod h1:Snxh5CrziiA2CjyLOZ6tGAg5vMPlE
github.com/creack/pty v1.1.17 h1:QeVUsEDNrLBW4tMgZHvxy18sKtr6VI492kBhUfhDJNI=
github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
@ -18,9 +19,8 @@ github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU=
github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/tview v0.0.0-20231206124440-5f078138442e h1:mPy47VW9tkqImnSPgcjnEHJuG3XHDBtXj2hDb1qBrRs=
github.com/rivo/tview v0.0.0-20231206124440-5f078138442e/go.mod h1:c0SPlNPXkM+/Zgjn/0vD3W0Ds1yxstN7lpquqLDpWCg=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.3 h1:utMvzDsuh3suAEnhH0RdHmoPbU648o6CvXxTx4SBMOw=
github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
@ -29,6 +29,7 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
@ -73,4 +74,5 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

244
main.go
View File

@ -7,103 +7,11 @@ import (
"os"
"os/exec"
"path/filepath"
"strings"
tcellterm "git.sr.ht/~rockorager/tcell-term"
"github.com/fsnotify/fsnotify"
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/views"
)
type model struct {
term *tcellterm.VT
s tcell.Screen
termView views.View
title *views.TextBar
titleView views.View
result *views.TextArea
resultView views.View
}
// Update is the main event handler. It should only be called by the main thread
func (m *model) Update(ev tcell.Event) {
switch ev := ev.(type) {
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyCtrlC:
m.term.Close()
m.s.Fini()
return
}
if m.term != nil {
m.term.HandleEvent(ev)
}
m.term.Draw()
m.result.Draw()
m.s.Show()
case *tcell.EventResize:
w, _ := m.s.Size()
if m.term != nil {
m.termView.Resize(0, 2, w/2, -1)
m.term.Resize(m.termView.Size())
}
m.titleView.Resize(0, 0, -1, 2)
m.title.Resize()
m.resultView.Resize(w/2, 2, w/2, -1)
m.result.Resize()
m.title.Draw()
m.term.Draw()
m.result.Draw()
m.s.Sync()
return
case *tcellterm.EventRedraw:
m.term.Draw()
m.title.Draw()
m.result.Draw()
row, col, style, vis := m.term.Cursor()
if vis {
m.s.SetCursorStyle(style)
m.s.ShowCursor(col, row+2)
} else {
m.s.HideCursor()
}
m.s.Show()
return
case *tcellterm.EventClosed:
m.s.Clear()
m.s.Fini()
return
case *tcell.EventPaste:
m.term.HandleEvent(ev)
return
case *tcell.EventMouse:
// Translate the coordinates to our global coordinates (y-2)
x, y := ev.Position()
if y-2 < 0 {
// Event is outside our view
return
}
e := tcell.NewEventMouse(x, y-2, ev.Buttons(), ev.Modifiers())
m.term.HandleEvent(e)
return
case *tcellterm.EventMouseMode:
m.s.EnableMouse(ev.Flags()...)
case *tcellterm.EventPanic:
m.s.Clear()
m.s.Fini()
fmt.Println(ev.Error)
}
return
}
// HandleEvent is used to handle events from underlying widgets. Any events
// which redraw must be executed in the main goroutine by posting the event back
// to tcell
func (m *model) HandleEvent(ev tcell.Event) {
m.s.PostEvent(ev)
}
func main() {
fs := flag.NewFlagSet("nixpl", flag.ExitOnError)
if err := fs.Parse(os.Args[1:]); err != nil {
@ -125,101 +33,14 @@ func main() {
}
defer fi.Close()
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
defer watcher.Close()
watcher.Add(apn)
m := &model{}
m.s, err = tcell.NewScreen()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if err = m.s.Init(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
m.s.EnablePaste()
w, _ := m.s.Size()
m.title = views.NewTextBar()
m.title.SetCenter(
"nixpl",
tcell.StyleDefault.Foreground(tcell.ColorBlue).
Bold(true).
Underline(true),
)
m.titleView = views.NewViewPort(m.s, 0, 0, -1, 2)
m.title.SetView(m.titleView)
m.termView = views.NewViewPort(m.s, 0, 2, w/2, -1)
m.term = tcellterm.New()
m.term.SetSurface(m.termView)
m.term.Attach(m.HandleEvent)
m.resultView = views.NewViewPort(m.s, w/2, 2, w/2, -1)
m.result = views.NewTextArea()
m.result.SetView(m.resultView)
m.s.EnableMouse()
go func() {
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) {
content, err := os.ReadFile(apn)
if err != nil {
continue
}
var evalBuf bytes.Buffer
eval := exec.Command("nix", "eval", "--expr", string(content))
eval.Stdout = &evalBuf
if err := eval.Run(); err != nil {
continue
}
var formatBuf bytes.Buffer
format := exec.Command("alejandra", "-q", "-")
format.Stdin = &evalBuf
format.Stdout = &formatBuf
if err := format.Run(); err != nil {
continue
}
var batBuf bytes.Buffer
bat := exec.Command("bat", "-pp", "-l", "nix", "--color", "always", "-")
bat.Stdin = &formatBuf
bat.Stdout = &batBuf
if err := bat.Run(); err != nil {
continue
}
m.result.SetContent(batBuf.String())
m.result.Draw()
m.s.Sync()
}
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
}
}()
if fs.Arg(0) != "" {
content, _ := os.ReadFile(fs.Arg(0))
fi.Write(content)
fi.Sync()
}
m := newModel()
go watch(apn, m)
cmd := exec.Command(os.Getenv("EDITOR"), apn)
err = m.term.Start(cmd)
if err != nil {
@ -233,3 +54,62 @@ func main() {
m.Update(ev)
}
}
func watch(file string, m *model) {
watcher, err := fsnotify.NewWatcher()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
defer watcher.Close()
watcher.Add(file)
fn := func() {
content, err := os.ReadFile(file)
if err != nil {
return
}
var evalBuf bytes.Buffer
var errBuf bytes.Buffer
eval := exec.Command("nix", "eval", "--expr", string(content))
eval.Stdout = &evalBuf
eval.Stderr = &errBuf
if err := eval.Run(); err != nil {
if errBuf.Len() > 0 {
m.UpdateResult(errBuf.String())
}
return
}
result := evalBuf.String()
if _, err := exec.LookPath("alejandra"); err == nil {
var formatBuf bytes.Buffer
format := exec.Command("alejandra", "-q", "-")
format.Stdin = strings.NewReader(result)
format.Stdout = &formatBuf
if err := format.Run(); err != nil {
return
}
result = formatBuf.String()
}
m.UpdateResult(result)
}
fn()
for {
select {
case event, ok := <-watcher.Events:
if !ok {
return
}
if event.Has(fsnotify.Write) {
fn()
}
case _, ok := <-watcher.Errors:
if !ok {
return
}
}
}
}

146
model.go 100644
View File

@ -0,0 +1,146 @@
package main
import (
"fmt"
"os"
tcellterm "git.sr.ht/~rockorager/tcell-term"
"github.com/gdamore/tcell/v2"
"github.com/gdamore/tcell/v2/views"
)
type model struct {
term *tcellterm.VT
s tcell.Screen
termView views.View
title *views.TextBar
titleView views.View
result *views.TextArea
resultView views.View
}
func newModel() *model {
var err error
m := &model{}
m.s, err = tcell.NewScreen()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if err = m.s.Init(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
m.s.EnablePaste()
w, _ := m.s.Size()
m.title = views.NewTextBar()
m.title.SetCenter(
"nixpl",
tcell.StyleDefault.Foreground(tcell.ColorBlue).
Bold(true).
Underline(true),
)
m.titleView = views.NewViewPort(m.s, 0, 0, -1, 2)
m.title.SetView(m.titleView)
m.termView = views.NewViewPort(m.s, 0, 2, w/2, -1)
m.term = tcellterm.New()
m.term.SetSurface(m.termView)
m.term.Attach(m.HandleEvent)
m.resultView = views.NewViewPort(m.s, w/2, 2, w/2, -1)
m.result = views.NewTextArea()
m.result.SetView(m.resultView)
m.s.EnableMouse()
return m
}
// Update is the main event handler. It should only be called by the main thread
func (m *model) Update(ev tcell.Event) {
switch ev := ev.(type) {
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyCtrlC:
m.term.Close()
m.s.Fini()
return
}
if m.term != nil {
m.term.HandleEvent(ev)
}
m.term.Draw()
m.result.Draw()
m.s.Show()
case *tcell.EventResize:
w, _ := m.s.Size()
if m.term != nil {
m.termView.Resize(0, 2, w/2, -1)
m.term.Resize(m.termView.Size())
}
m.titleView.Resize(0, 0, -1, 2)
m.title.Resize()
m.resultView.Resize(w/2, 2, w/2, -1)
m.result.Resize()
m.title.Draw()
m.term.Draw()
m.result.Draw()
m.s.Sync()
return
case *tcellterm.EventRedraw:
m.term.Draw()
m.title.Draw()
m.result.Draw()
row, col, style, vis := m.term.Cursor()
if vis {
m.s.SetCursorStyle(style)
m.s.ShowCursor(col, row+2)
} else {
m.s.HideCursor()
}
m.s.Show()
return
case *tcellterm.EventClosed:
m.s.Clear()
m.s.Fini()
return
case *tcell.EventPaste:
m.term.HandleEvent(ev)
return
case *tcell.EventMouse:
// Translate the coordinates to our global coordinates (y-2)
x, y := ev.Position()
if y-2 < 0 {
// Event is outside our view
return
}
e := tcell.NewEventMouse(x, y-2, ev.Buttons(), ev.Modifiers())
m.term.HandleEvent(e)
return
case *tcellterm.EventMouseMode:
m.s.EnableMouse(ev.Flags()...)
case *tcellterm.EventPanic:
m.s.Clear()
m.s.Fini()
fmt.Println(ev.Error)
}
return
}
// HandleEvent is used to handle events from underlying widgets. Any events
// which redraw must be executed in the main goroutine by posting the event back
// to tcell
func (m *model) HandleEvent(ev tcell.Event) {
m.s.PostEvent(ev)
}
func (m *model) UpdateResult(result string) {
m.result.SetContent(result)
m.result.Draw()
m.s.Sync()
}