Skip to content

Commit 01f395b

Browse files
author
Shingo
committed
init
1 parent 9610a5f commit 01f395b

File tree

6 files changed

+299
-0
lines changed

6 files changed

+299
-0
lines changed

App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
5+
</startup>
6+
</configuration>

Program.cs

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Text;
5+
6+
namespace ShingoTree
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
if(args.Length == 0)
13+
{
14+
PrintHelp();
15+
return;
16+
}
17+
18+
List<string> pathes = new List<string>();
19+
Runner runner = BuildRunner(args, pathes);
20+
21+
if (pathes.Count == 0)
22+
pathes.Add(Environment.CurrentDirectory);
23+
24+
runner.Lookup(pathes);
25+
}
26+
27+
private static void PrintHelp()
28+
{
29+
Console.WriteLine("以图形显示驱动器或路径的文件夹结构。");
30+
Console.WriteLine();
31+
Console.WriteLine("SHINGOTREE [/F] [/O output [/V]] [path, ...]");
32+
Console.WriteLine();
33+
Console.WriteLine(" /F 显示每个文件夹中文件的名称。");
34+
Console.WriteLine(" /O 将结果输出到文件。");
35+
Console.WriteLine(" /V 显示当前进度。");
36+
Console.WriteLine();
37+
}
38+
39+
private static Runner BuildRunner(string[] args, List<string> pathes)
40+
{
41+
bool printFile = false, output = false, progress = false;
42+
string outputFile = null;
43+
44+
foreach (var arg in args)
45+
{
46+
switch (arg)
47+
{
48+
case "/F":
49+
case "/f":
50+
printFile = true;
51+
break;
52+
case "/O":
53+
case "/o":
54+
if (outputFile != null)
55+
{
56+
Console.WriteLine("参数错误,指定了多个输出文件。");
57+
return null;
58+
}
59+
output = true;
60+
break;
61+
case "/V":
62+
case "/v":
63+
progress = true;
64+
break;
65+
66+
default:
67+
if (output)
68+
{
69+
output = false;
70+
outputFile = arg;
71+
}
72+
else
73+
{
74+
pathes.Add(arg);
75+
}
76+
break;
77+
}
78+
}
79+
80+
TextWriter outputStream;
81+
if (outputFile == null)
82+
{
83+
progress = false;
84+
outputStream = Console.Out;
85+
}
86+
else
87+
outputStream = new StreamWriter(outputFile, false, Encoding.UTF8);
88+
89+
return new Runner
90+
{
91+
printFile = printFile,
92+
output = outputStream,
93+
progress = progress,
94+
};
95+
}
96+
}
97+
}

Properties/AssemblyInfo.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("ShingoTree")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("ShingoTree")]
13+
[assembly: AssemblyCopyright("Copyright © 2019")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("5b66317c-eda3-4bd1-818e-48a4b1bb64a1")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
SHINGOTREE [/F] [/O output [/V]] [path, ...]
2+
3+
/F Show files.
4+
/O Output to file.
5+
/V Show progress.

Runner.cs

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
5+
namespace ShingoTree
6+
{
7+
class Runner
8+
{
9+
public bool printFile;
10+
public bool progress;
11+
public TextWriter output;
12+
13+
private double _progress;
14+
private int _lastReportProgress;
15+
16+
void LookUpDirectory(string path, string pprefix, double maxprog)
17+
{
18+
string[] directories = Directory.GetDirectories(path);
19+
int dirLength = directories.Length;
20+
21+
if (printFile)
22+
{
23+
string[] files = Directory.GetFiles(path);
24+
if (files.Length > 0)
25+
{
26+
string prefix = pprefix + (dirLength > 0 ? "│ " : " ");
27+
foreach (var file in files)
28+
{
29+
output.Write(prefix);
30+
output.WriteLine(file.Substring(path.Length + 1));
31+
}
32+
output.WriteLine(prefix);
33+
}
34+
}
35+
36+
if (dirLength > 0)
37+
{
38+
double prog = _progress;
39+
double progAdv = (maxprog - prog) / dirLength;
40+
41+
int dirLengthM1 = dirLength - 1;
42+
43+
if (dirLengthM1 > 0)
44+
{
45+
string prefix = pprefix + "├─";
46+
string subPrefix = pprefix + "│ ";
47+
for (int i = 0; i < dirLengthM1; i++)
48+
OutputDirectory(path, directories[i], prog += progAdv, prefix, subPrefix);
49+
}
50+
OutputDirectory(path, directories[dirLengthM1], prog + progAdv,
51+
pprefix + "└─", pprefix + " ");
52+
}
53+
}
54+
55+
void OutputDirectory(string path, string directory, double maxprog, string prefix, string subPrefix)
56+
{
57+
output.Write(prefix);
58+
output.WriteLine(directory.Substring(path.Length + 1));
59+
LookUpDirectory(directory, subPrefix, maxprog);
60+
ReportProgress(maxprog);
61+
}
62+
63+
void ReportProgress(double p)
64+
{
65+
const int PROGRESS_BAR_WIDTH = 70;
66+
67+
if (progress)
68+
{
69+
int intProg = (int)(p * 1000);
70+
if (intProg != _lastReportProgress)
71+
{
72+
_lastReportProgress = intProg;
73+
Console.Write("\r[");
74+
int w = (int)Math.Round(p * PROGRESS_BAR_WIDTH);
75+
Console.Write(new string('=', w));
76+
Console.Write(new string(' ', PROGRESS_BAR_WIDTH - w));
77+
Console.Write("] " + p.ToString("#.0%"));
78+
}
79+
}
80+
_progress = p;
81+
}
82+
83+
internal void Lookup(List<string> pathes)
84+
{
85+
_progress = 0.0;
86+
_lastReportProgress = 0;
87+
int count = pathes.Count;
88+
double maxprog = 0.0;
89+
90+
for (int i = 0; i < count; i++)
91+
{
92+
string path = Path.GetFullPath(pathes[i]);
93+
output.WriteLine(path);
94+
maxprog += 1.0 / count;
95+
LookUpDirectory(path, "", maxprog);
96+
ReportProgress(maxprog);
97+
output.WriteLine();
98+
}
99+
}
100+
}
101+
}

ShingoTree.csproj

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{5B66317C-EDA3-4BD1-818E-48A4B1BB64A1}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>ShingoTree</RootNamespace>
10+
<AssemblyName>ShingoTree</AssemblyName>
11+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Runner.cs" />
47+
<Compile Include="Program.cs" />
48+
<Compile Include="Properties\AssemblyInfo.cs" />
49+
</ItemGroup>
50+
<ItemGroup>
51+
<None Include="App.config" />
52+
</ItemGroup>
53+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
54+
</Project>

0 commit comments

Comments
 (0)