├── .gitignore ├── Makefile ├── README.md ├── config.toml ├── go.mod ├── go.sum └── main.go /.gitignore: -------------------------------------------------------------------------------- 1 | # Binaries for programs and plugins 2 | *.exe 3 | *.exe~ 4 | *.dll 5 | *.so 6 | *.dylib 7 | rofi-snippet 8 | 9 | # Test binary, built with `go test -c` 10 | *.test 11 | 12 | # Output of the go coverage tool, specifically when used with LiteIDE 13 | *.out 14 | 15 | # Dependency directories (remove the comment below to include it) 16 | # vendor/ -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | .DEFAULT_GOAL:=help 2 | 3 | .PHONY: help 4 | help: ## Display this help screen 5 | @grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}' 6 | 7 | .PHONY: install 8 | install: ## Install rofi-snippet 9 | @sudo mkdir -p /etc/rofi-snippet/ 10 | @sudo cp ./config.toml /etc/rofi-snippet/ 11 | @go install . 12 | 13 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
3 |
4 | 5 | > `rofi-snippet` is a text snippet tool for Linux 6 | 7 | ## Requirements 8 | 9 | `rofi-snippet` relies on the following command line tools 10 | 11 | - `xdotool` 12 | - `xsel` 13 | - `rofi` 14 | 15 | In addition, `rofi-snippet` requires [`golang`](https://go.dev/) for compilation. 16 | 17 | For example, on `Ubuntu`, you can get what you need with the following commands: 18 | 19 | ```bash 20 | sudo apt install xsel 21 | sudo apt install xdotool 22 | sudo apt install rofi 23 | sudo apt install golang-go 24 | ``` 25 | 26 | ## Installation 27 | 28 | 1. Clone this repo: `git clone https://github.com/tkancf/rofi-snippet` 29 | 2. Change into rofi-snippet: `cd rofi-snippet` 30 | 3. Install: `make install` 31 | 4. A binary is produced in `~/go/bin`. Make sure `GOPATH` is in your environment variables. To check, run `go env`. 32 | 5. Run `rofi-snippet` in the terminal to test. 33 | 34 | ### Remarks 35 | 36 | By default, `config.toml` is installed to `/etc/rofi-snippet/config.toml`. If you want to change this default location, modify `confPath := "/etc" + "/rofi-snippet" + "/config.toml"` in `main.go` and following two lines in `Makefile` to your desired location: 37 | 38 | ```sh 39 | @sudo mkdir -p /etc/rofi-snippet/ 40 | @sudo cp ./config.toml /etc/rofi-snippet/ 41 | ``` 42 | 43 | ## 🚀 Usage 44 | 45 | You probably want to bind `rofi-snippet` to a global shortcut. If you're using `i3`, you can add the following line to `~/.config/i3`: 46 | 47 | bindsym $mod+Shift+d exec --no-startup-id "rofi-snippet" 48 | 49 | Restart `i3`. By pressing `$mod+Shift+d`, it'll bring up the `rofi-snippet`. 50 | 51 | ## Author 52 | 53 | 👤 **tkancf** 54 | 55 | * Github: [@tkancf](https://github.com/tkancf) 56 | 57 | ## Show your support 58 | 59 | Give a ⭐️ if this project helped you! 60 | 61 | --- 62 | 63 | _This README was generated with ❤️ by [readme-md-generator](https://github.com/kefranabg/readme-md-generator)_ 64 | 65 | -------------------------------------------------------------------------------- /config.toml: -------------------------------------------------------------------------------- 1 | [[setting]] 2 | application = "thunderbird" 3 | [[setting.data]] 4 | desc = "hoge" 5 | text = "hごあしこdfj\nfjdskl" 6 | [[setting.data]] 7 | desc = "hge" 8 | text = "fdksljafdksじゃ" 9 | [[setting.data]] 10 | desc = "hoe" 11 | text = "hf;ds" 12 | [[setting.data]] 13 | desc = "hog" 14 | text = "fjdksl;あ" 15 | [[setting]] 16 | application = "firefox" 17 | [[setting.data]] 18 | desc = "hghfde" 19 | text = "fdksljafdksじゃ" 20 | [[setting.data]] 21 | desc = "hoehsod" 22 | text = "hf;ds" 23 | [[setting.data]] 24 | desc = "hogfsdjkal" 25 | text = "fjdksl;あ" 26 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/tkancf/rofi-snippet 2 | 3 | go 1.21 4 | 5 | require ( 6 | github.com/BurntSushi/toml v1.3.2 7 | github.com/atotto/clipboard v0.1.4 8 | github.com/labstack/gommon v0.4.2 9 | ) 10 | 11 | require ( 12 | github.com/mattn/go-colorable v0.1.13 // indirect 13 | github.com/mattn/go-isatty v0.0.20 // indirect 14 | github.com/valyala/bytebufferpool v1.0.0 // indirect 15 | github.com/valyala/fasttemplate v1.2.2 // indirect 16 | golang.org/x/sys v0.16.0 // indirect 17 | ) 18 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8= 2 | github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= 3 | github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= 4 | github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI= 5 | github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= 6 | github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 7 | github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0= 8 | github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU= 9 | github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= 10 | github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= 11 | github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= 12 | github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= 13 | github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 14 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= 15 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 16 | github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= 17 | github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= 18 | github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= 19 | github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= 20 | github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= 21 | github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ= 22 | golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 23 | golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= 24 | golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU= 25 | golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= 26 | gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= 27 | gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= 28 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "io" 5 | "os" 6 | "os/exec" 7 | "strings" 8 | "time" 9 | 10 | "github.com/BurntSushi/toml" 11 | "github.com/atotto/clipboard" 12 | "github.com/labstack/gommon/log" 13 | ) 14 | 15 | type Config struct { 16 | Settings []Setting `toml:"setting"` 17 | } 18 | 19 | type Setting struct { 20 | Application string `toml:"application"` 21 | Data []Data `toml:"data"` 22 | } 23 | 24 | type Data struct { 25 | Desc string `toml:"desc"` 26 | Text string `toml:"text"` 27 | } 28 | 29 | var conf Config 30 | 31 | func main() { 32 | Init() 33 | list := listAllDesc() 34 | reader := strings.NewReader(list) 35 | 36 | preClip, err := clipboard.ReadAll() 37 | if err != nil { 38 | log.Fatal(err) 39 | } 40 | 41 | err, desc := getResult("rofi -dmenu -sep '|'", reader) 42 | if err != nil { 43 | log.Fatal(err) 44 | } 45 | text := descToText(desc) 46 | clipboard.WriteAll(text) 47 | 48 | time.Sleep(30 * time.Millisecond) 49 | exec.Command("sh", "-c", "xdotool key shift+Insert").Run() 50 | time.Sleep(30 * time.Millisecond) 51 | 52 | clipboard.WriteAll(preClip) 53 | } 54 | 55 | func descToText(desc string) string { 56 | desc = strings.TrimRight(desc, "\n") 57 | text := "" 58 | for _, value := range conf.Settings { 59 | for _, v := range value.Data { 60 | if desc == v.Desc { 61 | return v.Text 62 | } 63 | } 64 | } 65 | return text 66 | } 67 | 68 | func getResult(command string, r io.Reader) (error, string) { 69 | var cmd *exec.Cmd 70 | cmd = exec.Command("sh", "-c", command) 71 | cmd.Stderr = os.Stderr 72 | cmd.Stdin = r 73 | out, err := cmd.Output() 74 | result := strings.TrimRight(string(out), "\n") 75 | return err, result 76 | } 77 | 78 | func run(command string, r io.Reader, w io.Writer) error { 79 | var cmd *exec.Cmd 80 | cmd = exec.Command("sh", "-c", command) 81 | cmd.Stderr = os.Stderr 82 | cmd.Stdout = w 83 | cmd.Stdin = r 84 | return cmd.Run() 85 | } 86 | 87 | func listAllDesc() string { 88 | var all []byte 89 | 90 | for _, value := range conf.Settings { 91 | for _, v := range value.Data { 92 | all = append(all, []byte(v.Desc)...) 93 | all = append(all, []byte("|")...) 94 | } 95 | } 96 | 97 | return string(all) 98 | } 99 | 100 | func Init() { 101 | confPath := "/etc" + "/rofi-snippet" + "/config.toml" 102 | if _, err := toml.DecodeFile(confPath, &conf); err != nil { 103 | log.Fatal(err) 104 | } 105 | 106 | } 107 | --------------------------------------------------------------------------------