File: pages.go

package info (click to toggle)
bombadillo 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 412 kB
  • sloc: makefile: 59
file content (114 lines) | stat: -rw-r--r-- 3,154 bytes parent folder | download
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
package main

import (
	"fmt"
)

//------------------------------------------------\\
// + + +             T Y P E S               + + + \\
//--------------------------------------------------\\

// Pages is a struct that represents the history of the client.
// It functions as a container for the pages (history array) and
// tracks the current history length and location.
type Pages struct {
	Position int
	Length   int
	History  [20]Page
}

//------------------------------------------------\\
// + + +           R E C E I V E R S         + + + \\
//--------------------------------------------------\\

// NavigateHistory takes a positive or negative integer
// and updates the current history position. Checks are done
// to make sure that the position moved to is a valid history
// location. Returns an error or nil.
func (p *Pages) NavigateHistory(qty int) error {
	newPosition := p.Position + qty
	if newPosition < 0 {
		return fmt.Errorf("You are already at the beginning of history")
	} else if newPosition > p.Length-1 {
		return fmt.Errorf("Your way is blocked by void, there is nothing forward")
	}

	p.Position = newPosition
	return nil
}

// Add gets passed a Page, which gets added to the history
// array. Add also updates the current length and position
// of the Pages struct to which it belongs. Add also shifts
// off array items if necessary.
func (p *Pages) Add(pg Page) {
	if p.Position == p.Length-1 && p.Length < len(p.History) {
		p.History[p.Length] = pg
		p.Length++
		p.Position++
	} else if p.Position == p.Length-1 && p.Length == 20 {
		for x := 1; x < len(p.History); x++ {
			p.History[x-1] = p.History[x]
		}
		p.History[len(p.History)-1] = pg
	} else {
		p.Position++
		p.Length = p.Position + 1
		p.History[p.Position] = pg
	}
}

// Render wraps the content for the current page and returns
// the page content as a string slice
func (p *Pages) Render(termHeight, termWidth, maxWidth int, color bool) []string {
	if p.Length < 1 {
		return make([]string, 0)
	}
	pos := p.History[p.Position].ScrollPosition
	prev := len(p.History[p.Position].WrappedContent)

	if termWidth != p.History[p.Position].WrapWidth || p.History[p.Position].Color != color {
		p.History[p.Position].WrapContent(termWidth, maxWidth, color)
	}

	now := len(p.History[p.Position].WrappedContent)
	if prev > now {
		diff := prev - now
		pos = pos - diff
	} else if prev < now {
		diff := now - prev
		pos = pos + diff
		if pos > now-termHeight {
			pos = now - termHeight
		}
	}

	if pos < 0 || now < termHeight-3 {
		pos = 0
	}

	p.History[p.Position].ScrollPosition = pos

	return p.History[p.Position].WrappedContent[pos:]
}

func (p *Pages) CopyHistory(pos int) error {
	if p.Length < 2 || pos > p.Position {
		return fmt.Errorf("There are not enough history locations available")
	}
	if pos < 0 {
		pos = p.Position-1
	}

	p.Add(p.History[pos])
	return nil
}

//------------------------------------------------\\
// + + +          F U N C T I O N S          + + + \\
//--------------------------------------------------\\

// MakePages returns a Pages struct with default values
func MakePages() Pages {
	return Pages{-1, 0, [20]Page{}}
}