Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clear terminal output #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 10 additions & 64 deletions ls/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ package ls

import (
"fmt"
"github.com/pee2pee/lse/ls/color"
"io"
"io/fs"
"os"
"os/user"
"path/filepath"
"strconv"
"strings"
"syscall"

"github.com/profclems/glab/pkg/tableprinter"

"github.com/pee2pee/lse/ls/color"
)

const dotCharacter = 46
Expand All @@ -22,15 +16,15 @@ var dotFiles = []string{".", ".."}

type Flags struct {
A bool // ls -a
C bool // ls -c
D bool // ls -d
G bool // ls --group
L bool // ls -l
R bool // ls -R
}

type LS struct {
Dir string

Dir string
Stderr io.Writer
StdOut io.Writer
Color *color.Palette
Expand All @@ -44,6 +38,10 @@ type Dir struct {
}

func (l *LS) ListDir() error {
if l.C {
return l.clearScreen()
}

if l.D {
return l.showDirStructure()
}
Expand Down Expand Up @@ -163,59 +161,7 @@ func (l *LS) showDirStructure() error {
return nil
}

func (l *LS) display(dirs []Dir) error {
totalBlkSize := 0
c := color.Color()

tb := tableprinter.NewTablePrinter()
tb.Wrap = true
tb.SetTerminalWidth(color.TerminalWidth(l.StdOut))

for i := range dirs {
dir := dirs[i]
name := dir.Info.Name()
if dir.Info.IsDir() {
name = c.Cyan(name)
}

if !l.L {
tb.AddCell(name)
continue
}

stat := dir.Info.Sys().(*syscall.Stat_t)

uid := stat.Uid
gid := stat.Gid

usr, err := user.LookupId(strconv.Itoa(int(uid)))
if err != nil {
return err
}

group, err := user.LookupGroupId(strconv.Itoa(int(gid)))
if err != nil {
return err
}

totalBlkSize += int(stat.Blocks)

timeStr := dir.Info.ModTime().UTC().Format("Jan 02 15:04")

tb.AddRow(dirs[i].Info.Mode(), stat.Nlink, usr.Username, group.Name, dirs[i].Info.Size(), timeStr, name)
}

if !l.L {
tb.EndRow()
}

if l.L {
_, err := fmt.Fprintln(l.StdOut, "total", totalBlkSize)
if err != nil {
return err
}
}

_, err := fmt.Fprint(l.StdOut, tb.String())
return err
func (l *LS) clearScreen() error {
fmt.Fprint(l.StdOut, "\033[H\033[2J")
return nil
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func main() {
},
}
cmd.Flags().BoolVarP(&lsf.A, "all", "a", false, "show all files including hidden files")
cmd.Flags().BoolVarP(&lsf.C, "clear", "c", false, "clear terminal output")
cmd.Flags().BoolVarP(&lsf.D, "directory", "d", false, "show directory structure")
cmd.Flags().BoolVarP(&lsf.G, "group", "g", false, "group directories before files")
cmd.Flags().BoolVarP(&lsf.L, "tabular", "l", false, "show detailed directory structure in tabular form")
Expand Down