-
Notifications
You must be signed in to change notification settings - Fork 0
/
hr.go
54 lines (47 loc) · 855 Bytes
/
hr.go
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
/* hr.c - writes horizontal bar to standard output
* (c) Copyright 2020 Bartosz Mierzynski
*/
package main
import (
"fmt"
"os"
"strconv"
"golang.org/x/crypto/ssh/terminal"
"unicode/utf8"
)
func get_columns() uint {
if columns , err := strconv.Atoi(os.Getenv("COLUMNS")); err == nil {
return uint(columns)
} else {
if width, _, err := terminal.GetSize(int(os.Stdout.Fd())); err == nil {
return uint(width)
} else {
return 80
}
}
}
func hr1(n uint) {
for n > 0 {
fmt.Print("#")
n--
}
fmt.Print("\n")
}
func hr2(n uint, s string) {
rlen := uint(utf8.RuneCountInString(s))
runes := []rune(s)
for i := uint(0); i < n; i++ {
fmt.Printf("%c", runes[i % rlen])
}
fmt.Print("\n")
}
func main() {
n := get_columns()
if len(os.Args) < 2 {
hr1(n)
} else {
for _, arg := range os.Args[1:] {
hr2(n, arg)
}
}
}