Rough changes to allow for full scroll on resize to small screen. Needs review.

This commit is contained in:
Brian Evans 2019-05-28 14:23:35 -07:00
parent c92db1001e
commit 4e27c44061
3 changed files with 55 additions and 54 deletions

View File

@ -26,7 +26,7 @@ type Screen struct {
// AddWindow adds a new window to the Screen struct in question
func (s *Screen) AddWindow(r1, c1, r2, c2 int, scroll, border, show bool) {
w := Window{box{r1, c1, r2, c2}, scroll, 0, []string{}, border, false, show}
w := Window{box{r1, c1, r2, c2}, scroll, 0, []string{}, border, false, show, 1}
s.Windows = append(s.Windows, &w)
}
@ -61,27 +61,7 @@ func (s Screen) Clear() {
// to redraw the full screen or just the content. On a resize
// event, the full screen will always be redrawn.
func (s *Screen) ReflashScreen(clearScreen bool) {
oldh, oldw := s.Height, s.Width
s.GetSize()
if s.Height != oldh || s.Width != oldw {
// TODO this should be pure library code and not rely on
// specific windows being present with specific behaviors.
// Maybe allow windows to have a resize function that can
// be declared within the application?
// For now this will be ok though.
s.Windows[0].Box.row2 = s.Height - 2
s.Windows[0].Box.col2 = s.Width
bookmarksWidth := 40
if s.Width < 40 {
bookmarksWidth = s.Width
}
s.Windows[1].Box.row2 = s.Height - 2
s.Windows[1].Box.col1 = s.Width - bookmarksWidth
s.Windows[1].Box.col2 = s.Width
s.DrawAllWindows()
s.DrawMsgBars()
} else if clearScreen {
if clearScreen {
s.DrawAllWindows()
s.DrawMsgBars()
} else {

View File

@ -6,10 +6,10 @@ import (
)
type box struct {
row1 int
col1 int
row2 int
col2 int
Row1 int
Col1 int
Row2 int
Col2 int
}
// TODO add coloring
@ -21,6 +21,7 @@ type Window struct {
drawBox bool
Active bool
Show bool
tempContentLen int
}
func (w *Window) DrawWindow() {
@ -36,18 +37,18 @@ func (w *Window) DrawBox() {
if w.Active {
lead = "a"
}
moveThenDrawShape(w.Box.row1, w.Box.col1, lead+"tl")
moveThenDrawShape(w.Box.row1, w.Box.col2, lead+"tr")
moveThenDrawShape(w.Box.row2, w.Box.col1, lead+"bl")
moveThenDrawShape(w.Box.row2, w.Box.col2, lead+"br")
for i := w.Box.col1 + 1; i < w.Box.col2; i++ {
moveThenDrawShape(w.Box.row1, i, lead+"ceiling")
moveThenDrawShape(w.Box.row2, i, lead+"ceiling")
moveThenDrawShape(w.Box.Row1, w.Box.Col1, lead+"tl")
moveThenDrawShape(w.Box.Row1, w.Box.Col2, lead+"tr")
moveThenDrawShape(w.Box.Row2, w.Box.Col1, lead+"bl")
moveThenDrawShape(w.Box.Row2, w.Box.Col2, lead+"br")
for i := w.Box.Col1 + 1; i < w.Box.Col2; i++ {
moveThenDrawShape(w.Box.Row1, i, lead+"ceiling")
moveThenDrawShape(w.Box.Row2, i, lead+"ceiling")
}
for i := w.Box.row1 + 1; i < w.Box.row2; i++ {
moveThenDrawShape(i, w.Box.col1, lead+"wall")
moveThenDrawShape(i, w.Box.col2, lead+"wall")
for i := w.Box.Row1 + 1; i < w.Box.Row2; i++ {
moveThenDrawShape(i, w.Box.Col1, lead+"wall")
moveThenDrawShape(i, w.Box.Col2, lead+"wall")
}
}
@ -61,10 +62,11 @@ func (w *Window) DrawContent() {
borderThickness, contenth = 1, 0
}
height := w.Box.row2 - w.Box.row1 + borderThickness
width := w.Box.col2 - w.Box.col1 + borderThickness
height := w.Box.Row2 - w.Box.Row1 + borderThickness
width := w.Box.Col2 - w.Box.Col1 + borderThickness
content := wrapLines(w.Content, width)
w.tempContentLen = len(content)
if len(content) < w.Scrollposition+height {
maxlines = len(content)
@ -74,14 +76,14 @@ func (w *Window) DrawContent() {
}
for i := w.Scrollposition; i < maxlines; i++ {
MoveCursorTo(w.Box.row1+contenth+i-w.Scrollposition, w.Box.col1+contenth)
MoveCursorTo(w.Box.Row1+contenth+i-w.Scrollposition, w.Box.Col1+contenth)
fmt.Print(strings.Repeat(" ", width))
MoveCursorTo(w.Box.row1+contenth+i-w.Scrollposition, w.Box.col1+contenth)
MoveCursorTo(w.Box.Row1+contenth+i-w.Scrollposition, w.Box.Col1+contenth)
fmt.Print(content[i])
}
if short_content {
for i := len(content); i <= height; i++ {
MoveCursorTo(w.Box.row1+contenth+i-w.Scrollposition, w.Box.col1+contenth)
MoveCursorTo(w.Box.Row1+contenth+i-w.Scrollposition, w.Box.Col1+contenth)
fmt.Print(strings.Repeat(" ", width))
}
}
@ -95,9 +97,9 @@ func (w *Window) ScrollDown() {
borderThickness = 1
}
height := w.Box.row2 - w.Box.row1 + borderThickness
contentLength := len(w.Content)
if w.Scrollposition < contentLength-height {
height := w.Box.Row2 - w.Box.Row1 + borderThickness
if w.Scrollposition < w.tempContentLen-height {
w.Scrollposition++
} else {
fmt.Print("\a")
@ -120,12 +122,12 @@ func (w *Window) PageDown() {
borderThickness = 1
}
height := w.Box.row2 - w.Box.row1 + borderThickness
contentLength := len(w.Content)
if w.Scrollposition < contentLength-height {
height := w.Box.Row2 - w.Box.Row1 + borderThickness
if w.Scrollposition < w.tempContentLen-height {
w.Scrollposition += height
if w.Scrollposition > contentLength-height {
w.Scrollposition = contentLength-height
if w.Scrollposition > w.tempContentLen-height {
w.Scrollposition = w.tempContentLen-height
}
} else {
fmt.Print("\a")
@ -140,7 +142,7 @@ func (w *Window) PageUp() {
borderThickness = 1
}
height := w.Box.row2 - w.Box.row1 + borderThickness
height := w.Box.Row2 - w.Box.Row1 + borderThickness
contentLength := len(w.Content)
if w.Scrollposition > 0 && height < contentLength {
w.Scrollposition -= height
@ -168,11 +170,10 @@ func (w *Window) ScrollEnd() {
borderThickness = 1
}
height := w.Box.row2 - w.Box.row1 + borderThickness
height := w.Box.Row2 - w.Box.Row1 + borderThickness
contentLength := len(w.Content)
if w.Scrollposition < contentLength-height {
w.Scrollposition = contentLength-height
if w.Scrollposition < w.tempContentLen-height {
w.Scrollposition = w.tempContentLen-height
} else {
fmt.Print("\a")
}

20
main.go
View File

@ -455,6 +455,25 @@ func initClient() error {
return loadConfig()
}
func handleResize() {
oldh, oldw := screen.Height, screen.Width
screen.GetSize()
if screen.Height != oldh || screen.Width != oldw {
screen.Windows[0].Box.Row2 = screen.Height - 2
screen.Windows[0].Box.Col2 = screen.Width
bookmarksWidth := 40
if screen.Width < 40 {
bookmarksWidth = screen.Width
}
screen.Windows[1].Box.Row2 = screen.Height - 2
screen.Windows[1].Box.Col1 = screen.Width - bookmarksWidth
screen.Windows[1].Box.Col2 = screen.Width
screen.DrawAllWindows()
screen.DrawMsgBars()
}
}
func main() {
cui.HandleAlternateScreen("smcup")
defer cui.Exit()
@ -481,6 +500,7 @@ func main() {
for {
c := cui.Getch()
handleResize()
switch c {
case 'j', 'J':
screen.Windows[screen.Activewindow].ScrollDown()