Browse Source

made credit/debit more flexible with account names

bmallred 10 years ago
parent
commit
6b8a340eb0
3 changed files with 14 additions and 18 deletions
  1. 3 2
      commandCredit.go
  2. 3 2
      commandDebit.go
  3. 8 14
      helpers.go

+ 3 - 2
commandCredit.go

28
	ensureFileExists(pendingFile)
28
	ensureFileExists(pendingFile)
29
29
30
	args := c.Args()
30
	args := c.Args()
31
	name, err := parseAccount(args)
31
	li := len(args) - 1
32
	name, err := parseAccount(args[:li])
32
	check(err)
33
	check(err)
33
	amount, err := parseValue(args, name)
34
	amount, err := parseAmount(args[li])
34
	check(err)
35
	check(err)
35
36
36
	a := Account{
37
	a := Account{

+ 3 - 2
commandDebit.go

28
	ensureFileExists(pendingFile)
28
	ensureFileExists(pendingFile)
29
29
30
	args := c.Args()
30
	args := c.Args()
31
	name, err := parseAccount(args)
31
	li := len(args) - 1
32
	name, err := parseAccount(args[:li])
32
	check(err)
33
	check(err)
33
	amount, err := parseValue(args, name)
34
	amount, err := parseAmount(args[li])
34
	check(err)
35
	check(err)
35
36
36
	a := Account{
37
	a := Account{

+ 8 - 14
helpers.go

42
42
43
// Parse a given string to extract an account name
43
// Parse a given string to extract an account name
44
func parseAccount(fields []string) (string, error) {
44
func parseAccount(fields []string) (string, error) {
45
	for i := 0; i < len(fields); i++ {
46
		if strings.HasPrefix(fields[i], "#") {
47
			return fields[i], nil
48
		}
45
	if len(fields) > 0 {
46
		return strings.Join(fields, " "), nil
49
	}
47
	}
50
48
51
	return fields[0], nil
49
	return "", errors.New("No account information found")
52
}
50
}
53
51
54
// Parse the given string to extract a proper date
52
// Parse the given string to extract a proper date
79
}
77
}
80
78
81
// Parse the value from the arguments
79
// Parse the value from the arguments
80
func parseAmount(text string) (*big.Rat, error) {
81
	amount := new(big.Rat)
82
82
83
func parseValue(fields []string, account string) (*big.Rat, error) {
84
	r := new(big.Rat)
85
86
	for i := 0; i < len(fields); i++ {
87
		if fields[i] != account {
88
			r.SetString(fields[i])
89
			return r, nil
90
		}
83
	if r, _ := amount.SetString(text); r != nil {
84
		return amount, nil
91
	}
85
	}
92
86
93
	return r, errors.New("No value found")
87
	return nil, errors.New("No value found")
94
}
88
}