~wrycode/DWCHelper

ref: eefcd76f0dce1a27168fa1966757fdf86bdd1425 DWCHelper/lib.go -rw-r--r-- 2.4 KiB View raw
eefcd76fNick Econopouly Add Prompt function 1 year, 1 month ago
                                                                                
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
package main

import (
	"io"
	"os"
	"strconv"
	"bufio"
	"fmt"
	"time"
	"strings"
)
// 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
}

// Replace returns a []string with oldTerm replaced by newTerm
// func Replace( terms []string, oldTerm, newTerm) []string {
// }

// 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-%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 _, v := range terms {
		fmt.Printf("\"%v\" \n",v)
	}
	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(100 * time.Millisecond)


	for b.Scan() {
		time.Sleep(100 * 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
			}
		}
	}
}