<commit_before>package main

import (
	"bufio"
	"fmt"
	"github.com/kballard/goirc/irc"
	"io"
	"os"
	"strings"
)

type Stdin struct {
	c chan irc.SafeConn
}

func NewStdin(conn irc.SafeConn) *Stdin {
	c := make(chan irc.SafeConn, 1)
	c <- conn
	return &Stdin{c: c}
}

func (s *Stdin) WithConn(f func(conn irc.SafeConn)) {
	conn := <-s.c
	defer func() { s.c <- conn }()
	f(conn)
}

func (s *Stdin) Run(quit chan<- struct{}) {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		text := scanner.Text()
		if strings.TrimSpace(text) != "" {
			s.WithConn(func(conn irc.SafeConn) {
				handleInputLine(conn, text)
			})
		}
	}
	if err := scanner.Err(); err != nil {
		if err == io.EOF {
			quit <- struct{}{}
		} else {
			fmt.Fprintln(os.Stderr, "reading standard input:", err)
		}
	}
}

func (s *Stdin) ReplaceConn(conn irc.SafeConn) {
	<-s.c
	s.c <- conn
}

func handleInputLine(conn irc.SafeConn, text string) {
	words := strings.SplitN(text, " ", 2)
	cmd := words[0]
	if !strings.HasPrefix(cmd, "/") {
		return
	}
	cmd = cmd[1:]

	if f, ok := inputCommands[cmd]; ok {
		f(conn, words[1])
	}
}

var inputCommands = map[string]func(irc.SafeConn, string){
	"raw": func(conn irc.SafeConn, text string) {
		fmt.Println(text)
		conn.Raw(text)
	},
	"msg": func(conn irc.SafeConn, text string) {
		words := strings.SplitN(text, " ", 2)
		if len(words) != 2 || words[0] == "" || words[1] == "" {
			fmt.Fprintln(os.Stderr, "usage: /msg target text")
			return
		}<commit_msg>stdin: Don't crash if a /foo command is entered alone<commit_after>package main

import (
	"bufio"
	"fmt"
	"github.com/kballard/goirc/irc"
	"io"
	"os"
	"strings"
)

type Stdin struct {
	c chan irc.SafeConn
}

func NewStdin(conn irc.SafeConn) *Stdin {
	c := make(chan irc.SafeConn, 1)
	c <- conn
	return &Stdin{c: c}
}

func (s *Stdin) WithConn(f func(conn irc.SafeConn)) {
	conn := <-s.c
	defer func() { s.c <- conn }()
	f(conn)
}

func (s *Stdin) Run(quit chan<- struct{}) {
	scanner := bufio.NewScanner(os.Stdin)
	for scanner.Scan() {
		text := scanner.Text()
		if strings.TrimSpace(text) != "" {
			s.WithConn(func(conn irc.SafeConn) {
				handleInputLine(conn, text)
			})
		}
	}
	if err := scanner.Err(); err != nil {
		if err == io.EOF {
			quit <- struct{}{}
		} else {
			fmt.Fprintln(os.Stderr, "reading standard input:", err)
		}
	}
}

func (s *Stdin) ReplaceConn(conn irc.SafeConn) {
	<-s.c
	s.c <- conn
}

func handleInputLine(conn irc.SafeConn, text string) {
	words := strings.SplitN(text, " ", 2)
	cmd := words[0]
	if !strings.HasPrefix(cmd, "/") {
		return
	}
	cmd = cmd[1:]

	if f, ok := inputCommands[cmd]; ok {
		f(conn, append(words, "")[1])
	}
}

var inputCommands = map[string]func(irc.SafeConn, string){
	"raw": func(conn irc.SafeConn, text string) {
		fmt.Println(text)
		conn.Raw(text)
	},
	"msg": func(conn irc.SafeConn, text string) {
		words := strings.SplitN(text, " ", 2)
		if len(words) != 2 || words[0] == "" || words[1] == "" {
			fmt.Fprintln(os.Stderr, "usage: /msg target text")
			return
		}
{'fraction_non_alphanumeric': 0.18841544607190414, 'fraction_numerical': 0.005992010652463382, 'mean_word_length': 3.4321533923303833, 'pattern_counts': {'":': 4, '<': 17, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2153807', 'n_tokens_mistral': 1353, 'n_tokens_neox': 1244, 'n_words': 280}