From 4e27c4406122eb1eee359641d65776efd2574002 Mon Sep 17 00:00:00 2001 From: Brian Evans Date: Tue, 28 May 2019 14:23:35 -0700 Subject: [PATCH] Rough changes to allow for full scroll on resize to small screen. Needs review. --- cui/screen.go | 24 ++----------------- cui/window.go | 65 ++++++++++++++++++++++++++------------------------- main.go | 20 ++++++++++++++++ 3 files changed, 55 insertions(+), 54 deletions(-) diff --git a/cui/screen.go b/cui/screen.go index 7b3cdd4..f79df2d 100644 --- a/cui/screen.go +++ b/cui/screen.go @@ -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 { diff --git a/cui/window.go b/cui/window.go index 067e306..fe82cd5 100644 --- a/cui/window.go +++ b/cui/window.go @@ -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") } diff --git a/main.go b/main.go index 314afae..3cc2e26 100644 --- a/main.go +++ b/main.go @@ -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()