Moreコマンド

パイプラインパターンで一時蓄積してみました。
外側のforeachループ一回ごとにMoreの中(右)では10回回ります。
まだちょっと荒削りというか、未熟だけど。


static void Main()
{
try
{
string fileName = "../../Program.cs";

foreach ( string str in More( 10, AddCount( TextFileReader( fileName ) ) ) )
{
Console.WriteLine( str );
Console.WriteLine( "--more--" );
Console.ReadLine();
}
}
catch ( Exception e )
{
Console.WriteLine( e.Message );
}
Console.ReadLine();
}

static IEnumerable<string> More( int count, IEnumerable<string> input )
{
if ( count <= 0 ) throw new ArgumentException( "countは1以上にしてください", "count" );

StringBuilder sb = new StringBuilder();

int n = count;
foreach ( string s in input )
{
sb.AppendLine( s );
n--;

if ( n <= 0 )
{
yield return sb.ToString();
sb.Remove( 0, sb.Length );
n = count;
}
}

if ( 0 < sb.Length )
yield return sb.ToString();
}

static IEnumerable<string> AddCount( IEnumerable<string> input )
{
int count = 1;

foreach ( string s in input )
{
yield return count + " : " + s;
count++;
}
}

static IEnumerable<string> TextFileReader( string fileName )
{
using ( StreamReader sr = new StreamReader( fileName ) )
{
string line;
while ( ( line = sr.ReadLine() ) != null )
yield return line;
}
}

#事情によりしばらく書けません。