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 5e018c8
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
120 changes: 120 additions & 0 deletions completers/fastfetch_completer/cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
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 {
for _, line := range strings.Split(string(out), "\n") {
if strings.HasPrefix(line, " -") {
lines = append(lines, line[4:])
}
}
} else {
return
}
re, _ := regexp.Compile(`<\??\w+>$`)
for _, line := range lines {
left, right, found := strings.Cut(line, ":")
if !found {
continue
}
left = strings.Trim(left, " ")
right = strings.Trim(right, " ")
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 := map_(strings.Split(strings.TrimRight(string(output), "\n"), "\n"), func(s string) string { x, _, _ := strings.Cut(s, ":"); return x })
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 {
var texts []string
for _, line := range strings.Split(strings.TrimRight(string(output), "\n"), "\n") {
name, desc, _ := strings.Cut(line, ":")
texts = append(texts, name, desc)
}
return carapace.ActionValuesDescribed(texts...).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 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 5e018c8

Please sign in to comment.