From 1527d2269038d67fb3f99ddc5ff9ce77c3581ef6 Mon Sep 17 00:00:00 2001 From: Naohiro CHIKAMATSU Date: Mon, 25 Dec 2023 16:52:58 +0900 Subject: [PATCH] add: s3hub interactive ui mock --- cmd/subcmd/s3hub/interactive.go | 6 +- go.mod | 16 ++ go.sum | 36 +++++ ui/common.go | 40 +++++ ui/s3hub.go | 270 ++++++++++++++++++++++++++++++++ 5 files changed, 365 insertions(+), 3 deletions(-) create mode 100644 ui/common.go create mode 100644 ui/s3hub.go diff --git a/cmd/subcmd/s3hub/interactive.go b/cmd/subcmd/s3hub/interactive.go index e928550..3b7e390 100644 --- a/cmd/subcmd/s3hub/interactive.go +++ b/cmd/subcmd/s3hub/interactive.go @@ -1,8 +1,8 @@ package s3hub -import "fmt" +import "github.com/nao1215/rainbow/ui" +// interactive starts s3hub command interactive UI. func interactive() error { - fmt.Println("interactive mode is not implemented yet") - return nil + return ui.RunS3hubUI() } diff --git a/go.mod b/go.mod index a9a905d..3106468 100644 --- a/go.mod +++ b/go.mod @@ -3,11 +3,27 @@ module github.com/nao1215/rainbow go 1.19 require ( + github.com/charmbracelet/bubbletea v0.25.0 github.com/google/go-cmp v0.6.0 github.com/spf13/cobra v1.8.0 ) require ( + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.18 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect + github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // 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.2.0 // indirect github.com/spf13/pflag v1.0.5 // indirect + golang.org/x/sync v0.1.0 // indirect + golang.org/x/sys v0.7.0 // indirect + golang.org/x/term v0.6.0 // indirect + golang.org/x/text v0.3.8 // indirect ) diff --git a/go.sum b/go.sum index 4d244e6..b747c82 100644 --- a/go.sum +++ b/go.sum @@ -1,12 +1,48 @@ +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/bubbletea v0.25.0 h1:bAfwk7jRz7FKFl9RzlIULPkStffg5k6pNt5dywy4TcM= +github.com/charmbracelet/bubbletea v0.25.0/go.mod h1:EN3QDR1T5ZdWmdfDzYcqOCAps45+QIJbLOBxmVNWNNg= +github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 h1:q2hJAaP1k2wIvVRd/hEHD7lacgqrCPS+k8g1MndzfWY= +github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk= github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= +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-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= +github.com/mattn/go-isatty v0.0.18/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.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34= +github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho= +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 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= 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.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= +golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/term v0.6.0 h1:clScbb1cHjoCkyRbWwBEUZ5H/tIFu5TAXIqaZD0Gcjw= +golang.org/x/term v0.6.0/go.mod h1:m6U89DPEgQRMq3DNkDClhWw02AUbt2daBVO4cn4Hv9U= +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.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/ui/common.go b/ui/common.go new file mode 100644 index 0000000..52d8834 --- /dev/null +++ b/ui/common.go @@ -0,0 +1,40 @@ +package ui + +import ( + "fmt" + + "github.com/muesli/termenv" +) + +const ( + progressBarWidth = 71 + progressFullChar = "█" + progressEmptyChar = "░" +) + +// General stuff for styling the view +var ( + term = termenv.EnvColorProfile() + keyword = makeFgStyle("211") + subtle = makeFgStyle("241") + progressEmpty = subtle(progressEmptyChar) + dot = colorFg(" • ", "236") +) + +// makeFgStyle returns a function that will colorize the foreground of a given. +func makeFgStyle(color string) func(string) string { + return termenv.Style{}.Foreground(term.Color(color)).Styled +} + +// Color a string's foreground with the given value. +func colorFg(val, color string) string { + return termenv.String(val).Foreground(term.Color(color)).String() +} + +// checkbox represent [ ] and [x] items in the view. +func checkbox(label string, checked bool) string { + if checked { + return colorFg("[x] "+label, "212") + } + return fmt.Sprintf("[ ] %s", label) +} diff --git a/ui/s3hub.go b/ui/s3hub.go new file mode 100644 index 0000000..1da6536 --- /dev/null +++ b/ui/s3hub.go @@ -0,0 +1,270 @@ +package ui + +import ( + "fmt" + + tea "github.com/charmbracelet/bubbletea" + "github.com/muesli/reflow/indent" +) + +const ( + // s3hubTopMinChoice is the minimum choice number. + s3hubTopMinChoice = 0 + // s3hubTopMaxChoice is the maximum choice number. + s3hubTopMaxChoice = 4 + // s3hubTopCreateChoice is the choice number for creating the S3 bucket. + s3hubTopCreateChoice = 0 + // s3hubTopListChoice is the choice number for listing S3 buckets. + s3hubTopListChoice = 1 + // s3hubTopCopyChoice is the choice number for copying file to the S3 bucket. + s3hubTopCopyChoice = 2 + // s3hubTopDeleteContentsChoice is the choice number for deleting contents from the S3 bucket. + s3hubTopDeleteContentsChoice = 3 + // s3hubTopDeleteBucketChoice is the choice number for deleting the S3 bucket. + s3hubTopDeleteBucketChoice = 4 +) + +// s3hubRootModel is the top-level model for the application. +type s3hubRootModel struct { + // Choice is the currently selected menu item. + Choice int + // Chosen is true when the user has chosen a menu item. + Chosen bool + // Quitting is true when the user has quit the application. + Quitting bool +} + +// RunS3hubUI start s3hub command interactive UI. +func RunS3hubUI() error { + _, err := tea.NewProgram(&s3hubRootModel{}).Run() + return err +} + +// Init initializes the model. +func (m *s3hubRootModel) Init() tea.Cmd { + return nil +} + +// Main update function. +func (m *s3hubRootModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + // Make sure these keys always quit + if msg, ok := msg.(tea.KeyMsg); ok { + k := msg.String() + if k == "q" || k == "esc" || k == "ctrl+c" { + m.Quitting = true + return m, tea.Quit + } + } + if !m.Chosen { + return m.updateChoices(msg) + } + return m.updateChosen(msg) +} + +// View renders the application's UI. +func (m *s3hubRootModel) View() string { + if m.Quitting { + return "\n See you later!\n\n" // TODO: print log. + } + + var s string + if !m.Chosen { + s = m.choicesView() + } + return indent.String("\n"+s+"\n\n", 2) +} + +// updateChoices updates the model based on keypresses. +func (m *s3hubRootModel) updateChoices(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.KeyMsg: + switch msg.String() { + case "j", "down": + m.Choice++ + if m.Choice > s3hubTopMaxChoice { + m.Choice = s3hubTopMinChoice + } + case "k", "up": + m.Choice-- + if m.Choice < s3hubTopMinChoice { + m.Choice = s3hubTopMaxChoice + } + case "enter": + m.Chosen = true + switch m.Choice { + case s3hubTopCreateChoice: + return &s3hubCreateBucketModel{}, nil + case s3hubTopListChoice: + return &s3hubListBucketModel{}, nil + case s3hubTopCopyChoice: + return &s3hubCopyModel{}, nil + case s3hubTopDeleteContentsChoice: + return &s3hubDeleteContentsModel{}, nil + case s3hubTopDeleteBucketChoice: + return &s3hubDeleteBucketModel{}, nil + } + } + } + return m, nil +} + +// updateChosen updates the model when the user has chosen a menu item. +func (m *s3hubRootModel) updateChosen(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg.(type) { + } + return m, nil +} + +// choicesView returns a string containing the choices menu. +func (m *s3hubRootModel) choicesView() string { + c := m.Choice + template := "%s\n\n" + template += subtle("j/k, up/down: select") + dot + subtle("enter: choose") + dot + subtle("q, esc: quit") + + choices := fmt.Sprintf( + "%s\n%s\n%s\n%s\n%s\n", + checkbox("Create the S3 bucket", c == s3hubTopMinChoice), + checkbox("List S3 buckets", c == 1), + checkbox("Copy file to the S3 bucket", c == 2), + checkbox("Delete contents from the S3 bucket", c == 3), + checkbox("Delete the S3 bucket", c == s3hubTopMaxChoice), + ) + return fmt.Sprintf(template, choices) +} + +type s3hubCreateBucketModel struct { + // Quitting is true when the user has quit the application. + Quitting bool +} + +func (m *s3hubCreateBucketModel) Init() tea.Cmd { + return nil +} + +func (m *s3hubCreateBucketModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + // Make sure these keys always quit + if msg, ok := msg.(tea.KeyMsg); ok { + k := msg.String() + m.Quitting = true + if k == "q" || k == "esc" || k == "ctrl+c" { + return m, tea.Quit + } + } + return m, nil +} + +func (m *s3hubCreateBucketModel) View() string { + return fmt.Sprintf( + "%s\n%s", + "s3hubCreateBucketModel", + subtle("j/k, up/down: select")+dot+subtle("enter: choose")+dot+subtle("q, esc: quit")) +} + +type s3hubListBucketModel struct { + // Quitting is true when the user has quit the application. + Quitting bool +} + +func (m *s3hubListBucketModel) Init() tea.Cmd { + return nil +} + +func (m *s3hubListBucketModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + if msg, ok := msg.(tea.KeyMsg); ok { + k := msg.String() + if k == "q" || k == "esc" || k == "ctrl+c" { + m.Quitting = true + return m, tea.Quit + } + } + return m, nil +} + +func (m *s3hubListBucketModel) View() string { + return fmt.Sprintf( + "%s\n%s", + "s3hubListBucketModel", + subtle("j/k, up/down: select")+dot+subtle("enter: choose")+dot+subtle("q, esc: quit")) +} + +type s3hubCopyModel struct { + // Quitting is true when the user has quit the application. + Quitting bool +} + +func (m *s3hubCopyModel) Init() tea.Cmd { + return nil +} + +func (m *s3hubCopyModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + if msg, ok := msg.(tea.KeyMsg); ok { + k := msg.String() + if k == "q" || k == "esc" || k == "ctrl+c" { + m.Quitting = true + return m, tea.Quit + } + } + return m, nil +} + +func (m *s3hubCopyModel) View() string { + return fmt.Sprintf( + "%s\n%s", + "s3hubCopyModel", + subtle("j/k, up/down: select")+dot+subtle("enter: choose")+dot+subtle("q, esc: quit")) +} + +type s3hubDeleteContentsModel struct { + // Quitting is true when the user has quit the application. + Quitting bool +} + +func (m *s3hubDeleteContentsModel) Init() tea.Cmd { + return nil +} + +func (m *s3hubDeleteContentsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + if msg, ok := msg.(tea.KeyMsg); ok { + k := msg.String() + if k == "q" || k == "esc" || k == "ctrl+c" { + m.Quitting = true + return m, tea.Quit + } + } + return m, nil +} + +func (m *s3hubDeleteContentsModel) View() string { + return fmt.Sprintf( + "%s\n%s", + "s3hubDeleteContentsModel", + subtle("j/k, up/down: select")+dot+subtle("enter: choose")+dot+subtle("q, esc: quit")) +} + +type s3hubDeleteBucketModel struct { + // Quitting is true when the user has quit the application. + Quitting bool +} + +func (m *s3hubDeleteBucketModel) Init() tea.Cmd { + return nil +} + +func (m *s3hubDeleteBucketModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + if msg, ok := msg.(tea.KeyMsg); ok { + k := msg.String() + if k == "q" || k == "esc" || k == "ctrl+c" { + m.Quitting = true + return m, tea.Quit + } + } + return m, nil +} + +func (m *s3hubDeleteBucketModel) View() string { + return fmt.Sprintf( + "%s\n%s", + "s3hubDeleteBucketModel", + subtle("j/k, up/down: select")+dot+subtle("enter: choose")+dot+subtle("q, esc: quit")) + +}