From d8fe0da383f1a29a10cba2c0928b7ecbbe660722 Mon Sep 17 00:00:00 2001 From: Niklas Date: Sat, 8 Aug 2020 19:14:21 +0200 Subject: [PATCH] put most stuff into package --- main.go | 96 ++++++++----------------------------------- rofi/rofi.go | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 80 deletions(-) create mode 100644 rofi/rofi.go diff --git a/main.go b/main.go index d78282b..d0058d5 100644 --- a/main.go +++ b/main.go @@ -1,46 +1,16 @@ package main import ( - "bufio" "encoding/json" - "fmt" "io/ioutil" "net/http" "net/url" "os" "os/exec" + + "./rofi" ) -type Output struct { - Message string `json:"message,omitempty"` // Sets rofi message, hides it if empty or null - Overlay string `json:"overlay,omitempty"` // Shows overlay with text, hides it if empty or null - Prompt string `json:"prompt,omitempty"` // sets prompt text - Input string `json:"input,omitempty"` // sets input text, to clear use empty string - InputAction string `json:"input action,omitempty"` // "send" or "filter" - EventFormat string `json:"event format,omitempty"` // event format used to send to input - Lines []Line `json:"lines"` // a list of sting or json object to set rofi list content -} - -type Line struct { - Text string `json:"text,required"` - Urgent bool `json:"urgent,omitempty"` - Highlight bool `json:"highlight,omitempty"` - Markup bool `json:"markup,omitempty"` - Icon string `json:"icon,omitempty"` -} - -type Input struct { - Name string `json:"name"` - Value string `json:"value"` -} - -func handleSelect(input Input) { - param := url.Values{} - param.Add("q", input.Value) - exec.Command("xdg-open", "https://duckduckgo.com/?"+param.Encode()).Start() - os.Exit(0) -} - func getSuggestions(query string) []string { param := url.Values{} param.Add("q", query) @@ -66,61 +36,27 @@ func getSuggestions(query string) []string { return rtn } -func convertStringsToLine(strings []string) []Line { - lines := make([]Line, len(strings)) - for i, v := range strings { - lines[i] = Line{v, false, false, false, ""} - } - return lines +func handleSelect(input rofi.Input) { + param := url.Values{} + param.Add("q", input.Value) + exec.Command("xdg-open", "https://duckduckgo.com/?"+param.Encode()).Start() + os.Exit(0) } -func handleInput(input Input) { - switch input.Name { - case "input change": - output := newOutput() - output.Lines = convertStringsToLine(getSuggestions(input.Value)) - sendOutput(output) - case "custom key": - case "active entry": - case "select entry": - handleSelect(input) - case "delete entry": - case "exec custom input": - } -} - -func sendOutput(output Output) { - res, _ := json.Marshal(output) - fmt.Printf("%s\n", res) -} - -func simpleLines(lines []string) []Line { - outputLines := make([]Line, len(lines)) - - for i := 0; i < len(lines); i++ { - outputLines[i] = Line{lines[i], false, false, false, ""} - } - - return outputLines -} - -func newOutput() Output { - return Output{"", "", "", "", "", "", []Line{}} +func handleInputChange(input rofi.Input) { + output := rofi.NewOutput() + output.Lines = rofi.ConvertStringsToLine(getSuggestions(input.Value)) + rofi.SendOutput(output) } func main() { // Setup rofi - initialOutput := newOutput() + initialOutput := rofi.NewOutput() initialOutput.Prompt = "Search" initialOutput.InputAction = "send" - sendOutput(initialOutput) + rofi.SendOutput(initialOutput) - // Input loop - scanner := bufio.NewScanner(os.Stdin) - for scanner.Scan() { - var input Input - bytes := scanner.Bytes() - json.Unmarshal(bytes, &input) - handleInput(input) - } + rofi.InputChangeCallback = handleInputChange + rofi.SelectEntryCallback = handleSelect + rofi.InputLoop() } diff --git a/rofi/rofi.go b/rofi/rofi.go new file mode 100644 index 0000000..31afe58 --- /dev/null +++ b/rofi/rofi.go @@ -0,0 +1,114 @@ +package rofi + +import ( + "bufio" + "encoding/json" + "fmt" + "os" +) + +var ( + // InputChangeCallback called when the user types somthing in + InputChangeCallback func(Input) + + // CustomKeyCallback idk + CustomKeyCallback func(Input) + + // ActivateEntryCallback when the user press enter on an entry + ActivateEntryCallback func(Input) + + // SelectEntryCallback when a user selects a entry via arrow keys + SelectEntryCallback func(Input) + + // DeleteEntryCallback idk + DeleteEntryCallback func(Input) + + // CustomInputCallback idk + CustomInputCallback func(Input) +) + +// Output json to send to rofi +type Output struct { + Message string `json:"message,omitempty"` // Sets rofi message, hides it if empty or null + Overlay string `json:"overlay,omitempty"` // Shows overlay with text, hides it if empty or null + Prompt string `json:"prompt,omitempty"` // sets prompt text + Input string `json:"input,omitempty"` // sets input text, to clear use empty string + InputAction string `json:"input action,omitempty"` // "send" or "filter" + EventFormat string `json:"event format,omitempty"` // event format used to send to input + Lines []Line `json:"lines"` // a list of sting or json object to set rofi list content +} + +// Line a single entry +type Line struct { + Text string `json:"text,required"` + Urgent bool `json:"urgent,omitempty"` + Highlight bool `json:"highlight,omitempty"` + Markup bool `json:"markup,omitempty"` + Icon string `json:"icon,omitempty"` +} + +// Input send when the user types something +type Input struct { + Name string `json:"name"` + Value string `json:"value"` +} + +// ConvertStringsToLine converts a string array to Line array with all flags set to false +func ConvertStringsToLine(strings []string) []Line { + lines := make([]Line, len(strings)) + for i, v := range strings { + lines[i] = Line{v, false, false, false, ""} + } + return lines +} + +// SendOutput sends output to rofi +func SendOutput(output Output) { + res, _ := json.Marshal(output) + fmt.Printf("%s\n", res) +} + +// NewOutput helper for a new empty outout +func NewOutput() Output { + return Output{"", "", "", "", "", "", []Line{}} +} + +// InputLoop blocking loop to handle incomming json +func InputLoop() { + scanner := bufio.NewScanner(os.Stdin) + for scanner.Scan() { + var input Input + bytes := scanner.Bytes() + json.Unmarshal(bytes, &input) + handleInput(input) + } +} + +func handleInput(input Input) { + switch input.Name { + case "input change": + if InputChangeCallback != nil { + InputChangeCallback(input) + } + case "custom key": + if CustomKeyCallback != nil { + CustomKeyCallback(input) + } + case "active entry": + if ActivateEntryCallback != nil { + ActivateEntryCallback(input) + } + case "select entry": + if SelectEntryCallback != nil { + SelectEntryCallback(input) + } + case "delete entry": + if DeleteEntryCallback != nil { + DeleteEntryCallback(input) + } + case "exec custom input": + if CustomInputCallback != nil { + CustomInputCallback(input) + } + } +}