Skip to content

Commit

Permalink
fastfetch: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
CarterLi committed Nov 17, 2023
1 parent 69a23c7 commit f8ecc73
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 0 deletions.
127 changes: 127 additions & 0 deletions completers/fastfetch_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package cmd

import (
"os/exec"
"regexp"
"strings"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/style"
"github.com/spf13/cobra"
)

var rootCmd = &cobra.Command{
Use: "fastfetch",
Short: "A neofetch-like tool for fetching system information and displaying them in a pretty way",
Long: "https://github.com/fastfetch-cli/fastfetch",
Run: func(cmd *cobra.Command, args []string) {},
}

func Execute() error {
return rootCmd.Execute()
}
func init() {
carapace.Gen(rootCmd).Standalone()
var lines []string;
if out, err := exec.Command("fastfetch", "--help").Output(); err == nil {
lines = map_(
filter(
strings.Split(string(out), "\n"),
func(s string) bool { return strings.HasPrefix(s, " -") },
),
func(s string) string { return s[4:] },
)
}

re, _ := regexp.Compile(`<\??\w+>$`)
for _, line := range lines {
kv := strings.SplitN(line, ":", 2)
if len(kv) != 2 {
continue
}
left := strings.Trim(kv[0], " ")
right := strings.Trim(kv[1], " ")

if left == "" || strings.HasPrefix(left, "--<") { // ignore --<module>-*
continue
}
type_ := ""

if typeLoc := re.FindStringIndex(left); typeLoc != nil {
type_ = strings.TrimLeft(left[typeLoc[0] + 1:typeLoc[1] - 1], "?")
left = strings.TrimRight(left[:typeLoc[0]], " ")
}

args := map_(strings.Split(left, ","), func(s string) string { return strings.Trim(s, "- ") })

switch type_ {
case "bool":
if len(args) == 1 { // long only
rootCmd.Flags().Bool(args[0], false, right)
} else { // short, long
rootCmd.Flags().BoolP(args[1], args[0], false, right)
}

case "num":
if len(args) == 1 { // long only
rootCmd.Flags().Int(args[0], 0, right)
} else { // short, long
rootCmd.Flags().IntP(args[1], args[0], 0, right)
}

default:
if len(args) == 1 { // long only
rootCmd.Flags().String(args[0], "", right)
} else { // short, long
rootCmd.Flags().StringP(args[1], args[0], "", right)
}
}
}

actionColors := carapace.ActionValues(
"black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "default",
)
carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
"help": carapace.ActionExecCommand("fastfetch", "--list-modules", "autocompletion")(func(output []byte) carapace.Action {
modules := strings.Split(strings.TrimRight(string(output), "\n"), "\n")
return carapace.ActionValues(append([]string{"color", "format", "config"}, modules...)...).StyleF(style.ForKeyword)
}),
"config": carapace.ActionExecCommand("fastfetch", "--list-presets", "autocompletion")(func(output []byte) carapace.Action {
presets := strings.Split(strings.TrimRight(string(output), "\n"), "\n")
return carapace.ActionValues(presets...)
}),
"logo": carapace.ActionExecCommand("fastfetch", "--list-logos", "autocompletion")(func(output []byte) carapace.Action {
logos := strings.Split(strings.TrimRight(string(output), "\n"), "\n")
return carapace.ActionValues(append([]string{"none", "small"}, logos...)...).StyleF(style.ForKeyword)
}),
"structure": carapace.ActionExecCommand("fastfetch", "--list-modules", "autocompletion")(func(output []byte) carapace.Action {
modules := strings.Split(strings.TrimRight(string(output), "\n"), "\n")
return carapace.ActionValues(modules...).StyleF(style.ForKeyword).List(":")
}),
"color-keys": actionColors,
"color-title": actionColors,
"color": actionColors,
"title-color-user": actionColors,
"title-color-at": actionColors,
"title-color-host": actionColors,
})

carapace.Gen(rootCmd).PositionalAnyCompletion(
carapace.ActionDirectories(),
)}

func filter[T any](ss []T, test func(T) bool) (ret []T) {
for _, s := range ss {
if test(s) {
ret = append(ret, s)
}
}
return
}

func map_[T any, U any](ss []T, f func(T) U) (ret []U) {
for _, s := range ss {
ret = append(ret, f(s))
}
return
}
7 changes: 7 additions & 0 deletions completers/fastfetch_completer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/rsteube/carapace-bin/completers/fastfetch_completer/cmd"

func main() {
cmd.Execute()
}

0 comments on commit f8ecc73

Please sign in to comment.