<commit_before>package dispatcher

import "strings"

import "github.com/cnf/go-claw/listeners"
import "github.com/cnf/go-claw/targets"
import "github.com/cnf/go-claw/clog"

type Dispatcher struct {
    Configfile string
    config Config
    listenermap map[string]*listeners.Listener
    targetmap map[string]targets.Target
    modemap map[string]*Mode
    activemode string
    cs *listeners.CommandStream
}

func (self *Dispatcher) Start() {
    defer self.cs.Close()
    self.activemode = "default"
    self.activemode = "plex"
    self.readConfig()
    self.setupListeners()
    self.setupModes()
    self.setupTargets()

    var out listeners.RemoteCommand

    for self.cs.Next(&out) {
        if self.cs.HasError() {
            clog.Warn("An error occured somewhere: %v", self.cs.GetError())
            self.cs.ClearError()
        }
        // clog.Debug("repeat: %2d - key: %s - source: %s", out.Repeat, out.Key, out.Source)
        self.dispatch(&out)
    }
}

func (self *Dispatcher) setupListeners() {
    self.listenermap = make(map[string]*listeners.Listener)
    self.cs = listeners.NewCommandStream()

    for k, v := range self.config.Listeners {
        l, ok := listeners.GetListener(v.Module, v.Params)
        if ok {
            clog.Debug("Setting up listener `%s`", k)
            self.listenermap[k] = &l
            self.cs.AddListener(l)
        }
    }

}

func (self *Dispatcher) setupModes() {
    self.modemap = make(map[string]*Mode)
    for k, v := range self.config.Modes {
        self.modemap[k] = &Mode{Keys: make(map[string][]string)}
        for kk, kv := range v {
            self.modemap[k].Keys[kk] = make([]string, len(kv))
            i := 0
            for _, av := range kv {
                self.modemap[k].Keys[kk][i] = av
                i++
            }
        }
    }
}

func (self *Dispatcher) setupTargets() {
    self.targetmap = make(map[string]targets.Target)
    for k, v := range self.config.Targets {
        t, ok := targets.GetTarget(v.Module, k, v.Params)
        if ok {
            self.targetmap[k] = t
            println(k)
        }
    }
}

func (self *Dispatcher) dispatch(rc *listeners.RemoteCommand) bool {
    clog.Debug("repeat: %2d - key: %s - source: %s", rc.Repeat, rc.Key, rc.Source)
    var mod string
    var cmd string<commit_msg>added key timeout<commit_after>package dispatcher

import "strings"
import "time"

import "github.com/cnf/go-claw/listeners"
import "github.com/cnf/go-claw/targets"
import "github.com/cnf/go-claw/clog"

type Dispatcher struct {
    Configfile string
    config Config
    keytimeout time.Duration
    listenermap map[string]*listeners.Listener
    targetmap map[string]targets.Target
    modemap map[string]*Mode
    activemode string
    cs *listeners.CommandStream
}

func (self *Dispatcher) Start() {
    defer self.cs.Close()
    self.activemode = "default"
    self.activemode = "plex"
    self.keytimeout = time.Duration(500 * time.Millisecond) 
    self.readConfig()
    self.setupListeners()
    self.setupModes()
    self.setupTargets()

    var out listeners.RemoteCommand

    for self.cs.Next(&out) {
        if self.cs.HasError() {
            clog.Warn("An error occured somewhere: %v", self.cs.GetError())
            self.cs.ClearError()
        }
        // clog.Debug("repeat: %2d - key: %s - source: %s", out.Repeat, out.Key, out.Source)
        self.dispatch(&out)
    }
}

func (self *Dispatcher) setupListeners() {
    self.listenermap = make(map[string]*listeners.Listener)
    self.cs = listeners.NewCommandStream()

    for k, v := range self.config.Listeners {
        l, ok := listeners.GetListener(v.Module, v.Params)
        if ok {
            clog.Debug("Setting up listener `%s`", k)
            self.listenermap[k] = &l
            self.cs.AddListener(l)
        }
    }

}

func (self *Dispatcher) setupModes() {
    self.modemap = make(map[string]*Mode)
    for k, v := range self.config.Modes {
        self.modemap[k] = &Mode{Keys: make(map[string][]string)}
        for kk, kv := range v {
            self.modemap[k].Keys[kk] = make([]string, len(kv))
            i := 0
            for _, av := range kv {
                self.modemap[k].Keys[kk][i] = av
                i++
            }
        }
    }
}

func (self *Dispatcher) setupTargets() {
    self.targetmap = make(map[string]targets.Target)
    for k, v := range self.config.Targets {
        t, ok := targets.GetTarget(v.Module, k, v.Params)
        if ok {
            self.targetmap[k] = t
            println(k)
        }
    }
}

func (self *Dispatcher) dispatch(rc *listeners.RemoteCommand) bool {
    clog.Debug("repeat: %2d - key: %s - source: %s", rc.Repeat, rc.Key, rc.Source)
    tdiff := time.Since(rc.Time)
    if tdiff > self.keytimeout {
        clog.Info("Key timeout reached: %# v", tdiff.String())
        return false
    }
    var mod string
    var cmd string
{'fraction_non_alphanumeric': 0.13719885667619436, 'fraction_numerical': 0.0018374846876276032, 'mean_word_length': 2.325865580448065, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '2192795', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1627, 'n_words': 350}