[theminds commit] r145 - in trunk: Quirk Structures Tabs Theminds Theminds/Filters Theminds/Tests

0 views
Skip to first unread message

codesite...@google.com

unread,
Aug 1, 2007, 10:16:20 PM8/1/07
to theminds...@googlegroups.com
Author: shadytrees
Date: Wed Aug 1 19:15:01 2007
New Revision: 145

Modified:
trunk/Quirk/Quirk.cs
trunk/Quirk/Quirk.csproj
trunk/Structures/StringEx.cs
trunk/Tabs/Tabber.cs
trunk/Theminds/App.cs
trunk/Theminds/AppControls.cs
trunk/Theminds/Filters/JoinPartQuit.cs
trunk/Theminds/Tests/JoinPartQuit.cs
trunk/Theminds/Tests/MockApp.cs
trunk/Theminds/Tests/PrivmsgFilter.cs

Log:
Object initializers and clarity

Modified: trunk/Quirk/Quirk.cs
==============================================================================
--- trunk/Quirk/Quirk.cs (original)
+++ trunk/Quirk/Quirk.cs Wed Aug 1 19:15:01 2007
@@ -1,5 +1,5 @@
-// Core server logic for Theminds. She takes a QuirkStart for input
-// and outputs via the Line event. Very self-contained.
+// Core server logic for Theminds and IRC client.
+// She takes a QuirkStart for input and outputs via the Line event.

using System;
using System.Net;
@@ -8,39 +8,33 @@
using System.Threading;
using System.Diagnostics;

-
[assembly: System.Runtime.InteropServices.ComVisible(false)]
[assembly: System.Reflection.AssemblyVersionAttribute("1.3")]
[assembly: System.CLSCompliant(true)]
[assembly: System.Security.Permissions.SecurityPermission(
System.Security.Permissions.SecurityAction.RequestMinimum, Execution = true)]
namespace Aspirations {
- // An IRC client class
public sealed partial class Quirk : IDisposable {
public QuirkStart Info;
+ static Random rnd = new Random();

// Everytime Connection has a new line, we send it to this event.
- // NOT LogBox's NewLine. This merely glues Connection and LogBox.
+ // NOT Buffer's NewLine. This merely glues Connection and LogBox.
public delegate void NewLineDel(string line);
public event NewLineDel NewLine;

- static Random rndAddressIndex = new Random();
-
bool dnsResolved;
public Quirk(QuirkStart connectionInfo) {
this.Info = connectionInfo;
+ dnsResolved = false;

try {
- IPAddress[] x = Dns.
- GetHostEntry(Info.Server).AddressList;
- this.Info.Server =
- x[rndAddressIndex.Next(x.Length)].ToString();
+ var x = Dns.GetHostEntry(Info.Server).AddressList;
+ this.Info.Server = x[rnd.Next(x.Length)].ToString();
dnsResolved = true;
}
catch (SocketException) {
- NewLine(String.Format(
- "Could not resolve {0}.", Info.Server));
- dnsResolved = false;
+ NewLine("Could not resolve {0}.".Fill(Info.Server));
}
}

@@ -48,8 +42,9 @@
public bool Started = false;
public void Start() {
if (this.Started || !dnsResolved) return;
- connectThread = new Thread(new ThreadStart(connect));
- connectThread.IsBackground = true;
+
+ var x = new ThreadStart(connect);
+ connectThread = new Thread(x) { IsBackground = true };
connectThread.Start();
this.Started = true;
}
@@ -63,15 +58,15 @@
Message(String.Format(line, args));
}

- // IDisposable
+ // IDisposable with the twist in that it takes an
+ // IRC quit message. I live on the edge.
bool disposed = false;
public void Dispose() { Dispose(null); }
public void Dispose(string quitMsg) {
- if (disposed) return;
- if (null == connectThread) return;
+ if (disposed || null == connectThread) return;
disposed = true;
-
if (null == writer) return;
+
try {
if (null == quitMsg) Message("QUIT");
else Message("QUIT " + quitMsg);
@@ -86,35 +81,40 @@
StreamReader reader;
void connect() {
Stream stream;
- try { stream = new TcpClient(Info.Server,
- Info.Port).GetStream(); }
+ try {
+ var x = new TcpClient(Info.Server, Info.Port);
+ stream = x.GetStream();
+ }
catch (SocketException) {
- NewLine(String.Format("Could not connect to \"{0}\".",
- Info.Server)); return;
+ var msg = "Could not connect to \"{0}\".".Fill(Info.Server);
+ NewLine(msg); return;
}
+
reader = new StreamReader(stream);
writer = new StreamWriter(stream);
writer.AutoFlush = true;

Message("NICK {0}\n{1}", Info.Nick, Info.User);
+
+ // Start the event pump and keep it running until we have
+ // to Dispose() at the end of the application runtime, at
+ // which point end _very, very carefully_ and quickly.
while (!disposed) pump();
writer.Dispose(); reader.Dispose();
}

void pump() {
string line = null;
+ Action<Exception> h = e => {
+ NewLine(e.ToString());
+ this.Dispose();
+ };
+
try { line = reader.ReadLine(); }
- catch (IOException e) { handleException(e); }
- catch (OutOfMemoryException e) {
- handleException(e); }
-
- if (line == null) return;
- NewLine(line);
- }
+ catch (IOException e) { h(e); }
+ catch (OutOfMemoryException e) { h(e); }

- void handleException(Exception e) {
- NewLine(e.ToString());
- this.Dispose();
+ if (line != null) NewLine(line);
}

public override int GetHashCode() {
@@ -122,7 +122,7 @@
}

public override bool Equals(object obj) {
- Quirk q = (Quirk)obj;
+ var q = (Quirk)obj;
return (this.Info.Equals(q.Info));
}
}

Modified: trunk/Quirk/Quirk.csproj
==============================================================================
--- trunk/Quirk/Quirk.csproj (original)
+++ trunk/Quirk/Quirk.csproj Wed Aug 1 19:15:01 2007
@@ -3,7 +3,7 @@
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
- <ProductVersion>8.0.50727</ProductVersion>
+ <ProductVersion>9.0.20706</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{38B595B3-1466-4D51-9EB2-CD2A095A6ACE}</ProjectGuid>
<OutputType>Library</OutputType>
@@ -50,5 +50,11 @@
<ItemGroup>
<Compile Include="Quirk.cs" />
<Compile Include="QuirkStart.cs" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\Structures\Structures.csproj">
+ <Project>{33D6C14F-9C5C-4B5D-8D4C-0B1F75215A9E}</Project>
+ <Name>Structures</Name>
+ </ProjectReference>
</ItemGroup>
</Project>

Modified: trunk/Structures/StringEx.cs
==============================================================================
--- trunk/Structures/StringEx.cs (original)
+++ trunk/Structures/StringEx.cs Wed Aug 1 19:15:01 2007
@@ -30,5 +30,9 @@
if (channel.StartsWith("&")) return true;
return false;
}
+
+ public static string Fill(this string template, params object[] args) {
+ return String.Format(template, args);
+ }
}
}

Modified: trunk/Tabs/Tabber.cs
==============================================================================
--- trunk/Tabs/Tabber.cs (original)
+++ trunk/Tabs/Tabber.cs Wed Aug 1 19:15:01 2007
@@ -42,30 +42,30 @@
// lastIndex + 1, or Count.
current = tabs.Count;

- ITab x = this.parent.CreateTab(label);
- this.NewTab(x);
+ ITab tab = this.parent.CreateTab(label);
+ this.NewTab(tab);

- this.right += x.Width;
- this.realWidth += x.TrueWidth;
+ this.right += tab.Width;
+ this.realWidth += tab.TrueWidth;

parent.GrabFocus();
resize();

- return x;
+ return tab;
}

+ public void Add() { Add(this.defaultLabel); }
public void Add(string[] labels) {
foreach (string label in labels) Add(label);
}
- public void Add() { Add(this.defaultLabel); }

public event TabDel Moved;
+ public void MoveTo(int index) { MoveTo(tabs[index]); }
public void MoveTo(ITab tab) {
int index = tabs.IndexOf(tab);
c(() => { current = index; parent.GrabFocus(); });
this.Moved(tab);
}
- public void MoveTo(int index) { MoveTo(tabs[index]); }

public void MoveToNext() {
c(() => current = (current + 1) % tabs.Count);
@@ -79,19 +79,18 @@
}

public event TabDel Removed;
+ static string outOfBoundsRemove =
+ "I am removing a tab that does not exist. Make sure to call Init.";
public void RemoveCurrent() { Remove(current); }
public void Remove(int index) {
if (tabs.Count < current + 1) {
- throw new InvalidOperationException(
- "I am removing a tab that does not exist. Make sure to call Init.");
+ throw new InvalidOperationException(outOfBoundsRemove);
}
ITab x = tabs[index];
int width = x.Width;
this.realWidth -= x.TrueWidth;

- // Places to clean up:
- // * Parent's collection
- // * Our collection
+ // Clean up parent's collection and our collection
parent.SuspendLayout();
parent.RemoveTab((System.Windows.Forms.Control)x);
tabs.Remove(x);
@@ -140,11 +139,11 @@
parent.ResumeLayout();
}

+ // Stop when true:
+ // * Close last, current one -> go to the new last tab.
+ // * Close tab after current tab -> stay, no changes.
+ // * Close tab before current tab -> update number.
void removeHelperForCounters(int index, int width) {
- // Stop when true:
- // * Close last, current one -> go to the new last tab.
- // * Close tab after current tab -> stay, no changes.
- // * Close tab before current tab -> update number.
if (tabs.Count > 0) {
if (index < current || current >= tabs.Count) {
current -= 1;
@@ -156,5 +155,5 @@
Add(); current = 0;
}
}
- } // class TabsController
-} // namespace
\ No newline at end of file
+ } // class Tabber
+}
\ No newline at end of file

Modified: trunk/Theminds/App.cs
==============================================================================
--- trunk/Theminds/App.cs (original)
+++ trunk/Theminds/App.cs Wed Aug 1 19:15:01 2007
@@ -6,28 +6,20 @@

namespace Theminds {
public sealed partial class App : Form {
- string currentChannel;
- public string CurrentChannel {
- get { return currentChannel; }
- set { currentChannel = value; }
- }
-
public static Ideas Lion = new Ideas(@"lion.txt", true);
-
Buffer buffer; Quirk quirk;
public App() {
this.SetUpForm(); // SetUpForm.cs

- QuirkStart mozNet = new QuirkStart();
- mozNet.Nick = "Tongue"; mozNet.Port = 6667;
- mozNet.Server = "irc.mozilla.org";
- mozNet.User = "USER cryptoliter2 8 * :Hi";
+ var mozNet = new QuirkStart() {
+ Nick = "Tongue", Port = 6667,
+ Server = "irc.mozilla.org", User = "USER cryptoliter2 8 * :Hi",
+ };
quirk = new Quirk(mozNet);

this.buffer = new Buffer(this, quirk);
quirk.NewLine += new Quirk.NewLineDel(Buffer.AddLine);
- App.LoadAttributeLovers(
- typeof(DesiresAppControlsAttribute), this);
+ App.LoadAttributeLovers(typeof(DesiresAppControlsAttribute), this);

PostOffice();
quirk.Start();
@@ -75,14 +67,13 @@
Type[] types = Assembly.GetExecutingAssembly().GetTypes();
try {
foreach (Type type in types) {
- object[] oneNightStands =
- type.GetCustomAttributes(attribute, false);
- if (oneNightStands.Length < 1) continue;
+ var plugins = type.GetCustomAttributes(attribute, false);
+ if (plugins.Length < 1) continue;
Activator.CreateInstance(type, args);
}
}
catch (TargetInvocationException e) {
- Exception x = e.InnerException;
+ var x = e.InnerException;
throw new InvalidOperationException(x.Message + x.StackTrace);
}
}

Modified: trunk/Theminds/AppControls.cs
==============================================================================
--- trunk/Theminds/AppControls.cs (original)
+++ trunk/Theminds/AppControls.cs Wed Aug 1 19:15:01 2007
@@ -46,6 +46,8 @@
get { return buffer; }
}

+ public string CurrentChannel { get; set; }
+
public void SwitchLogBox(LogBox l) {
if (l == logBoxPanel.Controls[0]) return;
this.SuspendLayout();

Modified: trunk/Theminds/Filters/JoinPartQuit.cs
==============================================================================
--- trunk/Theminds/Filters/JoinPartQuit.cs (original)
+++ trunk/Theminds/Filters/JoinPartQuit.cs Wed Aug 1 19:15:01 2007
@@ -1,6 +1,5 @@
using System.Drawing;
using Aspirations;
-using S = System.String;

namespace Theminds.Filters {
[DesiresAppControls]
@@ -59,18 +58,16 @@
int index = notes.ReasonIndex;
if (0 == index || notes.Line.Length <= index) return;
string reason = lion.Get(notes.Mode, "reason");
- reason = S.Format(reason, notes.Line.Substring(index));
- data.Line = S.Format(lion.Get(notes.Mode, "total"),
- data.Line, reason);
+ reason = reason.Fill(notes.Line.Substring(index));
+ data.Line = lion.Get(notes.Mode, "total").Fill(data.Line, reason);
}

void findMessage(ref BufferData data, ref JazzNotes notes) {
if (notes.FromMe) {
- data.Line = S.Format(lion.Get(notes.Mode, "self"),
- data.Channel); return;
+ data.Line = lion.Get(notes.Mode, "self").Fill(data.Channel); return;
}
- data.Line = S.Format(lion.Get(notes.Mode, "others"),
- notes.Nick, notes.Ip, data.Channel);
+ data.Line = lion.Get(notes.Mode, "others").
+ Fill(notes.Nick, notes.Ip, data.Channel);
}

void findNickAndIp(ref JazzNotes notes) {

Modified: trunk/Theminds/Tests/JoinPartQuit.cs
==============================================================================
--- trunk/Theminds/Tests/JoinPartQuit.cs (original)
+++ trunk/Theminds/Tests/JoinPartQuit.cs Wed Aug 1 19:15:01 2007
@@ -2,7 +2,6 @@
using System.Diagnostics;
using Theminds;
using Aspirations;
-using S = System.String;

namespace Theminds.Tests {
[DesiresTestingWithMockApp]
@@ -27,47 +26,41 @@

// join, others
test(":maria!ip JOIN :#spreadbutter",
- S.Format(joinOthers, "maria", "ip", channel), "maria");
+ joinOthers.Fill("maria", "ip", channel), "maria");
// join, self
test(":Tongue!ip JOIN :#spreadbutter",
- S.Format(joinSelf, channel), "tips");
+ joinSelf.Fill(channel), "tips");
// part, others, no reason
test(":Tongues!ip PART #spreadbutter",
- S.Format(partOthers, "Tongues", "ip", channel),
+ partOthers.Fill("Tongues", "ip", channel),
"roses");
// part, others, a reason
test(":Tongues!ip PART #spreadbutter :Too angsty",
- S.Format(partTotal,
- S.Format(partOthers, "Tongues", "ip", channel),
- S.Format(partReason, "Too angsty")), "angst");
+ partTotal.Fill(partOthers.Fill("Tongues", "ip", channel),
+ partReason.Fill("Too angsty")), "angst");

// And now those that do not need no stinking channel.
this.channel = null;
// part, self, no reason
test(":Tongue!ip PART #spreadbutter",
- S.Format(partSelf, "#spreadbutter"), "soulz");
+ partSelf.Fill("#spreadbutter"), "soulz");
// part, self, a reason
test(":Tongue!ip PART #spreadbutter :Too verklempt",
- S.Format(partTotal,
- S.Format(partSelf, "#spreadbutter"),
- S.Format(partReason, "Too verklempt")),
+ partTotal.Fill(partSelf.Fill("#spreadbutter"), partReason.Fill("Too verklempt")),
"verklempt");
// quit, others, a reason
test(":t!ip QUIT :Goodbye cruelle world",
- S.Format(quitTotal, S.Format(quitOthers, "t", "ip"),
- S.Format(quitReason, "Goodbye cruelle world")),
+ quitTotal.Fill(quitOthers.Fill("t", "ip"), quitReason.Fill("Goodbye cruelle world")),
"poverty");
// quit, others, no reason
- test(":Tongues!ip QUIT", S.Format(quitOthers, "Tongues", "ip"),
+ test(":Tongues!ip QUIT", quitOthers.Fill("Tongues", "ip"),
"slip");
// quit, self, a reason
test(":Tongue!ip QUIT :Goodbye cruelle monkey",
- S.Format(quitTotal, S.Format(quitSelf),
- S.Format(quitReason, "Goodbye cruelle monkey")),
+ quitTotal.Fill(quitSelf, quitReason.Fill("Goodbye cruelle monkey")),
"jane");
// quit, self, no reason
- test(":Tongue!ip QUIT",
- S.Format(quitSelf), "short");
+ test(":Tongue!ip QUIT", quitSelf, "short");
// should ignore
test(":Tongue!ip MODE :+x", "estar en moda");
}

Modified: trunk/Theminds/Tests/MockApp.cs
==============================================================================
--- trunk/Theminds/Tests/MockApp.cs (original)
+++ trunk/Theminds/Tests/MockApp.cs Wed Aug 1 19:15:01 2007
@@ -16,10 +16,10 @@
Buffer buffer; Quirk quirk;
Tabber tabber;
public MockApp() {
- QuirkStart mozNet = new QuirkStart();
- mozNet.Nick = "Tongue"; mozNet.Port = 6667;
- mozNet.Server = "irc.mozilla.org";
- mozNet.User = "USER cryptoliter2 8 * :Hi";
+ QuirkStart mozNet = new QuirkStart() {
+ Nick = "Tongue", Port = 6667,
+ Server = "irc.mozilla.org", User = "USER cryptoliter2 8 * :Hi",
+ };
quirk = new Quirk(mozNet);

this.tabber = new Tabber(this, "(new)");
@@ -93,6 +93,6 @@
app.Buffer.PostLine -= girls;
}

- public event MethodInvoker PostOffice;
+ public event MethodInvoker PostOffice = delegate { };
}
}

Modified: trunk/Theminds/Tests/PrivmsgFilter.cs
==============================================================================
--- trunk/Theminds/Tests/PrivmsgFilter.cs (original)
+++ trunk/Theminds/Tests/PrivmsgFilter.cs Wed Aug 1 19:15:01 2007
@@ -44,8 +44,7 @@
string nick, string msg, string id) {
if (null == template) template = line;
MockApp.PokeBuffer(app, line, (ref BufferData data) => {
- if (channel == data.Channel &&
- S.Format(template, nick, msg) == data.Line) return;
+ if (channel == data.Channel && template.Fill(nick, msg) == data.Line) return;
throw new InvalidOperationException("PrimvsgFilter failure in filter() " + id);
});
}

Reply all
Reply to author
Forward
0 new messages