From bcb27082ff88f59f14b7ce12499b5d96c7caa3dd Mon Sep 17 00:00:00 2001 From: Daylin Morgan Date: Fri, 3 May 2024 10:51:57 -0500 Subject: [PATCH] oizys-go because why not --- lib/default.nix | 3 +- modules/networking/vpn.nix | 2 +- pkgs/oizys/oizys-go/.gitignore | 0 pkgs/oizys/oizys-go/cmd/root.go | 134 +++++++++++++++++++++++++ pkgs/oizys/oizys-go/default.nix | 11 ++ pkgs/oizys/oizys-go/go.mod | 33 ++++++ pkgs/oizys/oizys-go/go.sum | 70 +++++++++++++ pkgs/oizys/oizys-go/main.go | 7 ++ pkgs/oizys/oizys-go/pkg/oizys/oizys.go | 113 +++++++++++++++++++++ pkgs/oizys/oizys-go/pkg/oizys/table.go | 75 ++++++++++++++ 10 files changed, 446 insertions(+), 2 deletions(-) create mode 100644 pkgs/oizys/oizys-go/.gitignore create mode 100644 pkgs/oizys/oizys-go/cmd/root.go create mode 100644 pkgs/oizys/oizys-go/default.nix create mode 100644 pkgs/oizys/oizys-go/go.mod create mode 100644 pkgs/oizys/oizys-go/go.sum create mode 100644 pkgs/oizys/oizys-go/main.go create mode 100644 pkgs/oizys/oizys-go/pkg/oizys/oizys.go create mode 100644 pkgs/oizys/oizys-go/pkg/oizys/table.go diff --git a/lib/default.nix b/lib/default.nix index e1aa474..2dc57be 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -35,7 +35,8 @@ in rec { oizys-zig = pkgs.callPackage ../pkgs/oizys/oizys-zig {zig2nix = inputs.zig2nix;}; oizys-nim = pkgs.callPackage ../pkgs/oizys/oizys-nim {}; oizys-rs = pkgs.callPackage ../pkgs/oizys/oizys-rs {}; - default = oizys-zig; + oizys-go = pkgs.callPackage ../pkgs/oizys/oizys-go {}; + default = oizys-go; } ); devShells = forAllSystems ( diff --git a/modules/networking/vpn.nix b/modules/networking/vpn.nix index aa33c8f..b2e2bb3 100644 --- a/modules/networking/vpn.nix +++ b/modules/networking/vpn.nix @@ -16,5 +16,5 @@ mkOizysModule config "vpn" { autoStart = false; updateResolvConf = true; }; -}; + }; } diff --git a/pkgs/oizys/oizys-go/.gitignore b/pkgs/oizys/oizys-go/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/pkgs/oizys/oizys-go/cmd/root.go b/pkgs/oizys/oizys-go/cmd/root.go new file mode 100644 index 0000000..fb57ec8 --- /dev/null +++ b/pkgs/oizys/oizys-go/cmd/root.go @@ -0,0 +1,134 @@ +package cmd + +import ( + "errors" + "fmt" + "io/fs" + "log" + "os" + + cc "github.com/ivanpirog/coloredcobra" + "github.com/spf13/cobra" + "oizys/pkg/oizys" +) + +func setFlake() { + if flake == "" { + oizysDir, ok := os.LookupEnv("OIZYS_DIR") + if !ok { + home := os.Getenv("HOME") + flake = fmt.Sprintf("%s/%s", home, "oizys") + } else { + flake = oizysDir + } + } + + if _, err := os.Stat(flake); errors.Is(err, fs.ErrNotExist) { + log.Fatalln("path to flake:", flake, "does not exist") + } +} + +func setHost() { + if host == "" { + hostname, err := os.Hostname() + if err != nil { + log.Fatal(err) + } + host = hostname + } +} + +func Execute() { + cc.Init(&cc.Config{ + RootCmd: rootCmd, + Headings: cc.HiCyan + cc.Bold, + Commands: cc.HiYellow + cc.Bold, + Example: cc.Italic, + ExecName: cc.Bold, + Flags: cc.Bold, + NoExtraNewlines: true, + NoBottomNewline: true, + }) + err := rootCmd.Execute() + if err != nil { + os.Exit(1) + } +} + +var ( + flake string + host string + cacheName string +) + +var rootCmd = &cobra.Command{ + Use: "oizys", + Short: "nix begat oizys", + PersistentPreRun: func(cmd *cobra.Command, args []string) { + setFlake() + setHost() + }, +} + +var dryCmd = &cobra.Command{ + Use: "dry", + Short: "poor man's nix flake check", + Run: func(cmd *cobra.Command, args []string) { + oizys.NixDryRun(oizys.Output(flake, host)) + }, +} + +var outputCmd = &cobra.Command{ + Use: "output", + Short: "show nixosConfiguration attr", + Run: func(cmd *cobra.Command, args []string) { + fmt.Print(oizys.Output(flake, host)) + }, +} + +var bootCmd = &cobra.Command{ + Use: "boot", + Short: "nixos rebuild boot", + Run: func(cmd *cobra.Command, args []string) { + oizys.NixosRebuild("boot", flake) + }, +} + +var switchCmd = &cobra.Command{ + Use: "switch", + Short: "nixos rebuild switch", + Run: func(cmd *cobra.Command, args []string) { + oizys.NixosRebuild("switch", flake, args...) + }, +} + +var cacheCmd = &cobra.Command{ + Use: "cache", + Short: "build and push to cachix", + Run: func(cmd *cobra.Command, args []string) { + oizys.CacheBuild(oizys.Output(flake, host), cacheName, args...) + }, +} + +var buildCmd = &cobra.Command{ + Use: "build", + Short: "A brief description of your command", + Run: func(cmd *cobra.Command, args []string) { + oizys.NixBuild(oizys.Output(flake, host), args...) + }, +} + + + +func init() { + rootCmd.CompletionOptions.HiddenDefaultCmd = true + rootCmd.PersistentFlags().StringVar(&flake, "flake", "", "path to flake ($OIZYS_DIR or $HOME/oizys)") + rootCmd.PersistentFlags().StringVar(&host, "host", "", "host to build (current host)") + rootCmd.AddCommand(dryCmd) + rootCmd.AddCommand(outputCmd) + rootCmd.AddCommand(bootCmd) + rootCmd.AddCommand(buildCmd) + rootCmd.AddCommand(switchCmd) + rootCmd.AddCommand(cacheCmd) + cacheCmd.Flags().StringVarP(&cacheName, "cache", "c", "daylin", "name of cachix binary cache") +} diff --git a/pkgs/oizys/oizys-go/default.nix b/pkgs/oizys/oizys-go/default.nix new file mode 100644 index 0000000..70f3043 --- /dev/null +++ b/pkgs/oizys/oizys-go/default.nix @@ -0,0 +1,11 @@ +{ + buildGoModule, + lib, +}: +buildGoModule { + pname = "oizys"; + version = "unstable"; + + src = lib.cleanSource ./.; + vendorHash = "sha256-kh/7dV49KaQcD9ho8IpBcRc6+05bn4XpMzAI9JXu7+o="; +} diff --git a/pkgs/oizys/oizys-go/go.mod b/pkgs/oizys/oizys-go/go.mod new file mode 100644 index 0000000..b9efd5e --- /dev/null +++ b/pkgs/oizys/oizys-go/go.mod @@ -0,0 +1,33 @@ +module oizys + +go 1.22.2 + +require ( + github.com/charmbracelet/bubbles v0.18.0 + github.com/charmbracelet/bubbletea v0.26.1 + github.com/charmbracelet/lipgloss v0.10.0 + github.com/ivanpirog/coloredcobra v1.0.1 + github.com/spf13/cobra v1.8.0 + golang.org/x/term v0.19.0 +) + +require ( + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect + github.com/fatih/color v1.16.0 // indirect + github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect + github.com/mattn/go-runewidth v0.0.15 // indirect + github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/reflow v0.3.0 // indirect + github.com/muesli/termenv v0.15.2 // indirect + github.com/rivo/uniseg v0.4.7 // indirect + github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/sync v0.7.0 // indirect + golang.org/x/sys v0.19.0 // indirect + golang.org/x/text v0.3.8 // indirect +) diff --git a/pkgs/oizys/oizys-go/go.sum b/pkgs/oizys/oizys-go/go.sum new file mode 100644 index 0000000..c30047b --- /dev/null +++ b/pkgs/oizys/oizys-go/go.sum @@ -0,0 +1,70 @@ +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/charmbracelet/bubbles v0.18.0 h1:PYv1A036luoBGroX6VWjQIE9Syf2Wby2oOl/39KLfy0= +github.com/charmbracelet/bubbles v0.18.0/go.mod h1:08qhZhtIwzgrtBjAcJnij1t1H0ZRjwHyGsy6AL11PSw= +github.com/charmbracelet/bubbletea v0.26.1 h1:xujcQeF73rh4jwu3+zhfQsvV18x+7zIjlw7/CYbzGJ0= +github.com/charmbracelet/bubbletea v0.26.1/go.mod h1:FzKr7sKoO8iFVcdIBM9J0sJOcQv5nDQaYwsee3kpbgo= +github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMtwg/AFW3s= +github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE= +github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= +github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= +github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= +github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= +github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= +github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= +github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +github.com/ivanpirog/coloredcobra v1.0.1 h1:aURSdEmlR90/tSiWS0dMjdwOvCVUeYLfltLfbgNxrN4= +github.com/ivanpirog/coloredcobra v1.0.1/go.mod h1:iho4nEKcnwZFiniGSdcgdvRgZNjxm+h20acv8vqmN6Q= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= +github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= +github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= +github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= +github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= +github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= +github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g= +github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= +github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= +github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= +github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q= +golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk= +golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/pkgs/oizys/oizys-go/main.go b/pkgs/oizys/oizys-go/main.go new file mode 100644 index 0000000..194a958 --- /dev/null +++ b/pkgs/oizys/oizys-go/main.go @@ -0,0 +1,7 @@ +package main + +import "oizys/cmd" + +func main() { + cmd.Execute() +} diff --git a/pkgs/oizys/oizys-go/pkg/oizys/oizys.go b/pkgs/oizys/oizys-go/pkg/oizys/oizys.go new file mode 100644 index 0000000..27eae08 --- /dev/null +++ b/pkgs/oizys/oizys-go/pkg/oizys/oizys.go @@ -0,0 +1,113 @@ +package oizys + +import ( + "fmt" + "log" + "os" + "os/exec" + "strings" + + "github.com/charmbracelet/bubbles/table" + "github.com/muesli/termenv" + "golang.org/x/term" +) + +func TerminalSize() (int, int) { + fd := os.Stdout.Fd() + if !term.IsTerminal(int(fd)) { + log.Fatal("failed to get terminal size") + } + w, h, err := term.GetSize(int(fd)) + if err != nil { + log.Fatal(err) + } + return w, h +} + +func ParseDryRunOutput(nixOutput string) { + output := termenv.NewOutput(os.Stdout) + parts := strings.Split(nixOutput, "\nthese") + if len(parts) != 3 { + log.Println("no changes...") + return + } + built := strings.Split(strings.TrimSpace(parts[1]), "\n")[1:] + fetched := strings.Split(strings.TrimSpace(parts[2]), "\n")[1:] + + fmt.Println("Packages to build:", + output.String(fmt.Sprint(len(built))).Bold().Foreground(output.Color("6")), + ) + var rows []table.Row + for _, pkg := range built { + s := strings.SplitN(pkg, "-", 2) + hash, name := strings.Replace(s[0], "/nix/store/", "", 1), s[1] + rows = append(rows, table.Row{hash, name}) + } + + fmt.Println("Packages to fetch:", + output.String(fmt.Sprint(len(fetched))).Bold().Foreground(output.Color("6")), + ) + for _, pkg := range fetched { + s := strings.SplitN(pkg, "-", 2) + hash, name := strings.Replace(s[0], "/nix/store/", "", 1), s[1] + rows = append(rows, table.Row{hash, name}) + } + + w, _ := TerminalSize() + columns := []table.Column{ + {Title: "hash", Width: 34}, + {Title: "pkg", Width: int(w / 4)}, + } + ShowTable(columns, rows) +} + +func NixDryRun(path string) { + cmd := exec.Command("nix", "build", path, "--dry-run") + output, err := cmd.CombinedOutput() + if err != nil { + log.Fatal(err) + } + ParseDryRunOutput(string(output)) +} + +func NixosRebuild(subcmd string, flake string, rest ...string) { + args := []string{subcmd, "--flake"} + args = append(args, rest...) + cmd := exec.Command("nixos-rebuild", args...) + runCommand(cmd) +} + +func runCommand(cmd *exec.Cmd) { + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + if err := cmd.Run(); err != nil { + log.Fatal(err) + } +} + +func NixBuild(path string, rest ...string) { + args := []string{"build", path} + args = append(args, rest...) + fmt.Println(args) + cmd := exec.Command("nix", args...) + runCommand(cmd) +} + +func CacheBuild(path string, cache string, rest ...string) { + args := []string{ + "watch-exec", cache, "--", "nix", + "build", path, "--print-build-logs", + "--accept-flake-config", + } + args = append(args, rest...) + cmd := exec.Command("cachix", args...) + runCommand(cmd) +} + +func Output(flake string, host string) string { + return fmt.Sprintf( + "%s#nixosConfigurations.%s.config.system.build.toplevel", + flake, + host, + ) +} diff --git a/pkgs/oizys/oizys-go/pkg/oizys/table.go b/pkgs/oizys/oizys-go/pkg/oizys/table.go new file mode 100644 index 0000000..014f09a --- /dev/null +++ b/pkgs/oizys/oizys-go/pkg/oizys/table.go @@ -0,0 +1,75 @@ +package oizys + +import ( + "fmt" + "os" + + "github.com/charmbracelet/bubbles/table" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" +) + +var baseStyle = lipgloss.NewStyle(). + BorderStyle(lipgloss.NormalBorder()). + BorderForeground(lipgloss.Color("240")) + +type model struct { + table table.Model +} + +func (m model) Init() tea.Cmd { return nil } + +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + var cmd tea.Cmd + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.String() { + case "esc": + if m.table.Focused() { + m.table.Blur() + } else { + m.table.Focus() + } + case "q", "ctrl+c": + return m, tea.Quit + case "enter": + return m, tea.Batch( + tea.Printf("Let's go to %s!", m.table.SelectedRow()[1]), + ) + } + } + m.table, cmd = m.table.Update(msg) + return m, cmd +} + +func (m model) View() string { + return baseStyle.Render(m.table.View()) + "\n" +} + +func ShowTable(columns []table.Column, rows []table.Row) { + + t := table.New( + table.WithColumns(columns), + table.WithRows(rows), + table.WithFocused(true), + table.WithHeight(15), + ) + + s := table.DefaultStyles() + s.Header = s.Header. + BorderStyle(lipgloss.NormalBorder()). + BorderForeground(lipgloss.Color("240")). + BorderBottom(true). + Bold(false) + s.Selected = s.Selected. + Foreground(lipgloss.Color("229")). + Background(lipgloss.Color("57")). + Bold(false) + t.SetStyles(s) + + m := model{t} + if _, err := tea.NewProgram(m).Run(); err != nil { + fmt.Println("Error running program:", err) + os.Exit(1) + } +}