mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
refactor map, add padding type
This commit is contained in:
@ -16,6 +16,7 @@ type Input struct {
|
|||||||
Data string
|
Data string
|
||||||
TextFgColor ui.Attribute
|
TextFgColor ui.Attribute
|
||||||
TextBgColor ui.Attribute
|
TextBgColor ui.Attribute
|
||||||
|
padding Padding
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewInput() *Input {
|
func NewInput() *Input {
|
||||||
@ -24,6 +25,7 @@ func NewInput() *Input {
|
|||||||
Label: "input",
|
Label: "input",
|
||||||
TextFgColor: ui.ThemeAttr("par.text.fg"),
|
TextFgColor: ui.ThemeAttr("par.text.fg"),
|
||||||
TextBgColor: ui.ThemeAttr("par.text.bg"),
|
TextBgColor: ui.ThemeAttr("par.text.bg"),
|
||||||
|
padding: Padding{4, 2},
|
||||||
}
|
}
|
||||||
i.Width, i.Height = 30, 3
|
i.Width, i.Height = 30, 3
|
||||||
return i
|
return i
|
||||||
@ -33,7 +35,7 @@ func (i *Input) Buffer() ui.Buffer {
|
|||||||
var cell ui.Cell
|
var cell ui.Cell
|
||||||
buf := i.Block.Buffer()
|
buf := i.Block.Buffer()
|
||||||
|
|
||||||
x := x_padding
|
x := i.padding[0]
|
||||||
for _, ch := range i.Data {
|
for _, ch := range i.Data {
|
||||||
cell = ui.Cell{Ch: ch, Fg: i.TextFgColor, Bg: i.TextBgColor}
|
cell = ui.Cell{Ch: ch, Fg: i.TextFgColor, Bg: i.TextBgColor}
|
||||||
buf.Set(x, 1, cell)
|
buf.Set(x, 1, cell)
|
||||||
|
@ -4,11 +4,7 @@ import (
|
|||||||
ui "github.com/gizak/termui"
|
ui "github.com/gizak/termui"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
type Padding [2]int // x,y padding
|
||||||
x_padding = 4
|
|
||||||
y_padding = 2
|
|
||||||
minWidth = 8
|
|
||||||
)
|
|
||||||
|
|
||||||
type Menu struct {
|
type Menu struct {
|
||||||
ui.Block
|
ui.Block
|
||||||
@ -18,6 +14,7 @@ type Menu struct {
|
|||||||
TextBgColor ui.Attribute
|
TextBgColor ui.Attribute
|
||||||
Selectable bool
|
Selectable bool
|
||||||
CursorPos int
|
CursorPos int
|
||||||
|
padding Padding
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMenu(items []string) *Menu {
|
func NewMenu(items []string) *Menu {
|
||||||
@ -29,8 +26,9 @@ func NewMenu(items []string) *Menu {
|
|||||||
TextBgColor: ui.ThemeAttr("par.text.bg"),
|
TextBgColor: ui.ThemeAttr("par.text.bg"),
|
||||||
Selectable: false,
|
Selectable: false,
|
||||||
CursorPos: 0,
|
CursorPos: 0,
|
||||||
|
padding: Padding{4, 2},
|
||||||
}
|
}
|
||||||
m.Width, m.Height = calcSize(items)
|
m.calcSize()
|
||||||
return m
|
return m
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -38,14 +36,8 @@ func (m *Menu) Buffer() ui.Buffer {
|
|||||||
var cell ui.Cell
|
var cell ui.Cell
|
||||||
buf := m.Block.Buffer()
|
buf := m.Block.Buffer()
|
||||||
|
|
||||||
// override display of items, if given
|
for n, item := range m.displayItems() {
|
||||||
items := m.Items
|
x := m.padding[0]
|
||||||
if len(m.DisplayItems) == len(m.Items) {
|
|
||||||
items = m.DisplayItems
|
|
||||||
}
|
|
||||||
|
|
||||||
for n, item := range items {
|
|
||||||
x := x_padding
|
|
||||||
for _, ch := range item {
|
for _, ch := range item {
|
||||||
// invert bg/fg colors on currently selected row
|
// invert bg/fg colors on currently selected row
|
||||||
if m.Selectable && n == m.CursorPos {
|
if m.Selectable && n == m.CursorPos {
|
||||||
@ -53,7 +45,7 @@ func (m *Menu) Buffer() ui.Buffer {
|
|||||||
} else {
|
} else {
|
||||||
cell = ui.Cell{Ch: ch, Fg: m.TextFgColor, Bg: m.TextBgColor}
|
cell = ui.Cell{Ch: ch, Fg: m.TextFgColor, Bg: m.TextBgColor}
|
||||||
}
|
}
|
||||||
buf.Set(x, n+y_padding, cell)
|
buf.Set(x, n+m.padding[1], cell)
|
||||||
x++
|
x++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -82,17 +74,25 @@ func (m *Menu) NavigationHandlers() {
|
|||||||
ui.Handle("/sys/kbd/q", func(ui.Event) { ui.StopLoop() })
|
ui.Handle("/sys/kbd/q", func(ui.Event) { ui.StopLoop() })
|
||||||
}
|
}
|
||||||
|
|
||||||
// return width and height based on menu items
|
// override display of items, if given
|
||||||
func calcSize(items []string) (w, h int) {
|
func (m *Menu) displayItems() []string {
|
||||||
h = len(items) + (y_padding * 2)
|
if len(m.DisplayItems) == len(m.Items) {
|
||||||
|
return m.DisplayItems
|
||||||
w = minWidth
|
|
||||||
for _, s := range items {
|
|
||||||
if len(s) > w {
|
|
||||||
w = len(s)
|
|
||||||
}
|
}
|
||||||
}
|
return m.Items
|
||||||
w += (x_padding * 2)
|
}
|
||||||
|
|
||||||
return w, h
|
// Set width and height based on menu items
|
||||||
|
func (m *Menu) calcSize() {
|
||||||
|
m.Width = 8 // minimum width
|
||||||
|
|
||||||
|
items := m.displayItems()
|
||||||
|
for _, s := range items {
|
||||||
|
if len(s) > m.Width {
|
||||||
|
m.Width = len(s)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m.Width += (m.padding[0] * 2)
|
||||||
|
m.Height = len(items) + (m.padding[1] * 2)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user