1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
|
package views
import (
"fmt"
"strings"
)
type SplitType uint8
const (
STVert = 0
STHoriz = 1
STUndef = 2
)
var idcounter uint64
// NewID returns a new unique id
func NewID() uint64 {
idcounter++
return idcounter
}
// A View is a size and location of a split
type View struct {
X, Y int
W, H int
}
// A Node describes a split in the tree
// If a node is a leaf node then it corresponds to a buffer that is being
// displayed otherwise it has a number of children of the opposite type
// (vertical splits have horizontal children and vice versa)
type Node struct {
View
Kind SplitType
parent *Node
children []*Node
// Nodes can be marked as non resizable if they shouldn't be rescaled
// when the terminal window is resized or when a new split is added
// Only the splits on the edges of the screen can be marked as non resizable
canResize bool
// A node may also be marked with proportional scaling. This means that when
// the window is resized the split maintains its proportions
propScale bool
// Defines the proportion of the screen this node should take up if propScale is
// on
propW, propH float64
// The id is unique for each leaf node and provides a way to keep track of a split
// The id cannot be 0
id uint64
}
// NewNode returns a new node with the given specifications
func NewNode(Kind SplitType, x, y, w, h int, parent *Node, id uint64) *Node {
n := new(Node)
n.Kind = Kind
n.canResize = true
n.propScale = true
n.X, n.Y, n.W, n.H = x, y, w, h
n.children = make([]*Node, 0)
n.parent = parent
n.id = id
if parent != nil {
n.propW, n.propH = float64(w)/float64(parent.W), float64(h)/float64(parent.H)
} else {
n.propW, n.propH = 1, 1
}
return n
}
// NewRoot returns an empty Node with a size and location
// The type of the node will be determined by the first action on the node
// In other words, a lone split is neither horizontal nor vertical, it only
// becomes one or the other after a vsplit or hsplit is made
func NewRoot(x, y, w, h int) *Node {
n1 := NewNode(STUndef, x, y, w, h, nil, NewID())
return n1
}
// IsLeaf returns if this node is a leaf node
func (n *Node) IsLeaf() bool {
return len(n.children) == 0
}
// ID returns this node's id or 0 if it is not viewable
func (n *Node) ID() uint64 {
if n.IsLeaf() {
return n.id
}
return 0
}
// CanResize returns if this node can be resized
func (n *Node) CanResize() bool {
return n.canResize
}
// PropScale returns if this node is proportionally scaled
func (n *Node) PropScale() bool {
return n.propScale
}
// SetResize sets the resize flag
func (n *Node) SetResize(b bool) {
n.canResize = b
}
// SetPropScale sets the propScale flag
func (n *Node) SetPropScale(b bool) {
n.propScale = b
}
// Children returns this node's children
func (n *Node) Children() []*Node {
return n.children
}
// GetNode returns the node with the given id in the tree of children
// that this node has access to or nil if the node with that id cannot be found
func (n *Node) GetNode(id uint64) *Node {
if n.id == id && n.IsLeaf() {
return n
}
for _, c := range n.children {
if c.id == id && c.IsLeaf() {
return c
}
gc := c.GetNode(id)
if gc != nil {
return gc
}
}
return nil
}
func (n *Node) vResizeSplit(i int, size int) bool {
if i < 0 || i >= len(n.children) {
return false
}
var c1, c2 *Node
if i == len(n.children)-1 {
c1, c2 = n.children[i-1], n.children[i]
} else {
c1, c2 = n.children[i], n.children[i+1]
}
toth := c1.H + c2.H
if size >= toth {
return false
}
c2.Y = c1.Y + size
c1.Resize(c1.W, size)
c2.Resize(c2.W, toth-size)
n.markSizes()
n.alignSizes(n.W, n.H)
return true
}
func (n *Node) hResizeSplit(i int, size int) bool {
if i < 0 || i >= len(n.children) {
return false
}
var c1, c2 *Node
if i == len(n.children)-1 {
c1, c2 = n.children[i-1], n.children[i]
} else {
c1, c2 = n.children[i], n.children[i+1]
}
totw := c1.W + c2.W
if size >= totw {
return false
}
c2.X = c1.X + size
c1.Resize(size, c1.H)
c2.Resize(totw-size, c2.H)
n.markSizes()
n.alignSizes(n.W, n.H)
return true
}
// ResizeSplit resizes a certain split to a given size
func (n *Node) ResizeSplit(size int) bool {
// TODO: `size < 0` does not work for some reason
if size <= 0 {
return false
}
if len(n.parent.children) <= 1 {
// cannot resize a lone node
return false
}
ind := 0
for i, c := range n.parent.children {
if c.id == n.id {
ind = i
}
}
if n.parent.Kind == STVert {
return n.parent.vResizeSplit(ind, size)
}
return n.parent.hResizeSplit(ind, size)
}
// Resize sets this node's size and resizes all children accordingly
func (n *Node) Resize(w, h int) {
n.W, n.H = w, h
if n.IsLeaf() {
return
}
x, y := n.X, n.Y
totw, toth := 0, 0
for _, c := range n.children {
cW := int(float64(w) * c.propW)
cH := int(float64(h) * c.propH)
c.X, c.Y = x, y
c.Resize(cW, cH)
if n.Kind == STHoriz {
x += cW
totw += cW
} else {
y += cH
toth += cH
}
}
n.alignSizes(totw, toth)
}
func (n *Node) alignSizes(totw, toth int) {
// Make sure that there are no off-by-one problems with the rounding
// of the sizes by making the final split fill the screen
if n.Kind == STVert && toth != n.H {
last := n.children[len(n.children)-1]
last.Resize(last.W, last.H+n.H-toth)
} else if n.Kind == STHoriz && totw != n.W {
last := n.children[len(n.children)-1]
last.Resize(last.W+n.W-totw, last.H)
}
}
// Resets all proportions for children
func (n *Node) markSizes() {
for _, c := range n.children {
c.propW = float64(c.W) / float64(n.W)
c.propH = float64(c.H) / float64(n.H)
c.markSizes()
}
}
func (n *Node) markResize() {
n.markSizes()
n.Resize(n.W, n.H)
}
// vsplits a vertical split and returns the id of the new split
func (n *Node) vVSplit(right bool) uint64 {
ind := 0
for i, c := range n.parent.children {
if c.id == n.id {
ind = i
}
}
return n.parent.hVSplit(ind, right)
}
// hsplits a horizontal split
func (n *Node) hHSplit(bottom bool) uint64 {
ind := 0
for i, c := range n.parent.children {
if c.id == n.id {
ind = i
}
}
return n.parent.vHSplit(ind, bottom)
}
// Returns the size of the non-resizable area and the number of resizable
// splits
func (n *Node) getResizeInfo(h bool) (int, int) {
numr := 0
numnr := 0
nonr := 0
for _, c := range n.children {
if !c.CanResize() {
if h {
nonr += c.H
} else {
nonr += c.W
}
numnr++
} else {
numr++
}
}
// if there are no resizable splits make them all resizable
if numr == 0 {
numr = numnr
}
return nonr, numr
}
func (n *Node) applyNewSize(size int, h bool) {
a := n.X
if h {
a = n.Y
}
for _, c := range n.children {
if h {
c.Y = a
} else {
c.X = a
}
if c.CanResize() {
if h {
c.Resize(c.W, size)
} else {
c.Resize(size, c.H)
}
}
if h {
a += c.H
} else {
a += c.H
}
}
n.markResize()
}
// hsplits a vertical split
func (n *Node) vHSplit(i int, right bool) uint64 {
if n.IsLeaf() {
newid := NewID()
hn1 := NewNode(STHoriz, n.X, n.Y, n.W, n.H/2, n, n.id)
hn2 := NewNode(STHoriz, n.X, n.Y+hn1.H, n.W, n.H/2, n, newid)
if !right {
hn1.id, hn2.id = hn2.id, hn1.id
}
n.children = append(n.children, hn1, hn2)
n.markResize()
return newid
} else {
nonrh, numr := n.getResizeInfo(true)
// size of resizable area
height := (n.H - nonrh) / (numr + 1)
newid := NewID()
hn := NewNode(STHoriz, n.X, 0, n.W, height, n, newid)
// insert the node into the correct slot
n.children = append(n.children, nil)
inspos := i
if right {
inspos++
}
copy(n.children[inspos+1:], n.children[inspos:])
n.children[inspos] = hn
n.applyNewSize(height, true)
return newid
}
}
// vsplits a horizontal split
func (n *Node) hVSplit(i int, right bool) uint64 {
if n.IsLeaf() {
newid := NewID()
vn1 := NewNode(STVert, n.X, n.Y, n.W/2, n.H, n, n.id)
vn2 := NewNode(STVert, n.X+vn1.W, n.Y, n.W/2, n.H, n, newid)
if !right {
vn1.id, vn2.id = vn2.id, vn1.id
}
n.children = append(n.children, vn1, vn2)
n.markResize()
return newid
} else {
nonrw, numr := n.getResizeInfo(false)
width := (n.W - nonrw) / (numr + 1)
newid := NewID()
vn := NewNode(STVert, 0, n.Y, width, n.H, n, newid)
// Inser the node into the correct slot
n.children = append(n.children, nil)
inspos := i
if right {
inspos++
}
copy(n.children[inspos+1:], n.children[inspos:])
n.children[inspos] = vn
n.applyNewSize(width, false)
return newid
}
}
// HSplit creates a horizontal split and returns the id of the new split
// bottom specifies if the new split should be created on the top or bottom
// of the current split
func (n *Node) HSplit(bottom bool) uint64 {
if !n.IsLeaf() {
return 0
}
if n.Kind == STUndef {
n.Kind = STVert
}
if n.Kind == STVert {
return n.vHSplit(0, bottom)
}
return n.hHSplit(bottom)
}
// VSplit creates a vertical split and returns the id of the new split
// right specifies if the new split should be created on the right or left
// of the current split
func (n *Node) VSplit(right bool) uint64 {
if !n.IsLeaf() {
return 0
}
if n.Kind == STUndef {
n.Kind = STHoriz
}
if n.Kind == STVert {
return n.vVSplit(right)
}
return n.hVSplit(0, right)
}
// unsplits the child of a split
func (n *Node) unsplit(i int) {
copy(n.children[i:], n.children[i+1:])
n.children[len(n.children)-1] = nil
n.children = n.children[:len(n.children)-1]
h := n.Kind == STVert
nonrs, numr := n.getResizeInfo(h)
if numr == 0 {
// This means that this was the last child
// The parent will get cleaned up in the next iteration and
// will resolve all sizing issues with its parent
return
}
size := (n.W - nonrs) / numr
if h {
size = (n.H - nonrs) / numr
}
n.applyNewSize(size, h)
}
// Unsplit deletes this split and resizes everything
// else accordingly
func (n *Node) Unsplit() bool {
if !n.IsLeaf() || n.parent == nil {
return false
}
ind := 0
for i, c := range n.parent.children {
if c.id == n.id {
ind = i
}
}
n.parent.unsplit(ind)
if n.parent.IsLeaf() {
return n.parent.Unsplit()
}
n.parent.flatten()
return true
}
// flattens the tree by removing unnecessary intermediate parents that have only one child
// and handles the side effect of it
func (n *Node) flatten() {
if n.parent == nil || len(n.children) != 1 {
return
}
ind := 0
for i, c := range n.parent.children {
if c.id == n.id {
ind = i
}
}
// Replace current node with its child node to remove chained parent
successor := n.children[0]
n.parent.children[ind] = successor
successor.parent = n.parent
// Maintain the tree in a consistent state: any child node's kind (horiz vs vert)
// should be the opposite of its parent's kind.
if successor.IsLeaf() {
successor.Kind = n.Kind
} else {
// If the successor node has children, that means it is a chained parent as well.
// Therefore it can be replaced by its own children.
origsize := len(n.parent.children)
// Let's say we have 5 children and want to replace [2] with its children [a] [b] [c]
// [0] [1] [2] [3] [4] --> [0] [1] [a] [b] [c] [3] [4]
// insertcount will be `3 - 1 = 2` in this case
insertcount := len(successor.children) - 1
n.parent.children = append(n.parent.children, make([]*Node, insertcount)...)
copy(n.parent.children[ind+insertcount+1:], n.parent.children[ind+1:origsize])
copy(n.parent.children[ind:], successor.children)
for i := 0; i < len(successor.children); i++ {
n.parent.children[ind+i].parent = n.parent
}
}
// Update propW and propH since the parent of the children has been updated,
// so the children have new siblings
n.parent.markSizes()
}
// String returns the string form of the node and all children (used for debugging)
func (n *Node) String() string {
var strf func(n *Node, ident int) string
strf = func(n *Node, ident int) string {
marker := "|"
if n.Kind == STHoriz {
marker = "-"
}
str := fmt.Sprint(strings.Repeat("\t", ident), marker, n.View, n.id)
if n.IsLeaf() {
str += "🍁"
}
str += "\n"
for _, c := range n.children {
str += strf(c, ident+1)
}
return str
}
return strf(n, 0)
}
|