dotfiles

regexghost dotfiles and scripts
git clone https://git.regexghost.com/dotfiles.git
Log | Files | Refs | README

soundboard.go (3112B)


      1 package main
      2 
      3 import (
      4 	"fmt"
      5 	"encoding/csv"
      6 	"github.com/gdamore/tcell/v2"
      7 	"os"
      8 	"os/exec"
      9 )
     10 
     11 var styles = map[string]tcell.Style{}
     12 var termHeight int
     13 var termWidth int
     14 
     15 var homeDir string
     16 
     17 type sound struct {
     18 	name string
     19 	path string
     20 	key rune
     21 }
     22 
     23 var sounds []sound
     24 
     25 var cmds []*exec.Cmd
     26 
     27 const CONFIG_FILE=".config/regexghost/soundboard.csv"
     28 const SOUND_LOC=".local/share/regexghost/sounds"
     29 
     30 // Function to allow adding strings to screen instead of just individual runes (chars)
     31 func drawText(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string) {
     32 	row := y1
     33 	col := x1
     34 	for _, r := range []rune(text) {
     35 		s.SetContent(col, row, r, nil, style)
     36 		col++
     37 		if col >= x2 {
     38 			row++
     39 			col = x1
     40 		}
     41 		if row > y2 {
     42 			break
     43 		}
     44 	}
     45 }
     46 
     47 func errorOut(e string) {
     48 	fmt.Println(e)
     49 	os.Exit(1)
     50 }
     51 
     52 func loadSounds() {
     53 	file, err := os.Open(homeDir + "/" + CONFIG_FILE)
     54 
     55 	if err != nil {
     56 		errorOut("Error, config file not present")
     57 	}
     58 
     59 	defer file.Close()
     60 
     61 	reader := csv.NewReader(file)
     62 	records, err := reader.ReadAll()
     63 
     64 	if err != nil {
     65 		errorOut("Error parsing csv file")
     66 	}
     67 
     68 	for _, record := range records {
     69 		key := rune(record[0][0])
     70 		newSound := sound {
     71 			name: record[1],
     72 			path: record[2],
     73 			key: key,
     74 		}
     75 		sounds = append(sounds, newSound)
     76 		fmt.Println(newSound)
     77 	}
     78 }
     79 
     80 func drawSoundboard(s tcell.Screen) {
     81 	s.Clear()
     82 	drawText(s, 0, 0, 50, 10, styles["pink"], "Soundboard")
     83 	for i, soundEntry := range sounds {
     84 		line := string(soundEntry.key) + ": " + soundEntry.name
     85 		drawText(s, 0, i+2, 50, 100, styles["blue"], line)
     86 	}
     87 }
     88 
     89 func playSound(path string) {
     90 //	cmd := exec.Command("mpv", "--no-config", "--no-resume-playback", "--force-window=no", homeDir + "/" + SOUND_LOC + "/" + path)
     91 	cmd := exec.Command("aplay", homeDir + "/" + SOUND_LOC + "/" + path)
     92 	cmds = append(cmds, cmd)
     93 	cmd.Start()
     94 }
     95 
     96 func killSounds() {
     97 	for _, cmd := range cmds {
     98 		cmd.Process.Kill()
     99 	}
    100 }
    101 
    102 func findAndPlaySound(key rune) {
    103 	for _, soundEntry := range sounds {
    104 		if soundEntry.key == key {
    105 			playSound(soundEntry.path)
    106 		}
    107 	}
    108 }
    109 
    110 func main() {
    111 	homeDir, _ = os.UserHomeDir()
    112 
    113 	loadSounds()
    114 
    115 	s, err := tcell.NewScreen()
    116 	if err != nil {
    117 		panic(err)
    118 	}
    119 	err = s.Init()
    120 	if err != nil {
    121 		panic(err)
    122 	}
    123 
    124 	// Create colours
    125 	blueColour := tcell.NewHexColor(0x38ffea)
    126 	pinkColour := tcell.NewHexColor(0xff76c1)
    127 
    128 	// Use the colours to make styles and add to "styles" map
    129 	styles["white"] = tcell.StyleDefault.Background(tcell.ColorReset).Foreground(tcell.ColorReset)
    130 	styles["blue"] = tcell.StyleDefault.Background(tcell.ColorReset).Foreground(blueColour)
    131 	styles["pink"] = tcell.StyleDefault.Background(tcell.ColorReset).Foreground(pinkColour)
    132 
    133 	s.SetStyle(styles["white"])
    134 	s.Clear()
    135 
    136 	quit := func() {
    137 		s.Fini()
    138 		os.Exit(0)
    139 	}
    140 
    141 	for {
    142 		s.Show()
    143 
    144 		drawSoundboard(s)
    145 		ev := s.PollEvent()
    146 
    147 		switch ev := ev.(type) {
    148 			case *tcell.EventResize:
    149 				s.Sync()
    150 			case *tcell.EventKey:
    151 				if ev.Key() == tcell.KeyRune {
    152 					if ev.Rune() == 'q' {
    153 						killSounds()
    154 						quit()
    155 					} else if ev.Rune() == ' ' {
    156 						killSounds()
    157 				} else {
    158 					findAndPlaySound(ev.Rune())
    159 				}
    160 			}
    161 		}
    162 	}
    163 }