Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
add autostart values, add omitempty to json struct
Browse files Browse the repository at this point in the history
  • Loading branch information
abs3ntdev committed Jan 5, 2023
1 parent c21f72e commit 90eb853
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
3 changes: 3 additions & 0 deletions flags/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Flags struct {
Raw bool
Version bool
Variables bool
AutoStart bool

FilterBinds string
Output string
Expand All @@ -23,6 +24,7 @@ func ReadFlags() *Flags {

optPath := getopt.StringLong("config-file", 'f', "", "path to config file, default is $HOME/.config/hypr/hyprland.conf")

optAutoStart := getopt.BoolLong("auto-start", 'a', "Show autostarting programs")
optVariables := getopt.BoolLong("variables", 'v', "Show variables")
optFilterBinds := getopt.StringLong("filter-binds", 'b', "", "get binding where command or dispatcher contains given string use * for all")

Expand All @@ -42,6 +44,7 @@ func ReadFlags() *Flags {
Version: *optVersion,
Variables: *optVariables,
FilterBinds: *optFilterBinds,
AutoStart: *optAutoStart,
ConfigPath: *optPath,
Output: *optOutput,
}
Expand Down
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,12 @@ func markdownHandler(configValues *reader.ConfigValues, flags *flags.Flags) erro
func jsonHandler(configValues *reader.ConfigValues, flags *flags.Flags) error {
var out []byte
var err error
if !flags.Variables {
out, err = json.MarshalIndent(configValues.Binds, "", " ")
if err != nil {
return err
}
} else {
out, err = json.MarshalIndent(configValues, "", " ")
if err != nil {
return err
}

out, err = json.MarshalIndent(configValues, "", " ")
if err != nil {
return err
}

fmt.Println(string(out))
if flags.Output != "" {
err := os.WriteFile(flags.Output, out, 0o644)
Expand Down Expand Up @@ -131,6 +126,11 @@ func rawHandler(configValues *reader.ConfigValues, flags *flags.Flags) error {
out += "}\n"
}
}
if flags.AutoStart {
for _, val := range configValues.AutoStart {
out += fmt.Sprintf("%s=%s\n", val.ExecType, val.Command)
}
}
for _, bind := range configValues.Binds {
out += fmt.Sprintf("%s = %s %s %s", bind.BindType, bind.Bind, bind.Dispatcher, bind.Command)
if bind.Comments != "" {
Expand Down
30 changes: 25 additions & 5 deletions reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ type Keybind struct {
Comments string
}

type Exec struct {
ExecType string
Command string
}

type ConfigValues struct {
Settings Settings
Binds []*Keybind
Settings Settings `json:",omitempty"`
AutoStart []*Exec `json:",omitempty"`
Binds []*Keybind `json:",omitempty"`
}

// Read Hyprland configuration file and return lines that start with bind= and bindm=
Expand Down Expand Up @@ -62,7 +68,7 @@ func ReadHyprlandConfig(flags *flags.Flags) (*ConfigValues, error) {
scanner := bufio.NewScanner(file)

var binds []*Keybind
// var variables []string
var autostart []*Exec

for scanner.Scan() {
line := scanner.Text()
Expand All @@ -75,15 +81,20 @@ func ReadHyprlandConfig(flags *flags.Flags) (*ConfigValues, error) {
switch {
case strings.HasPrefix(line, "bind"):
binds = append(binds, makeBind(line))
case strings.HasPrefix(line, "exec"):
if flags.AutoStart {
autostart = append(autostart, makeExec(line))
}
}
}

if err := scanner.Err(); err != nil {
panic(err)
}
configValues := &ConfigValues{
Settings: settings,
Binds: binds,
Settings: settings,
Binds: binds,
AutoStart: autostart,
}
return configValues, nil
}
Expand All @@ -106,6 +117,15 @@ func getSubCategory(check string, categories []string) (string, bool) {
return "", false
}

func makeExec(line string) *Exec {
split := strings.SplitN(line, "=", 2)
exec := &Exec{
ExecType: strings.TrimSpace(split[0]),
Command: strings.TrimSpace(split[1]),
}
return exec
}

func makeBind(bind string) *Keybind {
split := strings.SplitN(bind, "=", 2)
keyBind := &Keybind{
Expand Down

0 comments on commit 90eb853

Please sign in to comment.