github.com/krzko/gosh@v0.0.0-20241128233202-5abd4ef06633/internal/shell/builtins/ls.go (about)

     1  // internal/shell/builtins/ls.go
     2  package builtins
     3  
     4  import (
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/krzko/gosh/internal/utils/formatter"
    10  	"golang.org/x/term"
    11  )
    12  
    13  type LsCommand struct {
    14  	formatter *formatter.TableFormatter
    15  }
    16  
    17  type LsOptions struct {
    18  	Long  bool
    19  	All   bool
    20  	Human bool
    21  }
    22  
    23  func NewLsCommand() *LsCommand {
    24  	return &LsCommand{
    25  		formatter: formatter.New(),
    26  	}
    27  }
    28  
    29  func filterHiddenFiles(entries []formatter.FileInfo) []formatter.FileInfo {
    30  	var filtered []formatter.FileInfo
    31  	for _, entry := range entries {
    32  		if !strings.HasPrefix(entry.Name, ".") {
    33  			filtered = append(filtered, entry)
    34  		}
    35  	}
    36  	return filtered
    37  }
    38  
    39  func (l *LsCommand) parseOptions(args []string) ([]string, LsOptions, error) {
    40  	var opts LsOptions
    41  	var paths []string
    42  
    43  	for _, arg := range args {
    44  		if strings.HasPrefix(arg, "-") {
    45  			for _, c := range arg[1:] {
    46  				switch c {
    47  				case 'l':
    48  					opts.Long = true
    49  				case 'a':
    50  					opts.All = true
    51  				case 'h':
    52  					opts.Human = true
    53  				default:
    54  					return nil, opts, fmt.Errorf("unknown option: %c", c)
    55  				}
    56  			}
    57  		} else {
    58  			paths = append(paths, arg)
    59  		}
    60  	}
    61  
    62  	return paths, opts, nil
    63  }
    64  
    65  func (l *LsCommand) readDirectory(path string) ([]formatter.FileInfo, error) {
    66  	entries, err := os.ReadDir(path)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  
    71  	var fileInfos []formatter.FileInfo
    72  	for _, entry := range entries {
    73  		info, err := entry.Info()
    74  		if err != nil {
    75  			continue
    76  		}
    77  
    78  		stat := info.Sys()
    79  		owner, group := getOwnerGroup(stat)
    80  
    81  		fileInfo := formatter.FileInfo{
    82  			Name:        entry.Name(),
    83  			Size:        info.Size(),
    84  			Mode:        info.Mode(),
    85  			ModTime:     info.ModTime(),
    86  			IsDir:       entry.IsDir(),
    87  			Owner:       owner,
    88  			Group:       group,
    89  			Permissions: info.Mode().String(),
    90  		}
    91  		fileInfos = append(fileInfos, fileInfo)
    92  	}
    93  
    94  	return fileInfos, nil
    95  }
    96  
    97  func (l *LsCommand) Execute(args []string) error {
    98  	paths, opts, err := l.parseOptions(args)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	if len(paths) == 0 {
   104  		paths = []string{"."}
   105  	}
   106  
   107  	for i, path := range paths {
   108  		if i > 0 {
   109  			fmt.Printf("\n%s:\n", path)
   110  		}
   111  
   112  		entries, err := l.readDirectory(path)
   113  		if err != nil {
   114  			return err
   115  		}
   116  
   117  		if !opts.All {
   118  			entries = filterHiddenFiles(entries)
   119  		}
   120  
   121  		if opts.Long {
   122  			err = l.formatter.FormatLongList(entries)
   123  		} else {
   124  			width, _, err := term.GetSize(int(os.Stdout.Fd()))
   125  			if err != nil {
   126  				width = 80 // fallback width
   127  			}
   128  			err = l.formatter.FormatCompact(entries, width)
   129  		}
   130  
   131  		if err != nil {
   132  			return err
   133  		}
   134  	}
   135  
   136  	return nil
   137  }
   138  
   139  func (l *LsCommand) Help() string {
   140  	return `ls - list directory contents
   141  
   142  Usage: ls [OPTIONS] [PATH]
   143  
   144  Options:
   145      -l    use long listing format
   146      -a    show hidden files
   147      -h    human-readable sizes`
   148  }