2022-10-08 20:43:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Brian Evans
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, version 3 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2019-09-10 02:35:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
//------------------------------------------------\\
|
|
|
|
// + + + T Y P E S + + + \\
|
|
|
|
//--------------------------------------------------\\
|
|
|
|
|
2019-11-11 00:18:27 +00:00
|
|
|
// 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.
|
2019-09-10 02:35:16 +00:00
|
|
|
type Pages struct {
|
|
|
|
Position int
|
2019-11-10 18:41:12 +00:00
|
|
|
Length int
|
|
|
|
History [20]Page
|
2019-09-10 02:35:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------\\
|
|
|
|
// + + + R E C E I V E R S + + + \\
|
|
|
|
//--------------------------------------------------\\
|
|
|
|
|
2019-11-11 00:18:27 +00:00
|
|
|
// 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.
|
2019-09-10 02:35:16 +00:00
|
|
|
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")
|
2019-11-10 18:41:12 +00:00
|
|
|
} else if newPosition > p.Length-1 {
|
2019-09-10 02:35:16 +00:00
|
|
|
return fmt.Errorf("Your way is blocked by void, there is nothing forward")
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Position = newPosition
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-11 00:18:27 +00:00
|
|
|
// Add gets passed a Page, which gets added to the history
|
2020-11-14 23:40:18 +00:00
|
|
|
// array. Add also updates the current length and position
|
2019-11-11 00:18:27 +00:00
|
|
|
// of the Pages struct to which it belongs. Add also shifts
|
|
|
|
// off array items if necessary.
|
2019-09-12 05:53:36 +00:00
|
|
|
func (p *Pages) Add(pg Page) {
|
2019-11-10 18:41:12 +00:00
|
|
|
if p.Position == p.Length-1 && p.Length < len(p.History) {
|
2019-09-12 05:53:36 +00:00
|
|
|
p.History[p.Length] = pg
|
|
|
|
p.Length++
|
|
|
|
p.Position++
|
2019-11-10 18:41:12 +00:00
|
|
|
} else if p.Position == p.Length-1 && p.Length == 20 {
|
2019-09-12 05:53:36 +00:00
|
|
|
for x := 1; x < len(p.History); x++ {
|
|
|
|
p.History[x-1] = p.History[x]
|
|
|
|
}
|
|
|
|
p.History[len(p.History)-1] = pg
|
|
|
|
} else {
|
2019-11-10 19:35:52 +00:00
|
|
|
p.Position++
|
2019-09-12 05:53:36 +00:00
|
|
|
p.Length = p.Position + 1
|
|
|
|
p.History[p.Position] = pg
|
|
|
|
}
|
2019-09-10 02:35:16 +00:00
|
|
|
}
|
|
|
|
|
2019-11-11 00:18:27 +00:00
|
|
|
// Render wraps the content for the current page and returns
|
|
|
|
// the page content as a string slice
|
2022-02-07 04:21:08 +00:00
|
|
|
func (p *Pages) Render(termHeight, termWidth, maxWidth int, color bool) []string {
|
2019-09-12 05:53:36 +00:00
|
|
|
if p.Length < 1 {
|
2019-09-14 05:56:38 +00:00
|
|
|
return make([]string, 0)
|
2019-09-12 05:53:36 +00:00
|
|
|
}
|
2019-09-13 03:57:48 +00:00
|
|
|
pos := p.History[p.Position].ScrollPosition
|
|
|
|
prev := len(p.History[p.Position].WrappedContent)
|
2020-02-14 06:46:39 +00:00
|
|
|
|
2020-02-15 18:51:39 +00:00
|
|
|
if termWidth != p.History[p.Position].WrapWidth || p.History[p.Position].Color != color {
|
2022-02-07 04:21:08 +00:00
|
|
|
p.History[p.Position].WrapContent(termWidth, maxWidth, color)
|
2020-02-15 16:43:54 +00:00
|
|
|
}
|
2020-02-14 06:46:39 +00:00
|
|
|
|
2019-09-13 03:57:48 +00:00
|
|
|
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
|
2019-11-10 18:41:12 +00:00
|
|
|
if pos > now-termHeight {
|
2019-09-13 03:57:48 +00:00
|
|
|
pos = now - termHeight
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-10 18:41:12 +00:00
|
|
|
if pos < 0 || now < termHeight-3 {
|
2019-09-13 03:57:48 +00:00
|
|
|
pos = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
p.History[p.Position].ScrollPosition = pos
|
|
|
|
|
|
|
|
return p.History[p.Position].WrappedContent[pos:]
|
2019-09-10 02:35:16 +00:00
|
|
|
}
|
|
|
|
|
2020-11-14 23:40:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-09-10 02:35:16 +00:00
|
|
|
//------------------------------------------------\\
|
|
|
|
// + + + F U N C T I O N S + + + \\
|
|
|
|
//--------------------------------------------------\\
|
|
|
|
|
2019-11-11 00:18:27 +00:00
|
|
|
// MakePages returns a Pages struct with default values
|
2019-09-10 02:35:16 +00:00
|
|
|
func MakePages() Pages {
|
|
|
|
return Pages{-1, 0, [20]Page{}}
|
|
|
|
}
|