github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/shell/autocomplete/execs.go (about)

     1  package autocomplete
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/lmorg/murex/lang"
    10  	"github.com/lmorg/murex/lang/types"
    11  )
    12  
    13  var wslMounts []string
    14  
    15  // Read returns an interface{} of the user dictionary.
    16  // This is only intended to be used by `config.Properties.GoFunc.Read()`
    17  func WslMountsGet() (interface{}, error) {
    18  	return wslMounts, nil
    19  }
    20  
    21  // Write takes a JSON-encoded string and writes it to the dictionary slice.
    22  // This is only intended to be used by `config.Properties.GoFunc.Write()`
    23  func WslMountsSet(v interface{}) error {
    24  	switch v := v.(type) {
    25  	case string:
    26  		return json.Unmarshal([]byte(v), &wslMounts)
    27  
    28  	default:
    29  		return fmt.Errorf("invalid data-type. Expecting a %s encoded string", types.Json)
    30  	}
    31  }
    32  
    33  func listExesWindows(path string, exes map[string]bool) {
    34  	var showExts bool
    35  
    36  	v, err := lang.ShellProcess.Config.Get("shell", "extensions-enabled", types.Boolean)
    37  	if err != nil {
    38  		showExts = false
    39  	} else {
    40  		showExts = v.(bool)
    41  	}
    42  
    43  	files, _ := os.ReadDir(path)
    44  	for _, f := range files {
    45  		if f.IsDir() {
    46  			continue
    47  		}
    48  
    49  		name := strings.ToLower(f.Name())
    50  
    51  		if len(name) < 5 {
    52  			continue
    53  		}
    54  
    55  		ext := name[len(name)-4:]
    56  
    57  		if ext == ".exe" || ext == ".com" || ext == ".bat" || ext == ".cmd" || ext == ".scr" {
    58  			if showExts {
    59  				exes[name] = true
    60  			} else {
    61  				exes[name[:len(name)-4]] = true
    62  			}
    63  		}
    64  	}
    65  }