summaryrefslogtreecommitdiff
path: root/print_digit_map.go
blob: 4def1770108a254018ca78fcf36dbd7370a0c449 (plain)
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
package main

import (
	"aphopkins/radix_info/factors"
	"fmt"
	"io"
	"strconv"
	"strings"
)

func writeDigitMap(w io.Writer, digitMap []factors.DigitType) {
	if len(digitMap) < 2 {
		panic("Radices cannot be less than 2!")
	} else if len(digitMap) <= 36 {
		writeDigitMapSmall(w, digitMap)
	}
}

// Prints a digit map suitable for small bases (≤36).
func writeDigitMapSmall(w io.Writer, digitMap []factors.DigitType) {
	radix := uint(len(digitMap))
	digitsString := strings.Builder{}
	digitsString.Grow(int(3*radix + 6))
	digitsString.WriteString("Digit:")
	typesString := strings.Builder{}
	typesString.WriteString("Class:")
	for digit, digitType := range digitMap {
		digitString := fmt.Sprintf("%2s", strings.ToUpper(
			strconv.FormatUint(uint64(digit), int(radix))))
		typeString := digitType.String()
		fmt.Fprintf(&digitsString, " %s", colourString(digitString, digitType))
		fmt.Fprintf(&typesString, " %s", colourString(typeString, digitType))
	}

	fmt.Fprintln(w, digitsString.String())
	fmt.Fprintln(w, typesString.String())
}

func colourString(s string, digitType factors.DigitType) string {
	var colourBegin string
	switch digitType.TotativeType() {
	case factors.Regular:
		switch digitType.Regularity() {
		case 0:
			colourBegin = "\x1B[48;5;5m" // special cases (1)
		case 1:
			colourBegin = "\x1B[48;5;4m" // factors
		case 2:
			colourBegin = "\x1B[48;5;6m" // 2-regulars
		default:
			colourBegin = "\x1B[48;5;2m" // other regulars
		}
	case factors.Neighbour:
		if digitType.Regularity() == 0 {
			colourBegin = "\x1B[48;5;198m" // neighbourly totatives
		} else {
			colourBegin = "\x1B[48;5;136m" // neighbourly semitotatives
		}
	case factors.Opaque:
		if digitType.Regularity() == 0 {
			colourBegin = "\x1B[48;5;1m" // opaque totatives
		} else {
			colourBegin = "\x1B[48;5;130m" // opaque semitotatives
		}
	default:
		colourBegin = "\x1B[48;5;5m" // special cases (0)
	}

	return colourBegin + "\x1B[38;5;15m" + s + "\x1B[0m"
}