package main import ( "fmt" "os" "os/exec" "strings" "github.com/spf13/cobra" ) func main() { var rootCmd = &cobra.Command{ Use: "tmux_popup", Short: "Ein CLI-Tool für verschiedene Aktionen", Long: "tmux_popup ist ein CLI-Werkzeug, das verschiedene Aktionen wie tmux, lazygit, harlequin und postings unterstützt. Es oeffnet diese Programme in einem floating tmux pane", } rootCmd.PersistentFlags().String("width", "80%", "Breite des Popups (z. B. 80%)") rootCmd.PersistentFlags().String("height", "80%", "Höhe des Popups (z. B. 80%)") rootCmd.AddCommand(tmuxCmd) rootCmd.AddCommand(lazygitCmd) rootCmd.AddCommand(harlequinCmd) rootCmd.AddCommand(postingsCmd) rootCmd.AddCommand(checkCmd) if err := rootCmd.Execute(); err != nil { fmt.Println("Fehler:", err) os.Exit(1) } } var tmuxCmd = &cobra.Command{ Use: "tmux", Short: "Öffnet ein tmux-Popup", Run: func(cmd *cobra.Command, args []string) { width, _ := cmd.Flags().GetString("width") height, _ := cmd.Flags().GetString("height") openPopup("popup", "", width, height) }, } var lazygitCmd = &cobra.Command{ Use: "lazygit", Short: "Öffnet lazygit in einem Popup", Run: func(cmd *cobra.Command, args []string) { width, _ := cmd.Flags().GetString("width") height, _ := cmd.Flags().GetString("height") openPopup("lazygit", "lazygit", width, height) }, } var harlequinCmd = &cobra.Command{ Use: "harlequin", Short: "Öffnet harlequin in einem Popup", Run: func(cmd *cobra.Command, args []string) { width, _ := cmd.Flags().GetString("width") height, _ := cmd.Flags().GetString("height") openPopup("harlequin", "harlequin", width, height) }, } var postingsCmd = &cobra.Command{ Use: "postings", Short: "Öffnet postings in einem Popup", Run: func(cmd *cobra.Command, args []string) { width, _ := cmd.Flags().GetString("width") height, _ := cmd.Flags().GetString("height") openPopup("postings", "posting", width, height) }, } var checkCmd = &cobra.Command{ Use: "check", Short: "Prüft die Abhängigkeiten", Run: func(cmd *cobra.Command, args []string) { if err := CheckDependencies(); err != nil { fmt.Println("Fehler bei der Überprüfung der Abhängigkeiten:", err) os.Exit(1) } fmt.Println("Alle Abhängigkeiten sind vorhanden.") }, } func openPopup(sessionName, command string, width, height string) { currentSession, err := exec.Command("tmux", "display-message", "-p", "-F", "#{session_name}").Output() if err != nil { fmt.Println("Fehler beim Abrufen der aktuellen Session:", err) return } if strings.TrimSpace(string(currentSession)) == sessionName { exec.Command("tmux", "detach-client").Run() } else { args := []string{"popup", "-d", "#{pane_current_path}", "-xC", "-yC", fmt.Sprintf("-w%s", width), fmt.Sprintf("-h%s", height), "-E"} if command == "" { args = append(args, fmt.Sprintf("tmux attach -t %s || tmux new -s %s", sessionName, sessionName)) } else { args = append(args, fmt.Sprintf("tmux attach -t %s || tmux new -s %s '%s'", sessionName, sessionName, command)) } exec.Command("tmux", args...).Run() } } func CheckDependencies() error { deps := []string{"lazygit", "harlequin", "posting"} args := []string{"--version"} for _, dep := range deps { if dep == "posting" { if err := runCmd(dep, []string{"--help"}); err != nil { return err } } else { if err := runCmd(dep, args); err != nil { return err } } } return nil } func runCmd(dep string, args []string) error { cmd := exec.Command(dep, args...) if err := cmd.Run(); err != nil { return fmt.Errorf("Fehler beim Überprüfen von %s: %w", dep, err) } return nil }