hyprland/module: add init command to create new modules

parent 89361b7b
......@@ -341,6 +341,81 @@ func HyprlandModuleListCommand(ctx context.Context, cmd *cli.Command) error {
return nil
}
func HyprlandModuleInitCommand(ctx context.Context, cmd *cli.Command) error {
name := cmd.Args().Get(0)
if name == "" {
return errors.New(locale.T("module name not specified"))
}
var group string
if g, short, ok := strings.Cut(name, "/"); ok {
group = g
name = g + "-" + short
}
manager, err := GetHyprlandManager(ctx)
if err != nil {
return err
}
info := manager.GetModuleInfo(name)
if !info.Available {
return fmt.Errorf(locale.T("name '%s' is reserved"), name)
}
if !info.Status.IsEqual(config.ModuleStatus.Unknown) {
return fmt.Errorf(locale.T("module '%s' already exists"), name)
}
modulePath := filepath.Join(manager.GetModuleDir(true), name+".conf")
template := fmt.Sprintf("#---\n# summary: \n# description: \n# group: %s\n# order: 0\n#---\n\n", group)
if err := os.WriteFile(modulePath, []byte(template), 0644); err != nil {
return fmt.Errorf(locale.T("failed to create module: %s"), err)
}
manager.UserModules = append(manager.UserModules, name)
fmt.Println(locale.T("Module created:"), modulePath)
editor := getEditor()
editCmd := exec.Command(editor, modulePath)
editCmd.Stdin = os.Stdin
editCmd.Stdout = os.Stdout
editCmd.Stderr = os.Stderr
if err := editCmd.Run(); err != nil {
return fmt.Errorf(locale.T("failed to start editor: %s"), err)
}
if !cmd.Bool("enable") {
return nil
}
errs, err := manager.CheckModule(name)
if err != nil {
return err
}
if len(errs) > 0 {
fmt.Println(locale.T("Module has errors, not enabling:"))
for _, e := range errs {
fmt.Printf(" %d: %s\n", e.Line, e.Text)
}
return nil
}
msg, err := manager.SetModule("enable", name, false)
if err != nil {
return err
}
color.Green(msg)
return nil
}
func HyprlandModuleEditCommand(ctx context.Context, cmd *cli.Command) error {
module := cmd.Args().Get(0)
manager, err := GetHyprlandManager(ctx)
......
......@@ -79,6 +79,19 @@ func CommandList() *cli.Command {
Usage: locale.T("Hyprland modules"),
Commands: []*cli.Command{
{
Name: "init",
Usage: locale.T("Create a new module"),
ArgsUsage: "name",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "enable",
Usage: locale.T("Check and enable module after editing"),
Aliases: []string{"e"},
},
},
Action: HyprlandModuleInitCommand,
},
{
Name: "check",
Usage: locale.T("Check Hyprland module"),
ArgsUsage: "module",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment