Skip to content

Commit

Permalink
initial implementation (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasparada authored Feb 23, 2022
1 parent c580544 commit 09c90ee
Show file tree
Hide file tree
Showing 7 changed files with 1,057 additions and 2 deletions.
226 changes: 226 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
go-bubble-table

# Created by https://www.toptal.com/developers/gitignore/api/go,macos,linux,windows,dotenv,visualstudiocode,goland,vim,git
# Edit at https://www.toptal.com/developers/gitignore?templates=go,macos,linux,windows,dotenv,visualstudiocode,goland,vim,git

### dotenv ###
.env

### Git ###
# Created by git for backups. To disable backups in Git:
# $ git config --global mergetool.keepBackup false
*.orig

# Created by git when using merge tools for conflicts
*.BACKUP.*
*.BASE.*
*.LOCAL.*
*.REMOTE.*
*_BACKUP_*.txt
*_BASE_*.txt
*_LOCAL_*.txt
*_REMOTE_*.txt

### Go ###
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
Expand All @@ -13,3 +40,202 @@

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

### Go Patch ###
/vendor/
/Godeps/

### Goland ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm, Rider and Goland
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

# User-specific stuff
.idea/**/workspace.xml
.idea/**/tasks.xml
.idea/**/usage.statistics.xml
.idea/**/dictionaries
.idea/**/shelf

# AWS User-specific
.idea/**/aws.xml

# Generated files
.idea/**/contentModel.xml

# Sensitive or high-churn files
.idea/**/dataSources/
.idea/**/dataSources.ids
.idea/**/dataSources.local.xml
.idea/**/sqlDataSources.xml
.idea/**/dynamic.xml
.idea/**/uiDesigner.xml
.idea/**/dbnavigator.xml

# Gradle
.idea/**/gradle.xml
.idea/**/libraries

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
# auto-import.
# .idea/artifacts
# .idea/compiler.xml
# .idea/jarRepositories.xml
# .idea/modules.xml
# .idea/*.iml
# .idea/modules
# *.iml
# *.ipr

# CMake
cmake-build-*/

# Mongo Explorer plugin
.idea/**/mongoSettings.xml

# File-based project format
*.iws

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

# Editor-based Rest Client
.idea/httpRequests

# Ignores the whole .idea folder and all .iml files
.idea/

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

### Linux ###
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon


# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

### Vim ###
# Swap
[._]*.s[a-v][a-z]
!*.svg # comment out if you don't need vector files
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]

# Session
Session.vim
Sessionx.vim

# Temporary
.netrwhist
# Auto-generated tag files
tags
# Persistent undo
[._]*.un~

### VisualStudioCode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
!.vscode/*.code-snippets

# Local History for Visual Studio Code
.history/

# Built Visual Studio Code Extensions
*.vsix

### VisualStudioCode Patch ###
# Ignore all local history of files
.history
.ionide

# Support for Project snippet scope

### Windows ###
# Windows thumbnail cache files
Thumbs.db
Thumbs.db:encryptable
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
[Dd]esktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msix
*.msm
*.msp

# Windows shortcuts
*.lnk

# End of https://www.toptal.com/developers/gitignore/api/go,macos,linux,windows,dotenv,visualstudiocode,goland,vim,git
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# go-bubble-table
TUI table component for Bubbletea.
# Table

[![Go Reference](https://pkg.go.dev/badge/github.com/calyptia/go-bubble-table.svg)](https://pkg.go.dev/github.com/calyptia/go-bubble-table)

TUI table component for [Bubble Tea](https://github.com/charmbracelet/bubbletea) applications.

This component is used in the [Calyptia Cloud CLI](https://github.com/calyptia/cloud-cli) under the `top` command.

https://user-images.githubusercontent.com/7969166/155205138-e205b38b-3631-43b2-a369-1f57914da838.mp4

## Installation

```bash
go get github.com/calyptia/go-bubble-table
```

## Example

For an example please take a look at the `/example` directory.
81 changes: 81 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
"fmt"
"os"

"github.com/brianvoe/gofakeit/v6"
table "github.com/calyptia/go-bubble-table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"golang.org/x/term"
)

func main() {
err := tea.NewProgram(initialModel(), tea.WithAltScreen()).Start()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

var (
styleDoc = lipgloss.NewStyle().Padding(1)
)

func initialModel() model {
w, h, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil {
w = 80
h = 24
}
top, right, bottom, left := styleDoc.GetPadding()
w = w - left - right
h = h - top - bottom
tbl := table.New([]string{"ID", "NAME", "AGE", "CITY"}, w, h)
rows := make([]table.Row, 100)
for i := 0; i < 100; i++ {
rows[i] = table.SimpleRow{
i,
gofakeit.Name(),
gofakeit.Number(0, 122),
gofakeit.City(),
}
}
tbl.SetRows(rows)
return model{table: tbl}
}

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) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
top, right, bottom, left := styleDoc.GetPadding()
m.table.SetSize(
msg.Width-left-right,
msg.Height-top-bottom,
)
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
return m, tea.Quit
}
}

var cmd tea.Cmd
m.table, cmd = m.table.Update(msg)
return m, cmd
}

func (m model) View() string {
return styleDoc.Render(
m.table.View(),
)
}
25 changes: 25 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module github.com/calyptia/go-bubble-table

go 1.17

require (
github.com/brianvoe/gofakeit/v6 v6.14.5
github.com/charmbracelet/bubbles v0.10.3
github.com/charmbracelet/bubbletea v0.20.0
github.com/charmbracelet/lipgloss v0.5.0
github.com/juju/ansiterm v0.0.0-20210929141451-8b71cc96ebdc
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211
)

require (
github.com/containerd/console v1.0.3 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/lunixbochs/vtclean v1.0.0 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
)
Loading

0 comments on commit 09c90ee

Please sign in to comment.