Skip to content

Commit 2365e4d

Browse files
committed
Refactor
1 parent b99de64 commit 2365e4d

File tree

8 files changed

+199
-157
lines changed

8 files changed

+199
-157
lines changed

GitChat/Interface.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace GitChat {
2+
public sealed class Interface {
3+
Stage _stage;
4+
5+
public Interface() {
6+
_stage = new MainStage(new State());
7+
}
8+
9+
public void Update() {
10+
_stage.Update();
11+
_stage.Render();
12+
_stage.Input();
13+
_stage = _stage.NewStage ?? _stage;
14+
}
15+
}
16+
}

GitChat/MainStage.cs

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using GitChat.Library;
4+
5+
namespace GitChat {
6+
class MainStage : Stage {
7+
public MainStage(State state) : base(state) {
8+
KeyMapping[ConsoleKey.H] = PrevRepo;
9+
KeyMapping[ConsoleKey.L] = NextRepo;
10+
KeyMapping[ConsoleKey.O] = ChangeToOpen;
11+
KeyMapping[ConsoleKey.Y] = RemoveCurrent;
12+
}
13+
14+
public override void Update() {
15+
var repos = State.Storage.FindRepositories();
16+
PrepareRepositories(repos);
17+
EnsureCurrentRepo();
18+
}
19+
20+
public override void Render() {
21+
base.Render();
22+
RenderHeader();
23+
RenderCurrentMessages();
24+
RenderFooter();
25+
}
26+
27+
void PrepareRepositories(string[] repos) {
28+
foreach ( var repo in repos ) {
29+
if ( !State.Services.ContainsKey(repo) ) {
30+
State.Services[repo] = new ChatService(State.Storage, repoName: repo);
31+
}
32+
}
33+
State.OrderedServices = new List<ChatService>(State.Services.Values);
34+
}
35+
36+
void EnsureCurrentRepo() {
37+
if ( State.SelectedService >= State.OrderedServices.Count ) {
38+
State.SelectedService = 0;
39+
}
40+
}
41+
42+
void RenderHeader() {
43+
var services = State.OrderedServices;
44+
for ( var i = 0; i < services.Count; i++ ) {
45+
var repo = services[i];
46+
var isCurrent = (State.SelectedService == i);
47+
WriteWithColor(repo.RepoName, isCurrent ? ConsoleColor.Green : ConsoleColor.White);
48+
if ( i < (services.Count - 1) ) {
49+
Console.Write(" | ");
50+
}
51+
}
52+
if ( services.Count > 0 ) {
53+
Console.WriteLine();
54+
}
55+
}
56+
57+
void RenderCurrentMessages() {
58+
if ( State.OrderedServices.Count == 0 ) {
59+
Console.WriteLine("No repositories in cache");
60+
return;
61+
}
62+
var service = State.OrderedServices[State.SelectedService];
63+
var messages = service.ReadMessages();
64+
foreach ( var msg in messages ) {
65+
Console.Write(msg.Time);
66+
Console.Write(" ");
67+
WriteWithColor(msg.Author, msg.IsCurrentUser ? ConsoleColor.Green : ConsoleColor.Yellow);
68+
Console.Write(": ");
69+
Console.Write(msg.Content);
70+
Console.WriteLine();
71+
}
72+
}
73+
74+
void RenderFooter() {
75+
WriteWithColor("Move: [K] Move Up [J] Move Down [H] Prev Chat [L] Next Chat", ConsoleColor.Gray);
76+
Console.WriteLine();
77+
WriteWithColor("Chat: [O] Open [Y] Remove [U] Update [I] Reply", ConsoleColor.Gray);
78+
Console.WriteLine();
79+
}
80+
81+
void ChangeToOpen() {
82+
NewStage = new OpenStage(State);
83+
}
84+
85+
void RemoveCurrent() {
86+
var service = State.OrderedServices[State.SelectedService];
87+
State.Storage.Clear(service.WorkingDirectory);
88+
State.Services.Remove(service.WorkingDirectory);
89+
}
90+
91+
void PrevRepo() {
92+
var newIndex = State.SelectedService - 1;
93+
if ( newIndex < 0 ) {
94+
return;
95+
}
96+
State.SelectedService = newIndex;
97+
}
98+
99+
void NextRepo() {
100+
var newIndex = State.SelectedService + 1;
101+
if ( newIndex >= State.OrderedServices.Count ) {
102+
return;
103+
}
104+
State.SelectedService = newIndex;
105+
}
106+
}
107+
}

GitChat/OpenStage.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using GitChat.Library;
3+
4+
namespace GitChat {
5+
class OpenStage : Stage {
6+
public OpenStage(State state) : base(state) {}
7+
8+
public override void Render() {
9+
base.Render();
10+
Console.WriteLine("Your repository URL:");
11+
}
12+
13+
public override void Input() {
14+
var repoUrl = Console.ReadLine();
15+
var service = new ChatService(State.Storage, originUrl: repoUrl);
16+
State.Services[service.WorkingDirectory] = service;
17+
NewStage = new MainStage(State);
18+
}
19+
}
20+
}

GitChat/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
namespace GitChat {
22
class Program {
33
static void Main(string[] args) {
4-
var state = new State();
4+
var ui = new Interface();
55
while ( true ) {
6-
state.Update();
6+
ui.Update();
77
}
88
}
99
}

GitChat/Stage.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace GitChat {
5+
abstract class Stage {
6+
public Stage NewStage = null;
7+
8+
protected readonly State State;
9+
10+
protected Dictionary<ConsoleKey, Action> KeyMapping = new Dictionary<ConsoleKey, Action>();
11+
12+
public Stage(State state) {
13+
State = state;
14+
}
15+
16+
public virtual void Update() {}
17+
18+
public virtual void Render() {
19+
Console.Clear();
20+
}
21+
22+
public virtual void Input() {
23+
if ( KeyMapping.Count > 0 ) {
24+
var key = Console.ReadKey().Key;
25+
if ( KeyMapping.TryGetValue(key, out var handler) ) {
26+
handler();
27+
}
28+
}
29+
}
30+
31+
protected void WriteWithColor(string message, ConsoleColor color) {
32+
var oldColor = Console.ForegroundColor;
33+
Console.ForegroundColor = color;
34+
Console.Write(message);
35+
Console.ForegroundColor = oldColor;
36+
}
37+
}
38+
}

GitChat/State.cs

Lines changed: 5 additions & 146 deletions
Original file line numberDiff line numberDiff line change
@@ -1,153 +1,12 @@
1-
using System;
21
using System.Collections.Generic;
32
using GitChat.Library;
43

54
namespace GitChat {
6-
public sealed class State {
7-
enum Stage {
8-
Main,
9-
Open,
10-
Reply
11-
}
5+
sealed class State {
6+
public readonly CacheStorage Storage = new CacheStorage(".cache");
127

13-
readonly CacheStorage _storage;
14-
readonly Dictionary<Stage, Action> _handlers;
15-
readonly Dictionary<ConsoleKey, Action> _keyMapping;
16-
17-
Stage _stage = Stage.Main;
18-
Dictionary<string, ChatService> _services = new Dictionary<string, ChatService>();
19-
List<ChatService> _orderedServices = new List<ChatService>();
20-
int _selectedService = 0;
21-
22-
public State() {
23-
_storage = new CacheStorage(".cache");
24-
_handlers = new Dictionary<Stage, Action> {
25-
{ Stage.Main, Main },
26-
{ Stage.Open, Open },
27-
};
28-
_keyMapping = new Dictionary<ConsoleKey, Action> {
29-
{ ConsoleKey.O, ChangeToOpen },
30-
{ ConsoleKey.Y, RemoveCurrent },
31-
{ ConsoleKey.H, PrevRepo },
32-
{ ConsoleKey.L, NextRepo },
33-
{ ConsoleKey.U, () => {} },
34-
};
35-
}
36-
37-
public void Update() {
38-
Console.Clear();
39-
_handlers[_stage]();
40-
}
41-
42-
void Main() {
43-
var repos = _storage.FindRepositories();
44-
PrepareRepositories(repos);
45-
RenderHeader(repos);
46-
if ( repos.Length > 0 ) {
47-
EnsureCurrentRepo();
48-
RenderCurrentMessages();
49-
} else {
50-
Console.WriteLine("No repositories in cache");
51-
}
52-
RenderFooter();
53-
ReadMainInput();
54-
}
55-
56-
void PrepareRepositories(string[] repos) {
57-
foreach ( var repo in repos ) {
58-
if ( !_services.ContainsKey(repo) ) {
59-
_services[repo] = new ChatService(_storage, repoName: repo);
60-
}
61-
}
62-
_orderedServices = new List<ChatService>(_services.Values);
63-
}
64-
65-
void RenderHeader(string[] repos) {
66-
for ( var i = 0; i < repos.Length; i++ ) {
67-
var repo = repos[i];
68-
var isCurrent = (_orderedServices[_selectedService] == _services[repo]);
69-
WriteWithColor(repo, isCurrent ? ConsoleColor.Green : ConsoleColor.White);
70-
if ( i < (repos.Length - 1) ) {
71-
Console.Write(" | ");
72-
}
73-
}
74-
if ( repos.Length > 0 ) {
75-
Console.WriteLine();
76-
}
77-
}
78-
79-
void EnsureCurrentRepo() {
80-
if ( _selectedService >= _orderedServices.Count ) {
81-
_selectedService = 0;
82-
}
83-
}
84-
85-
void RenderCurrentMessages() {
86-
var service = _orderedServices[_selectedService];
87-
var messages = service.ReadMessages();
88-
foreach ( var msg in messages ) {
89-
Console.Write(msg.Time);
90-
Console.Write(" ");
91-
WriteWithColor(msg.Author, msg.IsCurrentUser ? ConsoleColor.Green : ConsoleColor.Yellow);
92-
Console.Write(": ");
93-
Console.Write(msg.Content);
94-
Console.WriteLine();
95-
}
96-
}
97-
98-
void RenderFooter() {
99-
WriteWithColor("Move: [K] Move Up [J] Move Down [H] Prev Chat [L] Next Chat", ConsoleColor.Gray);
100-
Console.WriteLine();
101-
WriteWithColor("Chat: [O] Open [Y] Remove [U] Update [I] Reply", ConsoleColor.Gray);
102-
Console.WriteLine();
103-
}
104-
105-
void ReadMainInput() {
106-
var key = Console.ReadKey().Key;
107-
if ( _keyMapping.TryGetValue(key, out var handler) ) {
108-
handler();
109-
}
110-
}
111-
112-
void ChangeToOpen() {
113-
_stage = Stage.Open;
114-
}
115-
116-
void RemoveCurrent() {
117-
var service = _orderedServices[_selectedService];
118-
_storage.Clear(service.WorkingDirectory);
119-
_services.Remove(service.WorkingDirectory);
120-
}
121-
122-
void PrevRepo() {
123-
var newIndex = _selectedService - 1;
124-
if ( newIndex < 0 ) {
125-
return;
126-
}
127-
_selectedService = newIndex;
128-
}
129-
130-
void NextRepo() {
131-
var newIndex = _selectedService + 1;
132-
if ( newIndex >= _orderedServices.Count ) {
133-
return;
134-
}
135-
_selectedService = newIndex;
136-
}
137-
138-
void Open() {
139-
Console.WriteLine("Your repository URL:");
140-
var repoUrl = Console.ReadLine();
141-
var service = new ChatService(_storage, originUrl: repoUrl);
142-
_services[service.WorkingDirectory] = service;
143-
_stage = Stage.Main;
144-
}
145-
146-
static void WriteWithColor(string message, ConsoleColor color) {
147-
var oldColor = Console.ForegroundColor;
148-
Console.ForegroundColor = color;
149-
Console.Write(message);
150-
Console.ForegroundColor = oldColor;
151-
}
8+
public Dictionary<string, ChatService> Services = new Dictionary<string, ChatService>();
9+
public List<ChatService> OrderedServices = new List<ChatService>();
10+
public int SelectedService = 0;
15211
}
15312
}

Library/ChatService.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33

44
namespace GitChat.Library {
55
public sealed class ChatService {
6-
readonly CacheStorage _storage;
7-
readonly GitRunner _git;
8-
readonly string _userName;
6+
readonly GitRunner _git;
7+
readonly string _userName;
98

109
public string WorkingDirectory => _git.WorkingDirectory;
10+
public string RepoName => _git.RepoName;
1111

12-
public ChatService(CacheStorage storage, string originUrl = null, string repoName = null) {
13-
_storage = storage;
14-
_git = new GitRunner(_storage, originUrl, repoName);
12+
public ChatService(CacheStorage storage, string originUrl = null, string repoName = null) {
13+
_git = new GitRunner(storage, originUrl, repoName);
1514
_userName = _git.GetUserName().Trim();
1615
}
1716

0 commit comments

Comments
 (0)