Forráskód Böngészése

Adding to docs, add more to color.

Steve Thielemann 1 éve
szülő
commit
0de37676c9
3 módosított fájl, 67 hozzáadás és 121 törlés
  1. 2 2
      color.go
  2. 11 92
      color_test.go
  3. 54 27
      irc-client.go

+ 2 - 2
color.go

@@ -10,7 +10,7 @@ import (
 func Stripper(input string) string {
 	var rx *regexp.Regexp
 	var err error
-	rx, err = regexp.Compile("\x02|\x1d|\x16|\x1e|\x11|\x16|\x0f|\x03[0-9]{2}")
+	rx, err = regexp.Compile("\x02|\x1d|\x16|\x1e|\x11|\x16|\x1f|\x0f|\x03[0-9]{2}")
 	if err != nil {
 		log.Panic("regexp:", err)
 	}
@@ -30,7 +30,7 @@ func Italics(input string) string {
 
 // Set IRC underline
 func Underline(input string) string {
-	return "\x16" + input + "\x16"
+	return "\x1f" + input + "\x1f"
 }
 
 // Set IRC strike through

+ 11 - 92
color_test.go

@@ -43,11 +43,11 @@ func DryAttributeTest(t *testing.T, attr Attr) {
 		expect = attr.Code + sent + attr.Code
 		got = attr.Func(sent)
 		if got != expect {
-			t.Errorf("%s: Got %#v, expected %#v", attr.Name, got, expect)
+			t.Errorf("%s: Got %q, expected %q", attr.Name, got, expect)
 		}
 		plain = Stripper(got)
 		if plain != sent {
-			t.Errorf("%s: Got %#v, expected %#v", attr.Name, got, expect)
+			t.Errorf("%s: stripped Got %q, expected %q", attr.Name, plain, sent)
 		}
 	}
 }
@@ -56,103 +56,22 @@ func TestAttributes(t *testing.T) {
 	Attrs := []Attr{
 		{"Bold", Bold, "\x02"},
 		{"Ttalics", Italics, "\x1d"},
+		{"Underline", Underline, "\x1f"},
+		{"Strike", Strike, "\x1e"},
+		{"Reverse", Reverse, "\x16"},
+		{"Monospace", Monospace, "\x11"},
 	}
 
 	for _, attr := range Attrs {
 		DryAttributeTest(t, attr)
 	}
-	/*
-		var sent string
-		var expect string
-		var got string
-		var plain string
-		for sent, expect = range map[string]string{"One": "\x02One\x02",
-			"Two":   "\x02Two\x02",
-			"Three": "\x02Three\x02"} {
-			got = Bold(sent)
-			if got != expect {
-				t.Errorf("Got %#v, expected %#v", got, expect)
-			}
-			plain = Stripper(got)
-			if plain != sent {
-				t.Errorf("Got %#v, expected %#v", plain, sent)
-			}
-		}
-	*/
-}
-
-func TestItalics(t *testing.T) {
-	var sent string
-	var expect string
-	var got string
-	var plain string
-	for sent, expect = range map[string]string{"One": "\x1dOne\x1d",
-		"Two":   "\x1dTwo\x1d",
-		"Three": "\x1dThree\x1d"} {
-		got = Italics(sent)
-		if got != expect {
-			t.Errorf("Got %#v, expected %#v", got, expect)
-		}
-		plain = Stripper(got)
-		if plain != sent {
-			t.Errorf("Got %s, expected %s", plain, sent)
-		}
-	}
-}
-
-func TestUnderline(t *testing.T) {
-	var sent string
-	var expect string
-	var got string
-	var plain string
-	for sent, expect = range map[string]string{"One": "\x16One\x16",
-		"Two":   "\x16Two\x16",
-		"Three": "\x16Three\x16"} {
-		got = Underline(sent)
-		if got != expect {
-			t.Errorf("Got %#v, expected %#v", got, expect)
-		}
-		plain = Stripper(got)
-		if plain != sent {
-			t.Errorf("Got %#v, expected %#v", plain, sent)
-		}
-	}
 }
 
-func TestStrike(t *testing.T) {
-	var sent string
-	var expect string
-	var got string
-	var plain string
-	for sent, expect = range map[string]string{"One": "\x1eOne\x1e",
-		"Two":   "\x1eTwo\x1e",
-		"Three": "\x1eThree\x1e"} {
-		got = Strike(sent)
-		if got != expect {
-			t.Errorf("Got %#v, expected %#v", got, expect)
-		}
-		plain = Stripper(got)
-		if plain != sent {
-			t.Errorf("Got %#v, expected %#v", plain, sent)
-		}
-	}
-}
+/*
+func TestColor(t *testing.T) {
+	var tests map[int]string {
+		1: "\x0301%s\x0f",
 
-func TestMono(t *testing.T) {
-	var sent string
-	var expect string
-	var got string
-	var plain string
-	for sent, expect = range map[string]string{"One": "\x11One\x11",
-		"Two":   "\x11Two\x11",
-		"Three": "\x11Three\x11"} {
-		got = Monospace(sent)
-		if got != expect {
-			t.Errorf("Got %#v, expected %#v", got, expect)
-		}
-		plain = Stripper(got)
-		if plain != sent {
-			t.Errorf("Got %#v, expected %#v", plain, sent)
-		}
 	}
 }
+*/

+ 54 - 27
irc-client.go

@@ -1,4 +1,49 @@
 // An IRC-Client library.
+
+/*
+An IRC client library.
+
+This handles connecting in, autojoining channels,
+and SASL authentication.
+
+Example:
+
+	package main
+
+	import (
+		"fmt"
+
+		ircclient "git.red-green.com/RedGreen/irc-client"
+	)
+
+	func main() {
+		config := ircclient.IRCConfig{Port: 6697,
+			Hostname: "irc.libera.chat",
+			UseTLS:   true,
+			Nick:     "go-bot",
+			Username: "go-bot",
+			Realname: "Example IRC bot",
+			AutoJoin: []string{"#go-nuts"},
+		}
+
+		FromIRC := make(chan ircclient.IRCMsg)
+
+		irc := ircclient.IRCClient{IRCConfig: config, ReadChannel: FromIRC}
+		irc.Connect()
+		var Msg ircclient.IRCMsg
+
+		for Msg = range FromIRC {
+			fmt.Println(Msg)
+			if Msg.Cmd == "PRIVMSG" {
+				if ircclient.Match(Msg.To, irc.MyNick) {
+					// Private message to bot
+				} else {
+					// Message to channel bot is on.
+				}
+			}
+		}
+	}
+*/
 package ircclient
 
 import (
@@ -23,6 +68,7 @@ import (
 // get client version information from runtime package.
 var VERSION string = GetModuleVersion()
 
+// Get irc-client version information from runtime.
 func GetModuleVersion() string {
 	modules := GetModules()
 	version, has := modules["git.red-green.com/RedGreen/irc-client"]
@@ -104,16 +150,14 @@ Everything before " :" is split into MsgParts[].
 
 Example Messages:
 
-:irc.red-green.com 001 test :Welcome to IRC
-^From              ^Cmd     ^Msg
-
-	^To
-
-^0                 ^1  ^2   MsgParts[]
+   :irc.red-green.com 001 test :Welcome to IRC
+   ^From              ^Cmd     ^Msg
+                          ^To
+   ^0                 ^1  ^2   MsgParts[]
 
-PING :1234567890
-^Cmd ^Msg
-^0 MsgParts[]
+   PING :1234567890
+   ^Cmd ^Msg
+   ^0 MsgParts[]
 */
 
 // IRCParse Parse an IRC line into the IRCMsg struct.
@@ -154,24 +198,6 @@ func IRCParse(line string) IRCMsg {
 	return results
 }
 
-/*
-func oldIRCParse(line string) []string {
-	var pos int = strings.Index(line, " :")
-	var message string
-
-	if pos != -1 {
-		message = line[pos+2:]
-		line = line[:pos]
-	}
-	var results []string
-	results = strings.Split(line, " ")
-	if message != "" {
-		results = append(results, message)
-	}
-	return results
-}
-*/
-
 // Sent to writer
 //
 //	The To part allows the writer to throttle messages to a specific target.
@@ -483,6 +509,7 @@ func (Config *IRCClient) Action(to string, message string) {
 // Goroutine reader routine
 //
 //	This reads a line at a time, delimited by '\n'.
+//
 //	Auto-replies to server PING.
 //	Converts CTCP & ACTION messages to type CTCP/ACTION.
 //	Automatically answers /VERSION and /TIME.