ref: ed91fcd170752c09a671fc21fd78f5bb10849f37 DWCHelper/lib.go -rw-r--r-- 3.8 KiB View raw
                                                                                
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main

import (
	"io"
	"os"
	"strconv"
	"bufio"
	"fmt"
	"time"
	"strings"
	"github.com/fatih/camelcase"
)

const termURL string = "https://raw.githubusercontent.com/tdwg/dwc/master/dist/simple_dwc_horizontal.csv"

// Index returns the first index of the target string t, or -1 if no
// match is found
func Index(terms []string, s string) int {
	for i, v := range terms {
		if v == s {
			return i
		}
	}
	return -1
}

// Include returns true if the target string t is in the slice
func Include(terms []string, term string) bool {
	return Index(terms, term) >= 0
}

// Remove returns a []string with all instances of term removed, or
// unchanged if the term isn't in the slice
func Remove(terms []string, term string) []string {
	var result []string
	for _, t := range terms {
		if t != term {
			result = append(result, t)
		}
	}
	if result != nil {
		return result
	}
	return []string{}
}

// Rename renames all occurrences of oldTerm to newTerm in terms
func Rename(terms []string, oldTerm, newTerm string) []string {
	for i, t := range terms {
		if t == oldTerm {
			terms[i] = newTerm
		}
	}
	return terms
}

// inputNumber returns an int between the given upper and lower
// limits, taken from the io.Reader argument (such as os.Stdin). If
// the input is invalid, it returns 0
func inputNumber (first int, second int, r io.Reader) int {
	fmt.Printf("Your choice? (%v to %v): ",first,second)
	b := bufio.NewScanner(r)
	for b.Scan() {
		n, err := strconv.Atoi(b.Text())
		if err == nil {
			if first <= n && n <= second {
				fmt.Println()
				return n
			}
		}
		fmt.Printf("Please enter a valid number between %v and %v and hit Enter:\n", first, second)
	}
	if err := b.Err(); err != nil {
		fmt.Fprintln(os.Stderr, "reading standard input:", err)
	}
	fmt.Println()
	return 0
}

// printStringSlice prints a []string, surrounding each string with
// quotes
func printStringSlice(terms []string) {
	for i, v := range terms {
		fmt.Printf("\"%v\" ",v)
		if i % 3 == 0 {
			fmt.Println()
		}
	}
}

// Prompt prints out the given string, and asks for user confirmation
// to continue is ask is set to true
func Prompt(ask bool, s string) {
	b := bufio.NewScanner(strings.NewReader(s))
		time.Sleep(25 * time.Millisecond)


	for b.Scan() {
		time.Sleep(25 * time.Millisecond)
		fmt.Println(b.Text())
	}
	if ask {
		r := bufio.NewReader(os.Stdin)
		time.Sleep(100 * time.Millisecond)
		fmt.Println("Press Enter to continue...")
		for {
			_, err := r.Peek(1) 
			if err == nil {
				return
			}
		}
	}
}

// PrintHLine draws one or more horizontal lines
func PrintHLine(i int) {
	for n := 0; n < i; n = n +1 {
		fmt.Println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
	}
}

// showReference returns the URL for a term on the Darwin Core website,
// which includes a definition, comments and examples
func showReference(term string) string {
	return referenceURL + "#" + term
}

// notAllSame returns true if not every element of a string slice is the same
func notAllSame(s []string) bool {
	for i := 0; i < len(s); i++ {
		if s[i] != s[0] {
			return true
		}
	}
	return false
}

// stringIsVariation is a loose matching function to determine if a
// string could be a variation of a term in camelCase
func stringIsVariation(s string, t string) bool {

	words := camelcase.Split(t)
	
	switch {
	case s == strings.ToLower(strings.Join(words, " ")):
		return true
	case s == strings.Title(strings.Join(words, " ")):
		return true
	}

	stringWords := strings.Split(s, " ")

	for _, x := range stringWords {
		for _, y := range words {
			if strings.ToLower(x) == strings.ToLower(y) {
				return true
			}
		}
	}

	return false
}

// inputTerm gets a new term from the user
func inputTerm(message string, r io.Reader) string {
	fmt.Print(message)
	b := bufio.NewScanner(r)
	b.Scan()
	fmt.Println()
	return b.Text()
}