Beispiel #1
0
func (t *Terminal) printPreview() {
	if !t.isPreviewEnabled() {
		return
	}
	t.pwindow.Erase()
	skip := t.previewer.offset
	extractColor(t.previewer.text, nil, func(str string, ansi *ansiState) bool {
		if skip > 0 {
			newlines := numLinesMax(str, skip)
			if skip <= newlines {
				for i := 0; i < skip; i++ {
					str = str[strings.Index(str, "\n")+1:]
				}
				skip = 0
			} else {
				skip -= newlines
				return true
			}
		}
		if !t.preview.wrap {
			lines := strings.Split(str, "\n")
			for i, line := range lines {
				limit := t.pwindow.Width
				if tui.DoesAutoWrap() {
					limit -= 1
				}
				if i == 0 {
					limit -= t.pwindow.X()
				}
				trimmed, _ := trimRight([]rune(line), limit)
				lines[i] = string(trimmed)
			}
			str = strings.Join(lines, "\n")
		}
		if ansi != nil && ansi.colored() {
			return t.pwindow.CFill(str, ansi.fg, ansi.bg, ansi.attr)
		}
		return t.pwindow.Fill(str)
	})
	if t.previewer.lines > t.pwindow.Height {
		offset := fmt.Sprintf("%d/%d", t.previewer.offset+1, t.previewer.lines)
		t.pwindow.Move(0, t.pwindow.Width-len(offset))
		t.pwindow.CPrint(tui.ColInfo, tui.Reverse, offset)
	}
}
Beispiel #2
0
func (t *Terminal) resizeWindows() {
	screenWidth := tui.MaxX()
	screenHeight := tui.MaxY()
	marginInt := [4]int{}
	for idx, sizeSpec := range t.margin {
		if sizeSpec.percent {
			var max float64
			if idx%2 == 0 {
				max = float64(screenHeight)
			} else {
				max = float64(screenWidth)
			}
			marginInt[idx] = int(max * sizeSpec.size * 0.01)
		} else {
			marginInt[idx] = int(sizeSpec.size)
		}
	}
	adjust := func(idx1 int, idx2 int, max int, min int) {
		if max >= min {
			margin := marginInt[idx1] + marginInt[idx2]
			if max-margin < min {
				desired := max - min
				marginInt[idx1] = desired * marginInt[idx1] / margin
				marginInt[idx2] = desired * marginInt[idx2] / margin
			}
		}
	}
	minAreaWidth := minWidth
	minAreaHeight := minHeight
	if t.isPreviewEnabled() {
		switch t.preview.position {
		case posUp, posDown:
			minAreaHeight *= 2
		case posLeft, posRight:
			minAreaWidth *= 2
		}
	}
	adjust(1, 3, screenWidth, minAreaWidth)
	adjust(0, 2, screenHeight, minAreaHeight)
	if t.window != nil {
		t.window.Close()
	}
	if t.bwindow != nil {
		t.bwindow.Close()
		t.pwindow.Close()
	}

	width := screenWidth - marginInt[1] - marginInt[3]
	height := screenHeight - marginInt[0] - marginInt[2]
	if t.isPreviewEnabled() {
		createPreviewWindow := func(y int, x int, w int, h int) {
			t.bwindow = tui.NewWindow(y, x, w, h, true)
			pwidth := w - 4
			// ncurses auto-wraps the line when the cursor reaches the right-end of
			// the window. To prevent unintended line-wraps, we use the width one
			// column larger than the desired value.
			if !t.preview.wrap && tui.DoesAutoWrap() {
				pwidth += 1
			}
			t.pwindow = tui.NewWindow(y+1, x+2, pwidth, h-2, false)
		}
		switch t.preview.position {
		case posUp:
			pheight := calculateSize(height, t.preview.size, minHeight, 3)
			t.window = tui.NewWindow(
				marginInt[0]+pheight, marginInt[3], width, height-pheight, false)
			createPreviewWindow(marginInt[0], marginInt[3], width, pheight)
		case posDown:
			pheight := calculateSize(height, t.preview.size, minHeight, 3)
			t.window = tui.NewWindow(
				marginInt[0], marginInt[3], width, height-pheight, false)
			createPreviewWindow(marginInt[0]+height-pheight, marginInt[3], width, pheight)
		case posLeft:
			pwidth := calculateSize(width, t.preview.size, minWidth, 5)
			t.window = tui.NewWindow(
				marginInt[0], marginInt[3]+pwidth, width-pwidth, height, false)
			createPreviewWindow(marginInt[0], marginInt[3], pwidth, height)
		case posRight:
			pwidth := calculateSize(width, t.preview.size, minWidth, 5)
			t.window = tui.NewWindow(
				marginInt[0], marginInt[3], width-pwidth, height, false)
			createPreviewWindow(marginInt[0], marginInt[3]+width-pwidth, pwidth, height)
		}
	} else {
		t.window = tui.NewWindow(
			marginInt[0],
			marginInt[3],
			width,
			height, false)
	}
}