gdamore/tcell

set background of a row

mortezadadgar opened this issue · 2 comments

Hey,
sorry asking this as a issue there's no discussion to ask questions like this; as a personal project i have been trying to write a file explorer program to list files or directory based of their size so all i'm trying to is to set the background of a row in ui like this
512KiB Go <- I want the current selection row to have a separate background as others
128KiB C
I can set the background of size text and file/dirs individually which results into:
image
as you can see there's a space between them which is not affected by changing the style as they have different x position
this is the function that i used to draw this tree:

func drawTree(dirs []fileAttr, screen tcell.Screen) {
	styleSizes := tcell.StyleDefault.Background(tcell.ColorBlack).Foreground(tcell.ColorYellow).Bold(true)

	var y int
	var style tcell.Style
	for i, dir := range dirs {
		var size string
		if i == currentSel {
			style = style.Background(tcell.ColorWhite).Foreground(tcell.ColorBlack)
		} else {
			style = style.Background(tcell.ColorBlack).Foreground(tcell.ColorWhite)
		}
		size = strconv.Itoa(dir.size / 1024 / 1024)
		drawText(screen, 4, y, style, "MiB")
		drawText(screen, 0, y, styleSizes, size)
		drawText(screen, 10, y, style, dir.name)
		y++
	}
}

one way i can think of is to change x position of remaining spaces between 4 and 10 but that would be so hacky

If you want to only change the style without changing the text you can do that. I thought I had created a SetStyle API, but it looks for setting the style on a given cell, but it looks like I didn't for some reason.

Anyway, you can do GetContent() , and then change the Style return value, and pass the results back to SetContent(). You can just iterate over all the cells.

I'll see if I can provide some better convenience functions for this, because changing the style seems super useful.

(I can also see wanting to have an API to quickly change the style of a region. So I might look at that too.)

This is really a duplicate of #579 ... so I'm closing it as such.

Note that I do intend to implement this functionality, which is why I've made some other changes to reduce code duplicate in the repo today.