File: vaxis_windows.go

package info (click to toggle)
golang-sourcehut-rockorager-vaxis 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 768 kB
  • sloc: makefile: 9
file content (71 lines) | stat: -rw-r--r-- 1,430 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//go:build windows

package vaxis

import (
	"fmt"
	"io"
	"os/signal"
	"syscall"
	"time"

	"git.sr.ht/~rockorager/vaxis/log"
)

func (vx *Vaxis) setupSignals() {
	signal.Notify(vx.chSigKill,
		// kill signals
		syscall.SIGABRT,
		syscall.SIGBUS,
		syscall.SIGFPE,
		syscall.SIGILL,
		syscall.SIGINT,
		syscall.SIGQUIT,
		syscall.SIGSEGV,
		syscall.SIGTERM,
	)
	// TODO: Use ReadConsoleInput for events??
	go vx.winch()
}

func (vx *Vaxis) winch() {
	ticker := time.NewTicker(100 * time.Millisecond)
	for {
		<-ticker.C
		if atomicLoad(&vx.resize) {
			continue
		}
		ws, err := vx.reportWinsize()
		if err != nil {
			log.Error("couldn't report winsize", "error", err)
			return
		}
		if ws.Cols != vx.winSize.Cols || ws.Rows != vx.winSize.Rows {
			atomicStore(&vx.resize, true)
			vx.PostEvent(Redraw{})
		}
	}
}

func (vx *Vaxis) reportWinsize() (Resize, error) {
	if vx.caps.reportSizeChars && vx.caps.reportSizePixels {
		log.Trace("requesting screen size from terminal")
		io.WriteString(vx.console, textAreaSize)
		deadline := time.NewTimer(100 * time.Millisecond)
		select {
		case <-deadline.C:
			return Resize{}, fmt.Errorf("screen size request deadline exceeded")
		case <-vx.chSizeDone:
			return vx.nextSize, nil
		}
	}
	log.Trace("requesting screen size from console")
	ws, err := vx.console.Size()
	if err != nil {
		return Resize{}, err
	}
	return Resize{
		Cols: int(ws.Width),
		Rows: int(ws.Height),
	}, nil
}