2015-04-16 17:01:37 -04:00
|
|
|
// +build windows
|
|
|
|
|
|
|
|
package winterm
|
|
|
|
|
2015-08-06 23:38:56 -04:00
|
|
|
// effectiveSr gets the current effective scroll region in buffer coordinates
|
2016-04-25 18:55:12 -04:00
|
|
|
func (h *windowsAnsiEventHandler) effectiveSr(window SMALL_RECT) scrollRegion {
|
|
|
|
top := addInRange(window.Top, h.sr.top, window.Top, window.Bottom)
|
|
|
|
bottom := addInRange(window.Top, h.sr.bottom, window.Top, window.Bottom)
|
2015-08-06 23:38:56 -04:00
|
|
|
if top >= bottom {
|
|
|
|
top = window.Top
|
|
|
|
bottom = window.Bottom
|
|
|
|
}
|
|
|
|
return scrollRegion{top: top, bottom: bottom}
|
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
func (h *windowsAnsiEventHandler) scrollUp(param int) error {
|
2015-08-06 23:38:56 -04:00
|
|
|
info, err := GetConsoleScreenBufferInfo(h.fd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sr := h.effectiveSr(info.Window)
|
|
|
|
return h.scroll(param, sr, info)
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
func (h *windowsAnsiEventHandler) scrollDown(param int) error {
|
2015-08-06 23:38:56 -04:00
|
|
|
return h.scrollUp(-param)
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
func (h *windowsAnsiEventHandler) deleteLines(param int) error {
|
2015-04-16 17:01:37 -04:00
|
|
|
info, err := GetConsoleScreenBufferInfo(h.fd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-06 23:38:56 -04:00
|
|
|
start := info.CursorPosition.Y
|
|
|
|
sr := h.effectiveSr(info.Window)
|
|
|
|
// Lines cannot be inserted or deleted outside the scrolling region.
|
|
|
|
if start >= sr.top && start <= sr.bottom {
|
|
|
|
sr.top = start
|
|
|
|
return h.scroll(param, sr, info)
|
|
|
|
} else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
func (h *windowsAnsiEventHandler) insertLines(param int) error {
|
2015-08-06 23:38:56 -04:00
|
|
|
return h.deleteLines(-param)
|
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
2015-08-06 23:38:56 -04:00
|
|
|
// scroll scrolls the provided scroll region by param lines. The scroll region is in buffer coordinates.
|
2016-04-25 18:55:12 -04:00
|
|
|
func (h *windowsAnsiEventHandler) scroll(param int, sr scrollRegion, info *CONSOLE_SCREEN_BUFFER_INFO) error {
|
2015-08-06 23:38:56 -04:00
|
|
|
logger.Infof("scroll: scrollTop: %d, scrollBottom: %d", sr.top, sr.bottom)
|
|
|
|
logger.Infof("scroll: windowTop: %d, windowBottom: %d", info.Window.Top, info.Window.Bottom)
|
2015-04-16 17:01:37 -04:00
|
|
|
|
2015-08-06 23:38:56 -04:00
|
|
|
// Copy from and clip to the scroll region (full buffer width)
|
|
|
|
scrollRect := SMALL_RECT{
|
|
|
|
Top: sr.top,
|
|
|
|
Bottom: sr.bottom,
|
|
|
|
Left: 0,
|
|
|
|
Right: info.Size.X - 1,
|
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
2015-08-06 23:38:56 -04:00
|
|
|
// Origin to which area should be copied
|
|
|
|
destOrigin := COORD{
|
|
|
|
X: 0,
|
2016-04-25 18:55:12 -04:00
|
|
|
Y: sr.top - int16(param),
|
2015-08-06 23:38:56 -04:00
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
2015-08-06 23:38:56 -04:00
|
|
|
char := CHAR_INFO{
|
|
|
|
UnicodeChar: ' ',
|
|
|
|
Attributes: h.attributes,
|
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
2015-08-06 23:38:56 -04:00
|
|
|
if err := ScrollConsoleScreenBuffer(h.fd, scrollRect, scrollRect, destOrigin, char); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
func (h *windowsAnsiEventHandler) deleteCharacters(param int) error {
|
2015-04-16 17:01:37 -04:00
|
|
|
info, err := GetConsoleScreenBufferInfo(h.fd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-08-06 23:38:56 -04:00
|
|
|
return h.scrollLine(param, info.CursorPosition, info)
|
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
2016-04-25 18:55:12 -04:00
|
|
|
func (h *windowsAnsiEventHandler) insertCharacters(param int) error {
|
2015-08-06 23:38:56 -04:00
|
|
|
return h.deleteCharacters(-param)
|
|
|
|
}
|
2015-04-16 17:01:37 -04:00
|
|
|
|
2015-08-06 23:38:56 -04:00
|
|
|
// scrollLine scrolls a line horizontally starting at the provided position by a number of columns.
|
2016-04-25 18:55:12 -04:00
|
|
|
func (h *windowsAnsiEventHandler) scrollLine(columns int, position COORD, info *CONSOLE_SCREEN_BUFFER_INFO) error {
|
2015-08-06 23:38:56 -04:00
|
|
|
// Copy from and clip to the scroll region (full buffer width)
|
2015-04-16 17:01:37 -04:00
|
|
|
scrollRect := SMALL_RECT{
|
2015-08-06 23:38:56 -04:00
|
|
|
Top: position.Y,
|
|
|
|
Bottom: position.Y,
|
|
|
|
Left: position.X,
|
|
|
|
Right: info.Size.X - 1,
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Origin to which area should be copied
|
|
|
|
destOrigin := COORD{
|
2016-04-25 18:55:12 -04:00
|
|
|
X: position.X - int16(columns),
|
2015-08-06 23:38:56 -04:00
|
|
|
Y: position.Y,
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
char := CHAR_INFO{
|
|
|
|
UnicodeChar: ' ',
|
2015-08-06 23:38:56 -04:00
|
|
|
Attributes: h.attributes,
|
2015-04-16 17:01:37 -04:00
|
|
|
}
|
|
|
|
|
2015-08-06 23:38:56 -04:00
|
|
|
if err := ScrollConsoleScreenBuffer(h.fd, scrollRect, scrollRect, destOrigin, char); err != nil {
|
2015-04-16 17:01:37 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|