<commit_before>package vunikbd

import (
	"fmt"
	"time"

	"github.com/bendahl/uinput"
)

type Keyboard struct {
	name  string
	delay time.Duration
	vk    uinput.Keyboard
}

func NewKeyboard(name string, delay time.Duration) (*Keyboard, error) {
	var err error

	k := Keyboard{
		name:  name,
		delay: delay,
	}

	k.vk, err = uinput.CreateKeyboard("/dev/uinput", k.name)

	return &k, err
}

func (k *Keyboard) Type(s string) {
	for _, r := range s {
		h := fmt.Sprintf("%x\n", r)
		k.vk.SendKeyPress(uinput.KEY_LEFTCTRL)
		k.sleep()
		k.vk.SendKeyPress(uinput.KEY_LEFTSHIFT)
		k.sleep()
		k.vk.SendKeyPress(uinput.KEY_U)
		k.sleep()
		k.vk.SendKeyRelease(uinput.KEY_U)
		k.sleep()
		k.vk.SendKeyRelease(uinput.KEY_LEFTCTRL)
		k.sleep()
		k.vk.SendKeyRelease(uinput.KEY_LEFTSHIFT)
		k.sleep()
		for _, v := range h {
			k.vk.SendKeyPress(keycodes[v])
			k.sleep()
			k.vk.SendKeyRelease(keycodes[v])
			k.sleep()
		}
		k.vk.SendKeyPress(uinput.KEY_ENTER)
		k.sleep()
		k.vk.SendKeyRelease(uinput.KEY_ENTER)
		k.sleep()
	}
}

func (k *Keyboard) sleep() {
	time.Sleep(k.delay)
}

var keycodes map[rune]int

func init() {
	keycodes = make(map[rune]int)
	keycodes['0'] = uinput.KEY_KP0
	keycodes['1'] = uinput.KEY_KP1
	keycodes['2'] = uinput.KEY_KP2
	keycodes['3'] = uinput.KEY_KP3
	keycodes['4'] = uinput.KEY_KP4
	keycodes['5'] = uinput.KEY_KP5
	keycodes['6'] = uinput.KEY_KP6
	keycodes['7'] = uinput.KEY_KP7
	keycodes['8'] = uinput.KEY_KP8
	keycodes['9'] = uinput.KEY_KP9
	keycodes['a'] = uinput.KEY_A<commit_msg>Minor optimizations<commit_after>package vunikbd

import (
	"fmt"
	"time"

	"github.com/bendahl/uinput"
)

type Keyboard struct {
	name  string
	delay time.Duration
	vk    uinput.Keyboard
}

func NewKeyboard(name string, delay time.Duration) (*Keyboard, error) {
	var err error

	k := Keyboard{
		name:  name,
		delay: delay,
	}

	k.vk, err = uinput.CreateKeyboard("/dev/uinput", k.name)

	return &k, err
}

func (k *Keyboard) Type(s string) {
	for _, r := range s {
		h := fmt.Sprintf("%x\n", r)
		k.vk.SendKeyPress(uinput.KEY_LEFTCTRL)
		k.sleep()
		k.vk.SendKeyPress(uinput.KEY_LEFTSHIFT)
		k.sleep()
		k.vk.SendKeyPress(uinput.KEY_U)
		k.sleep()
		k.vk.SendKeyRelease(uinput.KEY_U)
		k.sleep()
		for _, v := range h {
			k.vk.SendKeyPress(keycodes[v])
			k.sleep()
			k.vk.SendKeyRelease(keycodes[v])
			k.sleep()
		}
		k.vk.SendKeyRelease(uinput.KEY_LEFTCTRL)
		k.sleep()
		k.vk.SendKeyRelease(uinput.KEY_LEFTSHIFT)
		k.sleep()
	}
}

func (k *Keyboard) sleep() {
	time.Sleep(k.delay)
}

var keycodes map[rune]int

func init() {
	keycodes = make(map[rune]int)
	keycodes['0'] = uinput.KEY_KP0
	keycodes['1'] = uinput.KEY_KP1
	keycodes['2'] = uinput.KEY_KP2
	keycodes['3'] = uinput.KEY_KP3
	keycodes['4'] = uinput.KEY_KP4
	keycodes['5'] = uinput.KEY_KP5
	keycodes['6'] = uinput.KEY_KP6
	keycodes['7'] = uinput.KEY_KP7
	keycodes['8'] = uinput.KEY_KP8
	keycodes['9'] = uinput.KEY_KP9
	keycodes['a'] = uinput.KEY_A
{'fraction_non_alphanumeric': 0.16290212183436004, 'fraction_numerical': 0.013689253935660506, 'mean_word_length': 3.9880546075085324, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3453173', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1275, 'n_words': 212}