|
| 1 | +package style |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "text/tabwriter" |
| 9 | + |
| 10 | + "github.com/charmbracelet/bubbles/table" |
| 11 | + tea "github.com/charmbracelet/bubbletea" |
| 12 | + "github.com/charmbracelet/lipgloss" |
| 13 | + "github.com/sourcegraph/sourcegraph/lib/errors" |
| 14 | + "golang.design/x/clipboard" |
| 15 | +) |
| 16 | + |
| 17 | +var usageTableStyle = lipgloss.NewStyle(). |
| 18 | + BorderStyle(lipgloss.NormalBorder()). |
| 19 | + BorderForeground(lipgloss.Color("240")) |
| 20 | + |
| 21 | +type usageTableModel struct { |
| 22 | + table table.Model |
| 23 | +} |
| 24 | + |
| 25 | +func (m usageTableModel) Init() tea.Cmd { return nil } |
| 26 | + |
| 27 | +func (m usageTableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 28 | + var cmd tea.Cmd |
| 29 | + switch msg := msg.(type) { |
| 30 | + case tea.KeyMsg: |
| 31 | + switch msg.String() { |
| 32 | + case "esc", "q", "ctrl+c": |
| 33 | + return m, tea.Quit |
| 34 | + case "c": |
| 35 | + m.copyRowToClipboard(m.table.SelectedRow()) |
| 36 | + copiedMessage := lipgloss.NewStyle(). |
| 37 | + Foreground(lipgloss.Color("#32CD32")). |
| 38 | + Render(fmt.Sprintf( |
| 39 | + "Copied usage data for '%s' to clipboard", |
| 40 | + m.table.SelectedRow()[0], |
| 41 | + )) |
| 42 | + return m, tea.Batch( |
| 43 | + tea.Printf( |
| 44 | + copiedMessage, |
| 45 | + ), |
| 46 | + ) |
| 47 | + case "C": |
| 48 | + tmpDir := os.TempDir() |
| 49 | + filePath := filepath.Join(tmpDir, "usage-dump.txt") |
| 50 | + m.dump(m.table.Rows(), filePath) |
| 51 | + savedMessage := lipgloss.NewStyle(). |
| 52 | + Foreground(lipgloss.Color("#32CD32")). |
| 53 | + Render(fmt.Sprintf( |
| 54 | + "saved usage data to %s", |
| 55 | + filePath, |
| 56 | + )) |
| 57 | + return m, tea.Batch( |
| 58 | + tea.Printf( |
| 59 | + savedMessage, |
| 60 | + ), |
| 61 | + ) |
| 62 | + } |
| 63 | + } |
| 64 | + m.table, cmd = m.table.Update(msg) |
| 65 | + return m, cmd |
| 66 | +} |
| 67 | + |
| 68 | +func (m usageTableModel) View() string { |
| 69 | + s := "\n > Press 'j' and 'k' to go up and down\n" |
| 70 | + s += " > Press 'c' to copy highlighted row to clipboard\n" |
| 71 | + s += " > Press 'C' to copy all rows to a file\n" |
| 72 | + s += " > Press 'q' to quit\n\n" |
| 73 | + s += usageTableStyle.Render(m.table.View()) + "\n" |
| 74 | + return s |
| 75 | +} |
| 76 | + |
| 77 | +func (m usageTableModel) dump(rows []table.Row, filePath string) error { |
| 78 | + dumpFile, err := os.Create(filePath) |
| 79 | + if err != nil { |
| 80 | + return errors.Wrap(err, "error while creating new file") |
| 81 | + } |
| 82 | + defer dumpFile.Close() |
| 83 | + |
| 84 | + tw := tabwriter.NewWriter(dumpFile, 5, 0, 3, ' ', 0) |
| 85 | + defer tw.Flush() |
| 86 | + |
| 87 | + // default to docker terms |
| 88 | + headers := []string{ |
| 89 | + "Name", |
| 90 | + "Cores", |
| 91 | + "Usage", |
| 92 | + "Memory", |
| 93 | + "Usage", |
| 94 | + } |
| 95 | + |
| 96 | + // kubernetes rows will always have 6 items |
| 97 | + // change column headers to reflect k8s terms |
| 98 | + if len(rows[0]) == 7 { |
| 99 | + headers = []string{ |
| 100 | + "Name", |
| 101 | + "Cores", |
| 102 | + "Usage", |
| 103 | + "Memory", |
| 104 | + "Usage", |
| 105 | + "Storage", |
| 106 | + "Usage", |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + fmt.Fprintf(tw, strings.Join(headers, "\t")+"\n") |
| 111 | + for _, row := range rows { |
| 112 | + values := []string{row[0], row[1], row[2], row[3], row[4]} |
| 113 | + if len(row) == 7 { |
| 114 | + values = append(values, row[5]) |
| 115 | + values = append(values, row[6]) |
| 116 | + } |
| 117 | + fmt.Fprintf(tw, strings.Join(values, "\t")+"\n") |
| 118 | + } |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +func (m usageTableModel) copyRowToClipboard(row table.Row) { |
| 123 | + var containerInfo string |
| 124 | + |
| 125 | + // default to docker headers |
| 126 | + headers := []string{ |
| 127 | + "Name", |
| 128 | + "Cores", |
| 129 | + "Usage", |
| 130 | + "Memory", |
| 131 | + "Usage", |
| 132 | + } |
| 133 | + |
| 134 | + // kubernetes rows will always have 6 items |
| 135 | + // change column headers to reflect k8s terms |
| 136 | + if len(row) == 7 { |
| 137 | + headers = []string{ |
| 138 | + "Name", |
| 139 | + "Cores", |
| 140 | + "Usage", |
| 141 | + "Memory", |
| 142 | + "Usage", |
| 143 | + "Storage", |
| 144 | + "Usage", |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + for i, header := range headers { |
| 149 | + containerInfo += fmt.Sprintf("%s: %s\n", header, row[i]) |
| 150 | + } |
| 151 | + |
| 152 | + clipboard.Write(clipboard.FmtText, []byte(containerInfo)) |
| 153 | +} |
| 154 | + |
| 155 | +func UsageTable(columns []table.Column, rows []table.Row) { |
| 156 | + t := table.New( |
| 157 | + table.WithColumns(columns), |
| 158 | + table.WithRows(rows), |
| 159 | + table.WithFocused(true), |
| 160 | + table.WithHeight(14), |
| 161 | + ) |
| 162 | + |
| 163 | + s := table.DefaultStyles() |
| 164 | + s.Header = s.Header. |
| 165 | + BorderStyle(lipgloss.NormalBorder()). |
| 166 | + BorderForeground(lipgloss.Color("240")). |
| 167 | + BorderBottom(true). |
| 168 | + Bold(false) |
| 169 | + s.Selected = s.Selected. |
| 170 | + Foreground(lipgloss.Color("229")). |
| 171 | + Background(lipgloss.Color("57")). |
| 172 | + Bold(false) |
| 173 | + t.SetStyles(s) |
| 174 | + |
| 175 | + m := usageTableModel{t} |
| 176 | + if _, err := tea.NewProgram(m).Run(); err != nil { |
| 177 | + fmt.Println("Error running program:", err) |
| 178 | + os.Exit(1) |
| 179 | + } |
| 180 | +} |
0 commit comments