Expands texttop to full size of current terminal

This commit is contained in:
Thomas Buckley-Houston 2016-05-21 14:11:25 +09:00
parent 8acb1bcb96
commit 093b5ad07e
4 changed files with 68 additions and 47 deletions

View file

@ -3,6 +3,7 @@ package main
import ( import (
"fmt" "fmt"
"strings" "strings"
"strconv"
"path/filepath" "path/filepath"
"time" "time"
"os" "os"
@ -25,8 +26,13 @@ var logfile string
var current string var current string
var curev termbox.Event var curev termbox.Event
var lastMouseButton string var lastMouseButton string
var desktopWidth = float32(C.WIDTH)
var desktopHeight = float32(C.HEIGHT) var hipWidth int
var hipHeight int
var envDesktopWidth int
var envDesktopHeight int
var desktopWidth float32
var desktopHeight float32
var desktopXFloat float32 var desktopXFloat float32
var desktopYFloat float32 var desktopYFloat float32
var roundedDesktopX int var roundedDesktopX int
@ -36,10 +42,6 @@ var roundedDesktopY int
var stopXZoomChannel = make(chan struct{}) var stopXZoomChannel = make(chan struct{})
var xZoomStoppedChannel = make(chan struct{}) var xZoomStoppedChannel = make(chan struct{})
// Dimensions of hiptext output, can be slightly different from terminal dimensions
var hipWidth int
var hipHeight int
var panNeedsSetup bool var panNeedsSetup bool
var panStartingX float32 var panStartingX float32
var panStartingY float32 var panStartingY float32
@ -48,11 +50,36 @@ func initialise() {
setupLogging() setupLogging()
log("Starting...") log("Starting...")
setupTermbox() setupTermbox()
calculateHipDimensions() setupDimensions()
C.xzoom_init() C.xzoom_init()
xzoomBackground() xzoomBackground()
} }
func parseENVVar(variable string) int {
value, err := strconv.Atoi(os.Getenv(variable))
if err != nil {
panic(err)
}
return value
}
func setupDimensions() {
hipWidth = parseENVVar("TTY_WIDTH")
hipHeight = parseENVVar("TTY_HEIGHT")
envDesktopWidth = parseENVVar("DESKTOP_WIDTH")
envDesktopHeight = parseENVVar("DESKTOP_HEIGHT")
C.desktop_width = C.int(envDesktopWidth)
C.width[C.SRC] = C.desktop_width
C.width[C.DST] = C.desktop_width
C.desktop_height = C.int(envDesktopHeight)
C.height[C.SRC] = C.desktop_height
C.height[C.DST] = C.desktop_height
desktopWidth = float32(envDesktopWidth)
desktopHeight = float32(envDesktopHeight)
log(fmt.Sprintf("Desktop dimensions: W: %d, H: %d", envDesktopWidth, envDesktopHeight))
log(fmt.Sprintf("Term dimensions: W: %d, H: %d", hipWidth, hipHeight))
}
func setupTermbox() { func setupTermbox() {
err := termbox.Init() err := termbox.Init()
if err != nil { if err != nil {
@ -86,23 +113,6 @@ func log(msg string) {
} }
} }
// Hiptext needs to render the aspect ratio faithfully. So firstly it tries to fill
// the terminal as much as it can. And secondly it treats a row as representing twice
// as much as a column - thus why there are some multiplications/divisions by 2.
func calculateHipDimensions() {
_tw, _th := termbox.Size()
tw := float32(_tw)
th := float32(_th * 2)
ratio := desktopWidth / desktopHeight
bestHeight := min(th, (tw / ratio))
bestWidth := min(tw, (bestHeight * ratio))
// Not sure why the +1 and -1 are needed, but they are.
hipWidth = roundToInt(bestWidth) + 1
hipHeight = roundToInt(bestHeight / 2) - 1
log(fmt.Sprintf("Term dimensions: W: %d, H: %d", _tw, _th))
log(fmt.Sprintf("Hiptext dimensions: W: %d, H: %d", hipWidth, hipHeight))
}
func min(a float32, b float32) float32 { func min(a float32, b float32) float32 {
if a < b { if a < b {
return a return a
@ -191,8 +201,8 @@ func zoom(direction string) {
C.magnification-- C.magnification--
} }
} }
C.width[C.SRC] = (C.WIDTH + C.magnification - 1) / C.magnification; C.width[C.SRC] = (C.desktop_width + C.magnification - 1) / C.magnification;
C.height[C.SRC] = (C.HEIGHT + C.magnification - 1) / C.magnification; C.height[C.SRC] = (C.desktop_height + C.magnification - 1) / C.magnification;
moveViewportForZoom(oldZoom) moveViewportForZoom(oldZoom)
keepViewportInDesktop() keepViewportInDesktop()
@ -217,26 +227,26 @@ func manageViewportSize() {
if C.width[C.SRC] < 1 { if C.width[C.SRC] < 1 {
C.width[C.SRC] = 1 C.width[C.SRC] = 1
} }
if C.width[C.SRC] > C.WIDTH { if C.width[C.SRC] > C.desktop_width {
C.width[C.SRC] = C.WIDTH C.width[C.SRC] = C.desktop_width
} }
if C.height[C.SRC] < 1 { if C.height[C.SRC] < 1 {
C.height[C.SRC] = 1 C.height[C.SRC] = 1
} }
if C.height[C.SRC] > C.HEIGHT { if C.height[C.SRC] > C.desktop_height {
C.height[C.SRC] = C.HEIGHT C.height[C.SRC] = C.desktop_height
} }
} }
func manageViewportPosition() { func manageViewportPosition() {
if C.xgrab > (C.WIDTH - C.width[C.SRC]) { if C.xgrab > (C.desktop_width - C.width[C.SRC]) {
C.xgrab = C.WIDTH - C.width[C.SRC] C.xgrab = C.desktop_width - C.width[C.SRC]
} }
if C.xgrab < 0 { if C.xgrab < 0 {
C.xgrab = 0 C.xgrab = 0
} }
if C.ygrab > (C.HEIGHT - C.height[C.SRC]) { if C.ygrab > (C.desktop_height - C.height[C.SRC]) {
C.ygrab = C.HEIGHT - C.height[C.SRC] C.ygrab = C.desktop_height - C.height[C.SRC]
} }
if C.ygrab < 0 { if C.ygrab < 0 {
C.ygrab = 0 C.ygrab = 0

View file

@ -5,6 +5,8 @@ import (
. "github.com/onsi/ginkgo" . "github.com/onsi/ginkgo"
. "github.com/onsi/gomega" . "github.com/onsi/gomega"
"testing" "testing"
"os"
) )
func TestMouseInput(t *testing.T) { func TestMouseInput(t *testing.T) {
@ -14,13 +16,13 @@ func TestMouseInput(t *testing.T) {
var _ = Describe("Mouse Input", func() { var _ = Describe("Mouse Input", func() {
BeforeEach(func() { BeforeEach(func() {
err := termbox.Init() os.Setenv("DESKTOP_WIDTH", "1600")
if err != nil { os.Setenv("DESKTOP_HEIGHT", "1200")
panic(err) os.Setenv("TTY_WIDTH", "90")
} os.Setenv("TTY_HEIGHT", "30")
initialise() setupLogging()
hipWidth = 90 termbox.Init()
hipHeight = 30 setupDimensions()
setCurrentDesktopCoords() setCurrentDesktopCoords()
}) })
@ -74,6 +76,8 @@ var _ = Describe("Mouse Input", func() {
zoom("in") zoom("in")
setCurrentDesktopCoords() setCurrentDesktopCoords()
zoom("out") zoom("out")
// Shouldn't need to do this a second time, but this test helped me
// figure out a different bug, so I'm leaving it like this for now.
zoom("out") zoom("out")
setCurrentDesktopCoords() setCurrentDesktopCoords()
Expect(getXGrab()).To(Equal(0)) Expect(getXGrab()).To(Equal(0))

9
run.sh
View file

@ -4,7 +4,14 @@ export LC_ALL=C
export LANG=C export LANG=C
export DESKTOP_WIDTH='1600' export DESKTOP_WIDTH='1600'
export DESKTOP_HEIGHT='1200' export TTY_WIDTH=$(( $(stty size | cut -d' ' -f2) - 1))
export TTY_HEIGHT=$(( $(stty size | cut -d' ' -f1) - 1))
# Hiptext uses a row to represent twice as much as a column in order
# to more faithfully project the aspect ratio of the image/video.
ratio=$(echo "scale=5; $TTY_HEIGHT * 2 / $TTY_WIDTH" | bc)
height_float=$(echo "scale=5; $ratio*$DESKTOP_WIDTH" | bc)
export DESKTOP_HEIGHT=$(printf "%.0f\n" "$height_float")
export DISPLAY=:0 export DISPLAY=:0
DESKTOP_RES="$DESKTOP_WIDTH"x"$DESKTOP_HEIGHT" DESKTOP_RES="$DESKTOP_WIDTH"x"$DESKTOP_HEIGHT"
UDP_URI='udp://127.0.0.1:1234' UDP_URI='udp://127.0.0.1:1234'

View file

@ -28,8 +28,8 @@ GC gc;
#define SRC 0 #define SRC 0
#define DST 1 #define DST 1
#define WIDTH 1600 int desktop_width;
#define HEIGHT 1200 int desktop_height;
// The top left of the area that we zoom in on // The top left of the area that we zoom in on
int xgrab = 0; int xgrab = 0;
@ -38,8 +38,8 @@ int ygrab = 0;
int magnification = 1; int magnification = 1;
int old_magnification = 1; int old_magnification = 1;
int width[2] = { WIDTH, WIDTH }; int width[2];
int height[2] = { HEIGHT, HEIGHT }; int height[2];
unsigned depth = 0; unsigned depth = 0;
XImage *ximage[2]; XImage *ximage[2];
@ -137,7 +137,7 @@ int xzoom_init() {
depth = DefaultDepthOfScreen(scr); depth = DefaultDepthOfScreen(scr);
win = XCreateWindow(dpy, RootWindowOfScreen(scr), win = XCreateWindow(dpy, RootWindowOfScreen(scr),
WIDTH, 0, width[DST], height[DST], 0, desktop_width, 0, width[DST], height[DST], 0,
DefaultDepthOfScreen(scr), InputOutput, DefaultDepthOfScreen(scr), InputOutput,
DefaultVisualOfScreen(scr), DefaultVisualOfScreen(scr),
CWEventMask | CWBackPixel, &xswa); CWEventMask | CWBackPixel, &xswa);