From f5370ca6cfec698cb4e42453ea3964a4415d63bf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Johannes=20Ke=C3=9Fler?= Date: Mon, 20 Feb 2023 13:57:09 +0100 Subject: [PATCH] list view but there is a problem with the displayed size... --- client/go-tui/Makefile | 17 +++++++ client/go-tui/README | 26 ++++++++-- client/go-tui/create.go | 51 ++++++++++--------- client/go-tui/list.go | 96 +++++++++++++++++++++++++++++++++++ client/go-tui/scientia-tui.go | 42 ++++++++------- client/go-tui/start.go | 42 +++++++-------- 6 files changed, 209 insertions(+), 65 deletions(-) create mode 100644 client/go-tui/Makefile create mode 100644 client/go-tui/list.go diff --git a/client/go-tui/Makefile b/client/go-tui/Makefile new file mode 100644 index 0000000..ce72e53 --- /dev/null +++ b/client/go-tui/Makefile @@ -0,0 +1,17 @@ +all: + @echo "Options are: build and buildall" + +build: + @echo "Building for local os/arch..." + go build -o scientia-tui-`go env GOOS`-`go env GOARCH` + +buildall: + @echo "Building for linux/amd64 arch..." + GOOS=linux GOARCH=amd64 go build -o scientia-tui-linux-amd64 + @echo "Done: scientia-tui-linux-amd64" + @echo "Building for windows/amd64 arch..." + GOOS=windows GOARCH=amd64 go build -o scientia-tui-windows-amd64 + @echo "Done: scientia-tui-windows-amd64" + @echo "Building for macOS/amd64 arch..." + GOOS=darwin GOARCH=amd64 go build -o scientia-tui-darwin-amd64 + @echo "Done: scientia-tui-darwin-amd64" \ No newline at end of file diff --git a/client/go-tui/README b/client/go-tui/README index c7e4141..f05f429 100644 --- a/client/go-tui/README +++ b/client/go-tui/README @@ -5,10 +5,30 @@ https://://www.bananas-playground.net/projekt/scientia This is a very simple, with limited experience written, go program. Use at own risk and feel free to improve. -Howto build: -Nothing special, just go build -o scientia-tui +# Howto build: +Nothing special, just use the provide Makefile or directly "go build -o scientia-tui" to use your current os/arch settings. -Uses: +# Uses: https://github.com/charmbracelet/bubbletea https://github.com/charmbracelet/bubbles https://github.com/charmbracelet/lipgloss + +# Usage +At first usage you need to create the config and the individual secret. +Run $scientia-tui -create-config-file to create the default config file. +The path to the config file is printed. +Change the host address and update your server it with the secret, which is randomly created. + +Read from a file +$ scientia-tui file.txt +or piped +$ cat file.txt | scientia-tui + +Commandline arguments (optional): + -create-config-file + Create default config file + -debug + Print debug infos + -verbose + Produce verbose output + diff --git a/client/go-tui/create.go b/client/go-tui/create.go index ae5c8c9..fa758ae 100644 --- a/client/go-tui/create.go +++ b/client/go-tui/create.go @@ -36,7 +36,7 @@ func initCreate() textarea.Model { ta := textarea.New() ta.Placeholder = "Once upon a time..." ta.SetHeight(10) - ta.SetWidth(50) + ta.SetWidth(80) ta.Focus() return ta @@ -44,7 +44,8 @@ func initCreate() textarea.Model { func createView(m mainModel) string { return lipgloss.JoinVertical(lipgloss.Left, - headline.Render("Create a new entry"), m.create.View(), + headline.Render("Create a new entry"), + m.create.View(), infoText.Render("esc*2 to get back and discard. ctrl+s to save.")) } @@ -55,28 +56,32 @@ func createUpdate(msg tea.Msg, m mainModel) (tea.Model, tea.Cmd) { switch msg := msg.(type) { - case tea.KeyMsg: - switch msg.Type { - case tea.KeyCtrlC: - m.quitting = true - return m, tea.Quit - case tea.KeyCtrlS: - m.choice = "" - return m, nil + case tea.WindowSizeMsg: + m.create.SetWidth(msg.Width) + return m, nil - case tea.KeyEsc: - if m.create.Focused() { - m.create.Blur() - } else if !m.create.Focused() { - m.choice = "" - return m, nil - } - default: - if !m.create.Focused() { - cmd = m.create.Focus() - cmds = append(cmds, cmd) - } - } + case tea.KeyMsg: + switch msg.Type { + case tea.KeyCtrlC: + m.quitting = true + return m, tea.Quit + case tea.KeyCtrlS: + m.choice = "" + return m, nil + + case tea.KeyEsc: + if m.create.Focused() { + m.create.Blur() + } else if !m.create.Focused() { + m.choice = "" + return m, nil + } + default: + if !m.create.Focused() { + cmd = m.create.Focus() + cmds = append(cmds, cmd) + } + } } m.create, cmd = m.create.Update(msg) diff --git a/client/go-tui/list.go b/client/go-tui/list.go new file mode 100644 index 0000000..6a77a99 --- /dev/null +++ b/client/go-tui/list.go @@ -0,0 +1,96 @@ +/** + * scientia + * + * A terminal client written in go. + * + * Copyright 2023 Johannes Keßler + * + * https://www.bananas-playground.net/projekt/scientia/ + * + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the COMMON DEVELOPMENT AND DISTRIBUTION LICENSE + * + * You should have received a copy of the + * COMMON DEVELOPMENT AND DISTRIBUTION LICENSE (CDDL) Version 1.0 + * along with this program. If not, see http://www.sun.com/cddl/cddl.html + * + * + * This is the create "screen". It displays a textarea to input text into. + * Does the save and creation + */ +package main + +import ( + "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")) + headerStyle = lipgloss.NewStyle().BorderStyle(lipgloss.NormalBorder()).BorderForeground(lipgloss.Color("240")).BorderBottom(true).Bold(false) + selectedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("229")).Background(lipgloss.Color("57")).Bold(false) +) + +func initList() table.Model { + + columns := []table.Column{ + {Title: "ID", Width: 4}, + {Title: "Date", Width: 10}, + {Title: "Body", Width: 10}, + } + + rows := []table.Row{ + {"B0ug", "2023-02-17", "Determine this makefile"}, + {"k291", "2023-02-16", "This is a terminal cli client written in g"}, + {"A93a", "2023-02-15", "ava fallstricke https://www.fs-fmc.kit.edu/sites/default/fil"}, + } + + t := table.New( + table.WithColumns(columns), + table.WithRows(rows), + table.WithFocused(true), + table.WithHeight(10), + table.WithWidth(700), + ) + + s := table.DefaultStyles() + s.Header = headerStyle + s.Selected = selectedStyle + t.SetStyles(s) + + return t +} + +func listUpdate(msg tea.Msg, m mainModel) (tea.Model, tea.Cmd) { + + var cmds []tea.Cmd + var cmd tea.Cmd + + switch msg := msg.(type) { + + case tea.WindowSizeMsg: + m.list.SetWidth(msg.Width) + return m, nil + + case tea.KeyMsg: + switch msg.Type { + case tea.KeyCtrlC: + m.quitting = true + return m, tea.Quit + case tea.KeyEsc: + if m.list.Focused() { + m.list.Blur() + } + } + } + + m.list, cmd = m.list.Update(msg) + cmds = append(cmds, cmd) + return m, tea.Batch(cmds...) +} + +func listView(m mainModel) string { + return baseStyle.Render(m.list.View()) + "\n" +} diff --git a/client/go-tui/scientia-tui.go b/client/go-tui/scientia-tui.go index 8864044..1fe60ed 100644 --- a/client/go-tui/scientia-tui.go +++ b/client/go-tui/scientia-tui.go @@ -19,7 +19,9 @@ package main import ( "fmt" + "github.com/charmbracelet/bubbles/table" "github.com/charmbracelet/bubbles/textarea" + "github.com/charmbracelet/bubbles/viewport" "github.com/charmbracelet/lipgloss" "os" @@ -30,7 +32,7 @@ import ( // the unique identifiers for each action of the initial list of actions const ( ITEM_CREATE_VALUE = "create" - ITEM_LIST_VALUE = "list" + ITEM_LIST_VALUE = "list" ITEM_UPDATE_VALUE = "update" ) @@ -42,10 +44,12 @@ var ( // mainModel Holds all the important stuff // Needs to be extended if a new action is added type mainModel struct { - start list.Model - create textarea.Model - choice string + start list.Model + create textarea.Model + list table.Model + choice string quitting bool + viewport viewport.Model } func (m mainModel) Init() tea.Cmd { @@ -55,12 +59,12 @@ func (m mainModel) Init() tea.Cmd { // Update The main Update method. Decides the correct action update method func (m mainModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch m.choice { - case ITEM_UPDATE_VALUE: - //return quitTextStyle.Render("Update it is") - case ITEM_LIST_VALUE: - //return quitTextStyle.Render("List it is") - case ITEM_CREATE_VALUE: - return createUpdate(msg, m) + case ITEM_UPDATE_VALUE: + //return quitTextStyle.Render("Update it is") + case ITEM_LIST_VALUE: + return listUpdate(msg, m) + case ITEM_CREATE_VALUE: + return createUpdate(msg, m) } return startUpdate(msg, m) } @@ -72,22 +76,22 @@ func (m mainModel) View() string { } switch m.choice { - case ITEM_UPDATE_VALUE: - return quitTextStyle.Render("Update it is") - case ITEM_LIST_VALUE: - return quitTextStyle.Render("List it is") - case ITEM_CREATE_VALUE: - return createView(m) + case ITEM_UPDATE_VALUE: + return quitTextStyle.Render("Update it is") + case ITEM_LIST_VALUE: + return listView(m) + case ITEM_CREATE_VALUE: + return createView(m) } return startView(m) } func main() { - m := mainModel{start: initStart(), create: initCreate()} - p := tea.NewProgram(m) + m := mainModel{start: initStart(), create: initCreate(), list: initList()} + p := tea.NewProgram(m, tea.WithAltScreen()) - if err := p.Start(); err != nil { + if _, err := p.Run(); err != nil { fmt.Println("Error running program:", err) os.Exit(1) } diff --git a/client/go-tui/start.go b/client/go-tui/start.go index 043c219..a2fdb55 100644 --- a/client/go-tui/start.go +++ b/client/go-tui/start.go @@ -37,11 +37,11 @@ var ( helpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1) ) - // item stuff type item struct { title, desc string } + func (i item) Title() string { return i.title } func (i item) Description() string { return i.desc } func (i item) FilterValue() string { return i.title } @@ -76,7 +76,7 @@ func (d itemDelegate) Render(w io.Writer, m list.Model, index int, listItem list } func initStart() list.Model { - items := []list.Item { + items := []list.Item{ item{title: "Create", desc: ITEM_CREATE_VALUE}, item{title: "List", desc: ITEM_LIST_VALUE}, item{title: "Update", desc: ITEM_UPDATE_VALUE}, @@ -101,31 +101,33 @@ func startUpdate(msg tea.Msg, m mainModel) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case tea.WindowSizeMsg: m.start.SetWidth(msg.Width) + //m.viewport.Width = msg.Width + //m.viewport.Height = msg.Height return m, nil case tea.KeyMsg: switch msg.Type { - // esc does close? - case tea.KeyEsc: - return m, nil - case tea.KeyCtrlC: + // esc does close? + case tea.KeyEsc: + return m, nil + case tea.KeyCtrlC: + m.quitting = true + return m, tea.Quit + + case tea.KeyEnter: + i, ok := m.start.SelectedItem().(item) + if ok { + m.choice = i.Description() + } + return m, nil + + case tea.KeyRunes: + switch string(msg.Runes) { + case "q": m.quitting = true return m, tea.Quit - - case tea.KeyEnter: - i, ok := m.start.SelectedItem().(item) - if ok { - m.choice = i.Description() - } - return m, nil - - case tea.KeyRunes: - switch string(msg.Runes) { - case "q": - m.quitting = true - return m, tea.Quit - } } + } } var cmd tea.Cmd -- 2.39.5