package console

// AddInterrupt registers a handler to run when the console receives
// a given interrupt error from the underlying readline shell.
//
// On most systems, the following errors will be returned with keypresses:
// - Linux/MacOS/Windows : Ctrl-C will return os.Interrupt.
//
// Many will want to use this to switch menus. Note that these interrupt errors only
// work when the console is NOT currently executing a command, only when reading input.
func ( *Menu) ( error,  func( *Console)) {
	.mutex.RLock()
	.interruptHandlers[] = 
	.mutex.RUnlock()
}

// DelInterrupt removes one or more interrupt handlers from the menu registered ones.
// If no error is passed as argument, all handlers are removed.
func ( *Menu) ( ...error) {
	.mutex.RLock()
	if len() == 0 {
		.interruptHandlers = make(map[error]func( *Console))
	} else {
		for ,  := range  {
			delete(.interruptHandlers, )
		}
	}
	.mutex.RUnlock()
}

func ( *Menu) ( error) {
	.console.mutex.RLock()
	.console.isExecuting = true
	.console.mutex.RUnlock()

	defer func() {
		.console.mutex.RLock()
		.console.isExecuting = false
		.console.mutex.RUnlock()
	}()

	// TODO: this is not a very, very safe way of comparing
	// errors. I'm not sure what to right now with this, but
	// from my (unreliable) expectations and usage, I see and
	// use things like errors.New(os.Interrupt.String()), so
	// the string itself is likely to change in the future.
	//
	// But if people use their own third-party errors... nothing is guaranteed.
	for ,  := range .interruptHandlers {
		if .Error() == .Error() {
			(.console)
		}
	}
}