internal/rofi.go

55 lines · 1242 bytes

 1package internal
 2
 3import (
 4	"bytes"
 5	"fmt"
 6	"os/exec"
 7	"strings"
 8)
 9
10func GetTokenFromRofi() (string, error) {
11	// The URL to open
12	url := "https://anilist.co/api/v2/oauth/authorize?client_id=20686&response_type=token"
13
14	// Use rofi to display a prompt with the URL
15	message := "Press enter to open the anilist token page in your browser"
16	_, err := GetUserInputFromRofi(message)
17	if err != nil {
18		return "", err
19	}
20
21	// Open the URL in the default browser
22	err = exec.Command("xdg-open", url).Start()
23	if err != nil {
24		return "", err
25	}
26
27	// Use rofi again to get the token input from the user
28	token, err := GetUserInputFromRofi("Enter the token:")
29	if err != nil {
30		return "", err
31	}
32
33	return token, nil
34}
35
36// GetUserInputFromRofi prompts the user for input using Rofi with a custom message
37func GetUserInputFromRofi(message string) (string, error) {
38	// Create the Rofi command
39	cmd := exec.Command("rofi", "-dmenu", "-p", "Input", "-mesg", message)
40	
41	// Set up pipes for output
42	var out bytes.Buffer
43	cmd.Stdout = &out
44	
45	// Run the command
46	err := cmd.Run()
47	if err != nil {
48		return "", fmt.Errorf("failed to run Rofi: %w", err)
49	}
50	
51	// Get the entered input
52	userInput := strings.TrimSpace(out.String())
53	
54	return userInput, nil
55}