Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Shingo committed Apr 27, 2019
1 parent 9610a5f commit 01f395b
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 0 deletions.
6 changes: 6 additions & 0 deletions App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
97 changes: 97 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace ShingoTree
{
class Program
{
static void Main(string[] args)
{
if(args.Length == 0)
{
PrintHelp();
return;
}

List<string> pathes = new List<string>();
Runner runner = BuildRunner(args, pathes);

if (pathes.Count == 0)
pathes.Add(Environment.CurrentDirectory);

runner.Lookup(pathes);
}

private static void PrintHelp()
{
Console.WriteLine("以图形显示驱动器或路径的文件夹结构。");
Console.WriteLine();
Console.WriteLine("SHINGOTREE [/F] [/O output [/V]] [path, ...]");
Console.WriteLine();
Console.WriteLine(" /F 显示每个文件夹中文件的名称。");
Console.WriteLine(" /O 将结果输出到文件。");
Console.WriteLine(" /V 显示当前进度。");
Console.WriteLine();
}

private static Runner BuildRunner(string[] args, List<string> pathes)
{
bool printFile = false, output = false, progress = false;
string outputFile = null;

foreach (var arg in args)
{
switch (arg)
{
case "/F":
case "/f":
printFile = true;
break;
case "/O":
case "/o":
if (outputFile != null)
{
Console.WriteLine("参数错误,指定了多个输出文件。");
return null;
}
output = true;
break;
case "/V":
case "/v":
progress = true;
break;

default:
if (output)
{
output = false;
outputFile = arg;
}
else
{
pathes.Add(arg);
}
break;
}
}

TextWriter outputStream;
if (outputFile == null)
{
progress = false;
outputStream = Console.Out;
}
else
outputStream = new StreamWriter(outputFile, false, Encoding.UTF8);

return new Runner
{
printFile = printFile,
output = outputStream,
progress = progress,
};
}
}
}
36 changes: 36 additions & 0 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ShingoTree")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ShingoTree")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("5b66317c-eda3-4bd1-818e-48a4b1bb64a1")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
SHINGOTREE [/F] [/O output [/V]] [path, ...]

/F Show files.
/O Output to file.
/V Show progress.
101 changes: 101 additions & 0 deletions Runner.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using System.IO;

namespace ShingoTree
{
class Runner
{
public bool printFile;
public bool progress;
public TextWriter output;

private double _progress;
private int _lastReportProgress;

void LookUpDirectory(string path, string pprefix, double maxprog)
{
string[] directories = Directory.GetDirectories(path);
int dirLength = directories.Length;

if (printFile)
{
string[] files = Directory.GetFiles(path);
if (files.Length > 0)
{
string prefix = pprefix + (dirLength > 0 ? "│ " : " ");
foreach (var file in files)
{
output.Write(prefix);
output.WriteLine(file.Substring(path.Length + 1));
}
output.WriteLine(prefix);
}
}

if (dirLength > 0)
{
double prog = _progress;
double progAdv = (maxprog - prog) / dirLength;

int dirLengthM1 = dirLength - 1;

if (dirLengthM1 > 0)
{
string prefix = pprefix + "├─";
string subPrefix = pprefix + "│ ";
for (int i = 0; i < dirLengthM1; i++)
OutputDirectory(path, directories[i], prog += progAdv, prefix, subPrefix);
}
OutputDirectory(path, directories[dirLengthM1], prog + progAdv,
pprefix + "└─", pprefix + " ");
}
}

void OutputDirectory(string path, string directory, double maxprog, string prefix, string subPrefix)
{
output.Write(prefix);
output.WriteLine(directory.Substring(path.Length + 1));
LookUpDirectory(directory, subPrefix, maxprog);
ReportProgress(maxprog);
}

void ReportProgress(double p)
{
const int PROGRESS_BAR_WIDTH = 70;

if (progress)
{
int intProg = (int)(p * 1000);
if (intProg != _lastReportProgress)
{
_lastReportProgress = intProg;
Console.Write("\r[");
int w = (int)Math.Round(p * PROGRESS_BAR_WIDTH);
Console.Write(new string('=', w));
Console.Write(new string(' ', PROGRESS_BAR_WIDTH - w));
Console.Write("] " + p.ToString("#.0%"));
}
}
_progress = p;
}

internal void Lookup(List<string> pathes)
{
_progress = 0.0;
_lastReportProgress = 0;
int count = pathes.Count;
double maxprog = 0.0;

for (int i = 0; i < count; i++)
{
string path = Path.GetFullPath(pathes[i]);
output.WriteLine(path);
maxprog += 1.0 / count;
LookUpDirectory(path, "", maxprog);
ReportProgress(maxprog);
output.WriteLine();
}
}
}
}
54 changes: 54 additions & 0 deletions ShingoTree.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{5B66317C-EDA3-4BD1-818E-48A4B1BB64A1}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>ShingoTree</RootNamespace>
<AssemblyName>ShingoTree</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Runner.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

0 comments on commit 01f395b

Please sign in to comment.