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

     1  //go:build !windows && !plan9
     2  // +build !windows,!plan9
     3  
     4  package autocomplete
     5  
     6  import (
     7  	"os"
     8  	"strings"
     9  
    10  	"github.com/lmorg/murex/utils/consts"
    11  	"github.com/phayes/permbits"
    12  )
    13  
    14  func pathIsLocal(s string) bool {
    15  	return strings.HasPrefix(s, consts.PathSlash) ||
    16  		strings.HasPrefix(s, "."+consts.PathSlash) ||
    17  		strings.HasPrefix(s, ".."+consts.PathSlash)
    18  }
    19  
    20  func matchDirsOnce(s string) (items []string) {
    21  	//s = variables.ExpandString(s)
    22  	path, partial := partialPath(s)
    23  
    24  	var dirs []string
    25  
    26  	files, _ := os.ReadDir(path)
    27  	for _, f := range files {
    28  		if f.IsDir() && (f.Name()[0] != '.' ||
    29  			(len(partial) > 0 && partial[0] == '.')) {
    30  			dirs = append(dirs, f.Name()+consts.PathSlash)
    31  			continue
    32  		}
    33  
    34  		fi, _ := f.Info()
    35  		perm := permbits.FileMode(fi.Mode())
    36  		switch {
    37  		case perm.OtherExecute() && fi.Mode()&os.ModeSymlink != 0:
    38  			ln, err := os.Readlink(path + consts.PathSlash + f.Name())
    39  			if err != nil {
    40  				continue
    41  			}
    42  			if ln[0] != consts.PathSlash[0] {
    43  				ln = path + consts.PathSlash + ln
    44  			}
    45  			info, err := os.Lstat(ln)
    46  			if err != nil {
    47  				continue
    48  			}
    49  			perm := permbits.FileMode(info.Mode())
    50  			if perm.OtherExecute() && info.Mode().IsDir() {
    51  				dirs = append(dirs, f.Name()+consts.PathSlash)
    52  			}
    53  
    54  		default:
    55  			/*|| perm.GroupExecute()||perm.UserExecute() need to check what user and groups you are in first */
    56  		}
    57  	}
    58  
    59  	if path != consts.PathSlash {
    60  		dirs = append(dirs, ".."+consts.PathSlash)
    61  	}
    62  
    63  	for i := range dirs {
    64  		if strings.HasPrefix(dirs[i], partial) {
    65  			items = append(items, dirs[i][len(partial):])
    66  		}
    67  	}
    68  
    69  	return
    70  }