~dricottone/image2ascii

ref: 52470578178123a6b4fb14f529b0179d8b426a3a image2ascii/vendor/github.com/wayneashleyberry/terminal-dimensions/terminaldimensions.go -rw-r--r-- 1004 bytes
52470578 — qeesung remove uesless example 6 years ago
                                                                                
0aea0380 qeesung
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
// Package terminaldimensions provides simple helper functions to get the width
// and height of a users terminal.
package terminaldimensions

import (
	"os"
	"os/exec"
	"strconv"
	"strings"
)

func size() (string, error) {
	cmd := exec.Command("stty", "size")
	cmd.Stdin = os.Stdin
	out, err := cmd.Output()
	return string(out), err
}

func parse(input string) (uint, uint, error) {
	parts := strings.Split(input, " ")
	x, err := strconv.Atoi(parts[0])
	if err != nil {
		return 0, 0, err
	}
	y, err := strconv.Atoi(strings.Replace(parts[1], "\n", "", 1))
	if err != nil {
		return 0, 0, err
	}
	return uint(x), uint(y), nil
}

// Width return the width of the terminal.
func Width() (uint, error) {
	output, err := size()
	if err != nil {
		return 0, err
	}
	_, width, err := parse(output)
	return width, err
}

// Height returns the height of the terminal.
func Height() (uint, error) {
	output, err := size()
	if err != nil {
		return 0, err
	}
	height, _, err := parse(output)
	return height, err
}