Skip to content

Commit

Permalink
improve cli ui
Browse files Browse the repository at this point in the history
  • Loading branch information
marhaupe committed Oct 16, 2024
1 parent 3d65368 commit 511cb29
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
70 changes: 69 additions & 1 deletion packages/cli/app/cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package cmd

import (
"bufio"
"context"
"fmt"
"os"
"os/exec"
"runtime"
"strings"
"time"

"github.com/fatih/color"
"github.com/iris-contrib/middleware/cors"
"github.com/kataras/iris/v12"
"github.com/marhaupe/uitail/internal/executor"
"github.com/marhaupe/uitail/internal/logs"
"github.com/marhaupe/uitail/internal/static"
"github.com/spf13/cobra"
"golang.org/x/term"
)

var (
Expand Down Expand Up @@ -97,7 +103,10 @@ func (a *Root) Start() error {
app.Get("/logs", a.logService.GetLogs())
app.Get("/{asset:path}", a.staticServer.Handler())

fmt.Printf("🚀 Running uitail on http://localhost:%d\n", port)
clearScreen()
printStartupLogs(port)
go handleKeyPresses(port)

return app.Listen(fmt.Sprintf(":%d", port),
iris.WithConfiguration(iris.Configuration{
DisableStartupLog: true,
Expand All @@ -107,3 +116,62 @@ func (a *Root) Start() error {
}),
)
}

func printStartupLogs(port int) {
url := fmt.Sprintf("http://localhost:%d", port)
fmt.Printf("uitail is running on %s\n\n", color.New(color.FgCyan).Sprintf(url))
bold := color.New(color.Bold).SprintFunc()
light := color.New(color.FgBlack).SprintFunc()
fmt.Println("Shortcuts:")
fmt.Printf("› %s | %s\n", bold("w + enter"), light("Open in browser"))
fmt.Printf("› %s | %s\n", bold("q + enter"), light("Quit"))
}

func handleKeyPresses(port int) {
reader := bufio.NewReader(os.Stdin)
for {
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)

switch input {
case "w":
openWebBrowser(port)
case "q":
os.Exit(0)
}
}
}

func openWebBrowser(port int) {
url := fmt.Sprintf("http://localhost:%d", port)
var err error

switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}

if err != nil {
fmt.Printf("Error opening web browser: %v\n", err)
}
}

func clearScreen() {
fd := int(os.Stdout.Fd())
if !term.IsTerminal(fd) {
return
}
_, height, err := term.GetSize(fd)
if err != nil {
return
}
for i := 0; i < height-2; i++ {
fmt.Println()
}
// Move cursor to top-left corner
fmt.Print("\033[0;0H")
}
5 changes: 4 additions & 1 deletion packages/cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ toolchain go1.22.6

require (
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/fatih/color v1.17.0
github.com/google/uuid v1.6.0
github.com/iris-contrib/middleware/cors v0.0.0-20240502084239-34f27409ce72
github.com/kataras/iris/v12 v12.2.11
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/r3labs/sse/v2 v2.10.0
golang.org/x/term v0.25.0
)

require (
Expand All @@ -37,6 +39,7 @@ require (
github.com/klauspost/compress v1.17.9 // indirect
github.com/mailgun/raymond/v2 v2.0.48 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/microcosm-cc/bluemonday v1.0.27 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
Expand All @@ -51,7 +54,7 @@ require (
github.com/yosssi/ace v0.0.5 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240808152545-0cdaa3abc0fa // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/time v0.6.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
Expand Down
12 changes: 8 additions & 4 deletions packages/cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
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/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/fatih/color v1.17.0 h1:GlRw1BRJxkpqUCBKzKOw098ed57fEsKeNjpTe3cSjK4=
github.com/fatih/color v1.17.0/go.mod h1:YZ7TlrGPkiz6ku9fK3TLD/pl3CpsiFyu8N92HLgmosI=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/flosch/pongo2/v4 v4.0.2 h1:gv+5Pe3vaSVmiJvh/BZa82b7/00YUGm0PIyVVLop0Hw=
Expand Down Expand Up @@ -79,6 +79,7 @@ github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
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.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/microcosm-cc/bluemonday v1.0.27 h1:MpEUotklkwCSLeH+Qdx1VJgNqLlpY2KXwXFM08ygZfk=
Expand Down Expand Up @@ -164,10 +165,13 @@ golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/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.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.25.0 h1:WtHI/ltw4NvSUig5KARz9h521QvRC8RmF/cuYqifU24=
golang.org/x/term v0.25.0/go.mod h1:RPyXicDX+6vLxogjjRxjgD2TKtmAO6NZBsBRfrOLu7M=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand Down

0 comments on commit 511cb29

Please sign in to comment.