package core

import (
	
	
	

	
)

// Iterations manages iterations for commands.
type Iterations struct {
	times   string // Stores iteration value
	active  bool   // Are we currently setting the iterations.
	pending bool   // Has the last command been an iteration one (vi-pending style)
}

// Add accepts a string to be converted as an integer representing
// the number of times some action should be performed.
// The times parameter can also be a negative sign, in which case
// the iterations value will be negative until those are reset.
func ( *Iterations) ( string) {
	if  == "" {
		return
	}

	// Never accept non-digit values.
	if ,  := strconv.Atoi();  != nil &&  != "-" {
		return
	}

	.active = true
	.pending = true

	switch {
	case  == "-":
		.times =  + .times
	case strings.HasPrefix(, "-"):
		.times = "-" + .times + strings.TrimPrefix(, "-")
	default:
		.times += 
	}
}

// Get returns the number of iterations (possibly
// negative), and resets the iterations to 1.
func ( *Iterations) () int {
	,  := strconv.Atoi(.times)

	// Any invalid value is still one time.
	if  != nil && strings.HasPrefix(.times, "-") {
		 = -1
	} else if  != nil &&  == 0 {
		 = 1
	} else if  == 0 && strings.HasPrefix(.times, "-") {
		 = -1
	}

	// At least one iteration
	if  == 0 {
		++
	}

	.times = ""

	return 
}

// IsSet returns true if an iteration/numeric argument is active.
func ( *Iterations) () bool {
	return .active
}

// IsPending returns true if the very last command executed was an
// iteration one. This is only meant for the main readline loop/run.
func ( *Iterations) () bool {
	return .pending
}

// Reset resets the iterations (drops them).
func ( *Iterations) () {
	.times = ""
	.active = false
	.pending = false
}

// ResetPostRunIterations resets the iterations if the last command didn't set them.
// If the reset operated on active iterations, this function returns true.
func ( *Iterations) ( string) {
	if .pending {
		 = color.Dim + fmt.Sprintf("(arg: %s)", .times)
	}

	if .pending {
		.pending = false

		return
	}

	.active = false

	return
}