Skip to content

Commit a5ba219

Browse files
committed
Simple File Explorer
1 parent c26d890 commit a5ba219

File tree

5 files changed

+109
-23
lines changed

5 files changed

+109
-23
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ If you have any questions, you can contact me by email morasiu2@gmail.com
3434
## Progress
3535

3636
All - **100** <br>
37-
Done - **16** <br>
38-
Remain - **84** <br>
37+
Done - **18** <br>
38+
Remain - **82** <br>
3939

4040
* <a name="00">00</a> Name Generator - 29.01.2018 *Done* (`Python 3`) <br>
4141
![00](docs/images/00.png)
@@ -85,6 +85,7 @@ string.Concat(text.Reverse())
8585
* <a name="17">17</a>~~Eurelian Path~~<br>
8686
Why I didn't solve that challenge? It sounds fun, but I hate writing GUI and that would be a nightmare for me.
8787
Maybe some day?
88-
* <a name="18">18</a> Simple file Explorer *Pending* (`C#`)<br>
88+
* <a name="18">18</a> Simple file Explorer - 09.08.2018 *Done* (`C#`)<br>
89+
* ![18](docs/images/18.png)
8990
## Bonus
9091
* <a name="bonus1">Bonus 1</a> Loading animation in console 24.02.2018 *Done* (`C#`)![Bonus 1](docs/images/bonus1.gif)

challenges/18_Simple_File_Explorer/SimpleFileExplorer.cs

Lines changed: 104 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,124 @@
33
using System.Collections.Generic;
44
using System.IO;
55
using System.Linq;
6-
using System.Linq.Expressions;
7-
using System.Reflection.Metadata.Ecma335;
86

97
class SimpleFileExplorer {
10-
static void Main(string[] args) {
8+
9+
private static string _currentDirectory = Directory.GetCurrentDirectory();
10+
private const string Version = "0.1.0";
11+
private static int _cursorPosition;
12+
private static string _selectedDirectory;
13+
private static int _currentDirectoryLength;
14+
15+
private static void Main(string[] args) {
1116
Console.ForegroundColor = ConsoleColor.Green;
17+
Console.Title = "Simple file explorer " + Version;
1218
Console.WriteLine("<--------Simple File Explorer--------->");
1319
Console.WriteLine("Created by Morasiu (morasiu2@gmail.com)");
1420

1521
Start();
1622
}
1723

18-
static void Start() {
24+
private static void Start() {
1925
Console.Clear();
20-
Console.WriteLine("..");
21-
var files = Directory.GetFiles(Directory.GetCurrentDirectory()).ToList();
22-
//var files = Directory.GetFiles(@"c:\Users\hmorawski\Downloads").ToList();
23-
foreach (var file in files) {
24-
var fileName = Path.GetFileName(file);
25-
var line = fileName;
26-
var fileSize = GetFileSize(new FileInfo(file).Length);
27-
for (var i = 0; i < Console.WindowWidth - fileName.Length - fileSize.Length - 1; i++) {
28-
line += " ";
29-
}
30-
31-
line += fileSize;
32-
Console.WriteLine(line);
26+
while (true) {
27+
var files = GetFileList(_currentDirectory);
28+
_currentDirectoryLength = files.Count;
29+
RefreshScreen(files);
30+
ManageInput();
31+
}
32+
}
33+
34+
private static void ManageInput() {
35+
var key = Console.ReadKey().Key;
36+
switch (key) {
37+
case ConsoleKey.Q:
38+
Environment.Exit(0);
39+
break;
40+
case ConsoleKey.F1:
41+
DisplayInstruction();
42+
break;
43+
case ConsoleKey.UpArrow:
44+
ChangeCurrentPositionBy(-1);
45+
break;
46+
case ConsoleKey.DownArrow:
47+
ChangeCurrentPositionBy(1);
48+
break;
49+
case ConsoleKey.LeftArrow:
50+
_currentDirectory = Directory.GetParent(_currentDirectory).FullName;
51+
_cursorPosition = 0;
52+
break;
53+
case ConsoleKey.RightArrow:
54+
if (IsFileDirectory(_selectedDirectory)) {
55+
_currentDirectory = _selectedDirectory;
56+
_cursorPosition = 0;
57+
}
58+
break;
3359
}
60+
}
3461

62+
private static void ChangeCurrentPositionBy(int change) {
63+
_cursorPosition += change;
64+
if (_cursorPosition < 0) _cursorPosition = 0;
65+
else if (_cursorPosition > _currentDirectoryLength - 1) _cursorPosition = _currentDirectoryLength - 1;
66+
}
67+
68+
private static void DisplayInstruction() {
69+
Console.Clear();
70+
Console.WriteLine("Simple File Explorer v" + Version);
71+
Console.WriteLine("Created by Morasiu in 2018");
72+
Console.WriteLine("--------------------------\n");
73+
Console.WriteLine("1. Use arrows to naviagte");
74+
Console.WriteLine("\nPress any key to bo back");
75+
Console.ReadKey();
76+
}
77+
78+
private static void RefreshScreen(List<string> files) {
79+
Console.Clear();
80+
WriteAllFiles(files);
3581
MakeEmptyLines(files);
82+
WriteInstructionOnBottom();
83+
}
3684

37-
Console.Write(" q - Quit");
38-
var key = Console.ReadKey().Key;
39-
if (key == ConsoleKey.Q) Environment.Exit(0);
85+
private static void WriteInstructionOnBottom() {
86+
Console.Write("F1 - Help; Arrows - control; q - Quit");
87+
}
88+
89+
private static void WriteAllFiles(IReadOnlyList<string> files) {
90+
Console.WriteLine("..");
91+
for (var i = 0; i < files.Count(); i++) {
92+
WriteFileInfo(files[i], i == _cursorPosition);
93+
}
94+
}
95+
96+
private static void WriteFileInfo(string file, bool addCursor) {
97+
var fileName = Path.GetFileName(file);
98+
if (addCursor) {
99+
fileName += " <-";
100+
_selectedDirectory = file;
101+
}
102+
var line = fileName;
103+
var fileSize = "DIR";
104+
if (!IsFileDirectory(file)) fileSize = GetFileSize(new FileInfo(file).Length);
105+
line = FillLineWithSpaces(fileName, line, fileSize);
106+
line += fileSize;
107+
Console.WriteLine(line);
108+
}
109+
110+
private static bool IsFileDirectory(string file) {
111+
return (File.GetAttributes(file) & FileAttributes.Directory) != 0;
112+
}
113+
114+
private static string FillLineWithSpaces(string fileName, string line, string fileSize) {
115+
for (var i = 0; i < Console.WindowWidth - fileName.Length - fileSize.Length - 1; i++) {
116+
line += " ";
117+
}
118+
119+
return line;
120+
}
121+
122+
private static List<string> GetFileList(string directory) {
123+
return Directory.GetFileSystemEntries(directory).ToList();
40124
}
41125

42126
private static string GetFileSize(long size) {
7 KB
Binary file not shown.

challenges/Template.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
class Template{
44
static void Main(string[] args){
55
Console.ForegroundColor = ConsoleColor.Green;
6+
Console.Title = "Templete";
67
Console.WriteLine("<--------------Template--------------->");
78
Console.WriteLine("Created by Morasiu (morasiu2@gmail.com)");
89
Console.ReadKey();

docs/images/18.PNG

21 KB
Loading

0 commit comments

Comments
 (0)