<commit_before>/* sbt -- Simple Build Tool
 * Copyright 2009, 2010  <NAME>, <NAME>, <NAME>
 */
package sbt

	import CommandSupport.{ClearOnFailure,FailureWall}
	import annotation.tailrec
	import java.io.File

trait Watched
{
	/** A `PathFinder` that determines the files watched when an action is run with a preceeding ~ when this is the current
	* project.  This project does not need to include the watched paths for projects that this project depends on.*/
	def watchPaths(s: State): Seq[File] = Nil
	def terminateWatch(key: Int): Boolean = Watched.isEnter(key)
	/** The time in milliseconds between checking for changes.  The actual time between the last change made to a file and the
	* execution time is between `pollInterval` and `pollInterval*2`.*/
	def pollInterval: Int = Watched.PollDelayMillis
}

object Watched
{
	private[this] class AWatched extends Watched
	
	def multi(base: Watched, paths: Seq[Watched]): Watched = 
		new AWatched
		{
			override def watchPaths(s: State) = (base.watchPaths(s) /: paths)(_ ++ _.watchPaths(s))
			override def terminateWatch(key: Int): Boolean = base.terminateWatch(key)
			override val pollInterval = (base +: paths).map(_.pollInterval).min
		}
	def empty: Watched = new AWatched
		
	val PollDelayMillis = 500
	def isEnter(key: Int): Boolean = key == 10 || key == 13

	def executeContinuously(watched: Watched, s: State, next: String, repeat: String): State =
	{
		@tailrec def shouldTerminate: Boolean = (System.in.available > 0) && (watched.terminateWatch(System.in.read()) || shouldTerminate)
		val sourcesFinder = Path.finder { watched watchPaths s }
		val watchState = s get ContinuousState getOrElse WatchState.empty

		if(watchState.count > 0)
			System.out.println(watchState.count + ". Waiting for source changes... (press enter to interrupt)")

		val (triggered, newWatchState) = SourceModificationWatch.watch(sourcesFinder, watched.pollInterval, watchState)(shouldTerminate)

		if(triggered)
			(ClearOnFailure :: next :: FailureWall :: repeat :: s).put(ContinuousState, newWatchState)
		else
		{
			while (System.in.available() > 0) System.in.read()
			s.put(ContinuousState, WatchState.empty)
		}
	}
	val ContinuousState = AttributeKey[WatchState]("watch state", "Internal: tracks state for continuous execution.")
	val Configuration = AttributeKey[Watched]("watched-configuration", "Configures continuous execution.")
}<commit_msg>Handle exceptions thrown during triggered execution polling<commit_after>/* sbt -- Simple Build Tool
 * Copyright 2009, 2010  <NAME>, <NAME>, <NAME>
 */
package sbt

	import CommandSupport.{ClearOnFailure,FailureWall}
	import annotation.tailrec
	import java.io.File

trait Watched
{
	/** The files watched when an action is run with a preceeding ~ */
	def watchPaths(s: State): Seq[File] = Nil
	def terminateWatch(key: Int): Boolean = Watched.isEnter(key)
	/** The time in milliseconds between checking for changes.  The actual time between the last change made to a file and the
	* execution time is between `pollInterval` and `pollInterval*2`.*/
	def pollInterval: Int = Watched.PollDelayMillis
}

object Watched
{
	private[this] class AWatched extends Watched
	
	def multi(base: Watched, paths: Seq[Watched]): Watched = 
		new AWatched
		{
			override def watchPaths(s: State) = (base.watchPaths(s) /: paths)(_ ++ _.watchPaths(s))
			override def terminateWatch(key: Int): Boolean = base.terminateWatch(key)
			override val pollInterval = (base +: paths).map(_.pollInterval).min
		}
	def empty: Watched = new AWatched
		
	val PollDelayMillis = 500
	def isEnter(key: Int): Boolean = key == 10 || key == 13

	def executeContinuously(watched: Watched, s: State, next: String, repeat: String): State =
	{
		@tailrec def shouldTerminate: Boolean = (System.in.available > 0) && (watched.terminateWatch(System.in.read()) || shouldTerminate)
		val sourcesFinder = Path.finder { watched watchPaths s }
		val watchState = s get ContinuousState getOrElse WatchState.empty

		if(watchState.count > 0)
			System.out.println(watchState.count + ". Waiting for source changes... (press enter to interrupt)")

		val (triggered, newWatchState, newState) =
			try {
				val (triggered, newWatchState) = SourceModificationWatch.watch(sourcesFinder, watched.pollInterval, watchState)(shouldTerminate)
				(triggered, newWatchState, s)
			}
			catch { case e: Exception =>
				val log = CommandSupport.logger(s)
				log.error("Error occurred obtaining files to watch.  Terminating continuous execution...")
				BuiltinCommands.handleException(e, s, log)
				(false, watchState, s.fail)
			}

		if(triggered)
			(ClearOnFailure :: next :: FailureWall :: repeat :: s).put(ContinuousState, newWatchState)
		else
		{
			while (System.in.available() > 0) System.in.read()
			s.put(ContinuousState, WatchState.empty)
		}
	}
	val ContinuousState = AttributeKey[WatchState]("watch state", "Internal: tracks state for continuous execution.")
	val Configuration = AttributeKey[Watched]("watched-configuration", "Configures continuous execution.")
}
{'fraction_non_alphanumeric': 0.11168727562824092, 'fraction_numerical': 0.007578779417630634, 'mean_word_length': 4.634831460674158, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5172606', 'n_tokens_mistral': 1625, 'n_tokens_neox': 1472, 'n_words': 465}