Skip to content

Commit

Permalink
fix merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
RamanaReddy0M committed Feb 1, 2023
1 parent 5c87910 commit 5d42eb7
Show file tree
Hide file tree
Showing 16 changed files with 306 additions and 44 deletions.
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ require (
github.com/brianvoe/gofakeit v3.18.0+incompatible
github.com/cenkalti/backoff v2.2.1+incompatible
github.com/derailed/tview v0.7.2
github.com/fvbommel/sortorder v1.0.2
github.com/gdamore/tcell/v2 v2.5.2
github.com/rs/zerolog v1.28.0
github.com/spf13/cobra v1.6.1
)

require github.com/atotto/clipboard v0.1.4 // indirect
require github.com/atotto/clipboard v0.1.4

require (
github.com/aws/aws-sdk-go-v2/credentials v1.13.8 // indirect
Expand All @@ -32,12 +33,12 @@ require (
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-runewidth v0.0.14 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/sys v0.3.0 // indirect
golang.org/x/term v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
13 changes: 10 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ github.com/derailed/tview v0.7.2 h1:9AyPjBR0yr8t4e+pQMayLhc9AASediouUGpcoDvwy+Q=
github.com/derailed/tview v0.7.2/go.mod h1:XspPZ+rl8rub+JeysbFRbuE55Q61qYI906HpH0zzH0I=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/fvbommel/sortorder v1.0.2 h1:mV4o8B2hKboCdkJm+a7uX/SIpZob4JzUpc5GGnM45eo=
github.com/fvbommel/sortorder v1.0.2/go.mod h1:uk88iVf1ovNn1iLfgUVU2F9o5eO30ui720w+kxuqRs0=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.2.1/go.mod h1:cTTuF84Dlj/RqmaCIV5p4w8uG1zWdk0SF6oBpwHp4fU=
Expand All @@ -56,10 +58,13 @@ github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfC
github.com/lucasb-eyer/go-colorful v1.0.3/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
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-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
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.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-runewidth v0.0.10/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
Expand Down Expand Up @@ -103,8 +108,10 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220318055525-2edf467146b5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.3.0 h1:w8ZOecv6NaNa/zC8944JTU3vz4u6Lagfk4RPQxv92NQ=
golang.org/x/sys v0.3.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210406210042-72f3dc4e9b72/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
Expand Down
58 changes: 58 additions & 0 deletions internal/color/color.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package color

import (
"fmt"
"strconv"
)

const colorFmt = "\x1b[%dm%s\x1b[0m"

// Paint describes a terminal color.
type Paint int

// Colorize returns an ASCII colored string based on given color.
func Colorize(s string, c Paint) string {

if c == 0 {
return s
}
return fmt.Sprintf(colorFmt, c, s)
}

func ColorizeAt(s string, idx int, color string) string {
if len(s) <= idx {
return s
}
//colr := color.New(c).FprintfFunc()

chrs := []rune(s)
left := string(chrs[:idx])

mid := fmt.Sprintf("[-:-:-][%s::b]%s[-:-:-]", color, string(chrs[idx]))
right := string(chrs[idx+1:])
return fmt.Sprintf("%s%s%s", left, mid, right)
}

// ANSIColorize colors a string.
func ANSIColorize(text string, color int) string {
return "\033[38;5;" + strconv.Itoa(color) + "m" + text + "\033[0m"
}

// Highlight colorize bytes at given indices.
func Highlight(bb []byte, ii []int, c int) []byte {
b := make([]byte, 0, len(bb))
for i, j := 0, 0; i < len(bb); i++ {
if j < len(ii) && ii[j] == i {
b = append(b, colorizeByte(bb[i], 209)...)
j++
} else {
b = append(b, bb[i])
}
}

return b
}

func colorizeByte(b byte, color int) []byte {
return []byte(ANSIColorize(string(b), color))
}
2 changes: 1 addition & 1 deletion internal/model/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
)

var Registry = map[string]ResourceMeta{
"ec2": {
"EC2": {
DAO: &dao.EC2{},
Renderer: &render.EC2{},
},
Expand Down
17 changes: 9 additions & 8 deletions internal/render/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package render
import (
"fmt"

"github.com/derailed/tview"
"github.com/one2nc/cloud-lens/internal/aws"
)

Expand All @@ -12,13 +13,13 @@ type EC2 struct {
// Header returns a header row.
func (e EC2) Header() Header {
return Header{
HeaderColumn{Name: "Instance-Id"},
HeaderColumn{Name: "Instance-State"},
HeaderColumn{Name: "Instance-Type"},
HeaderColumn{Name: "Monitoring-State"},
HeaderColumn{Name: "Launch-Time"},
HeaderColumn{Name: "Public-DNS", Wide: true},
HeaderColumn{Name: "Availability-Zone"},
HeaderColumn{Name: "Instance-Id", SortIndicatorIdx: 9, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Instance-State", SortIndicatorIdx: 9, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Instance-Type", SortIndicatorIdx: 9, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Monitoring-State", SortIndicatorIdx: 0, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: false},
HeaderColumn{Name: "Launch-Time", SortIndicatorIdx: 0, Align: tview.AlignLeft, Hide: false, Wide: false, MX: false, Time: true},
HeaderColumn{Name: "Public-DNS", SortIndicatorIdx: 0, Align: tview.AlignLeft, Hide: false, Wide: true, MX: false, Time: false},
HeaderColumn{Name: "Availability-Zone", SortIndicatorIdx: -1, Align: tview.AlignCenter, Hide: false, Wide: false, MX: false, Time: false},
}
}

Expand All @@ -35,7 +36,7 @@ func (e EC2) Render(o interface{}, ns string, row *Row) error {
ec2Resp.InstanceState,
ec2Resp.InstanceType,
ec2Resp.MonitoringState,
ec2Resp.LaunchTime,
ec2Resp.LaunchTime,
ec2Resp.PublicDNS,
ec2Resp.AvailabilityZone,
}
Expand Down
23 changes: 18 additions & 5 deletions internal/render/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import (
)

// HeaderColumn represent a table header.
// SortIndicatorIdx position of alphabet in header name to highlight
// Set SortIndicatorIdx to -1 to ignore highlighting any alphabet in header name
type HeaderColumn struct {
Name string
Align int
Hide bool
Wide bool
Time bool
Name string
SortIndicatorIdx int
Align int
Hide bool
Wide bool
MX bool
Time bool
}

// Clone copies a header.
Expand Down Expand Up @@ -112,6 +116,15 @@ func (h Header) Customize(cols []string, wide bool) Header {
return cc
}

// IsMetricsCol checks if given column index represents metrics.
func (h Header) IsMetricsCol(col int) bool {
if col < 0 || col >= len(h) {
return false
}

return h[col].MX
}

// IsTimeCol checks if given column index represents a timestamp.
func (h Header) IsTimeCol(col int) bool {
if col < 0 || col >= len(h) {
Expand Down
42 changes: 42 additions & 0 deletions internal/render/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package render

func runesToNum(rr []rune) int64 {
var r int64
var m int64 = 1
for i := len(rr) - 1; i >= 0; i-- {
v := int64(rr[i] - '0')
r += v * m
m *= 10
}

return r
}

func durationToSeconds(duration string) int64 {
if len(duration) == 0 {
return 0
}

num := make([]rune, 0, 5)
var n, m int64
for _, r := range duration {
switch r {
case 'y':
m = 365 * 24 * 60 * 60
case 'd':
m = 24 * 60 * 60
case 'h':
m = 60 * 60
case 'm':
m = 60
case 's':
m = 1
default:
num = append(num, r)
continue
}
n, num = n+runesToNum(num)*m, num[:0]
}

return n
}
31 changes: 30 additions & 1 deletion internal/render/row.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package render

import (
"strings"

"github.com/fvbommel/sortorder"
)

// Fields represents a collection of row fields.
type Fields []string

Expand Down Expand Up @@ -100,4 +106,27 @@ func (rr Rows) Find(id string) (int, bool) {
}

return 0, false
}
}

// ----------------------------------------------------------------------------
// Helpers...

// Less return true if c1 < c2.
func Less(isNumber, isDuration bool, id1, id2, v1, v2 string) bool {
var less bool
switch {
case isNumber:
v1, v2 = strings.Replace(v1, ",", "", -1), strings.Replace(v2, ",", "", -1)
less = sortorder.NaturalLess(v1, v2)
case isDuration:
d1, d2 := durationToSeconds(v1), durationToSeconds(v2)
less = d1 <= d2
default:
less = sortorder.NaturalLess(v1, v2)
}
if v1 == v2 {
return sortorder.NaturalLess(id1, id2)
}

return less
}
54 changes: 51 additions & 3 deletions internal/render/row_event.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package render

import "sort"

const (
// EventUnchanged notifies listener resource has not changed.
EventUnchanged ResEvent = 1 << iota
Expand Down Expand Up @@ -37,8 +39,8 @@ func NewRowEvent(kind ResEvent, row Row) RowEvent {
// Customize returns a new subset based on the given column indices.
func (r RowEvent) Customize(cols []int) RowEvent {
return RowEvent{
Kind: r.Kind,
Row: r.Row.Customize(cols),
Kind: r.Kind,
Row: r.Row.Customize(cols),
}
}

Expand Down Expand Up @@ -107,4 +109,50 @@ func (r RowEvents) FindIndex(id string) (int, bool) {
}

return 0, false
}
}

// Sort rows based on column index and order.
func (r RowEvents) Sort(sortCol int, isDuration, numCol, asc bool) {
if sortCol == -1 {
return
}

t := RowEventSorter{
Events: r,
Index: sortCol,
Asc: asc,
IsNumber: numCol,
IsDuration: isDuration,
}
sort.Sort(t)
}

// ----------------------------------------------------------------------------

// RowEventSorter sorts row events by a given colon.
type RowEventSorter struct {
Events RowEvents
Index int
IsNumber bool
IsDuration bool
Asc bool
}

func (r RowEventSorter) Len() int {
return len(r.Events)
}

func (r RowEventSorter) Swap(i, j int) {
r.Events[i], r.Events[j] = r.Events[j], r.Events[i]
}

func (r RowEventSorter) Less(i, j int) bool {
f1, f2 := r.Events[i].Row.Fields, r.Events[j].Row.Fields
id1, id2 := r.Events[i].Row.ID, r.Events[j].Row.ID
less := Less(r.IsNumber, r.IsDuration, id1, id2, f1[r.Index], f2[r.Index])
if r.Asc {
return less
}

return !less
}
4 changes: 2 additions & 2 deletions internal/ui/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ func toMnemonic(s string) string {
}

func formatNSMenu(i int, name string) string {
return fmt.Sprintf("<%d> %s", i, name)
return fmt.Sprintf("[pink::]<%d>[black::] %s", i, name)
}

func formatPlainMenu(h model.MenuHint, size int) string {
famt := "%" + strconv.Itoa(size+2) + "s %s"
famt := "[blue::]%" + strconv.Itoa(size+2) + "s[white::d] %s"
return fmt.Sprintf(famt, toMnemonic(h.Mnemonic), h.Description)
}
2 changes: 1 addition & 1 deletion internal/ui/select_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (s *SelectTable) selectionChanged(r, c int) {
return
}
if cell := s.GetCell(r, c); cell != nil {
s.SetSelectedStyle(tcell.StyleDefault.Foreground(s.fgColor).Background(cell.Color).Attributes(tcell.AttrBold))
s.SetSelectedStyle(tcell.StyleDefault.Foreground(tcell.ColorBlack.TrueColor()).Background(tcell.ColorAqua).Attributes(tcell.AttrBold))
}
}

Expand Down
Loading

0 comments on commit 5d42eb7

Please sign in to comment.