mirror of
https://github.com/charmbracelet/glow
synced 2025-03-04 23:07:12 +00:00
Truncate long lines in stash get
command + cosmetic stuff
This commit is contained in:
parent
97d11c0065
commit
01de366703
1 changed files with 25 additions and 4 deletions
29
stash.go
29
stash.go
|
@ -6,9 +6,12 @@ import (
|
|||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/charmbracelet/charm"
|
||||
"github.com/mattn/go-runewidth"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/crypto/ssh/terminal"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -51,13 +54,31 @@ var (
|
|||
cc := initCharmClient()
|
||||
mds, err := cc.GetStash(1)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error getting stash")
|
||||
return fmt.Errorf("error getting stash: " + err.Error())
|
||||
}
|
||||
fmt.Println("ID\tNote")
|
||||
fmt.Println("--\t----")
|
||||
|
||||
const gap = " "
|
||||
const gapWidth = len(gap)
|
||||
numDigits := len(fmt.Sprintf("%d", len(mds)))
|
||||
termWidth, _, err := terminal.GetSize(int(os.Stdout.Fd()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
noteColWidth := termWidth - numDigits - gapWidth
|
||||
|
||||
// Header
|
||||
fmt.Println("ID" + gap + "Note")
|
||||
fmt.Println(strings.Repeat("─", numDigits) + gap + strings.Repeat("─", noteColWidth))
|
||||
|
||||
// Body
|
||||
for _, md := range mds {
|
||||
fmt.Printf("%d\t%s\n", md.ID, md.Note)
|
||||
fmt.Printf("%"+fmt.Sprintf("%d", numDigits)+".d%s%s\n",
|
||||
md.ID,
|
||||
gap,
|
||||
runewidth.Truncate(md.Note, noteColWidth, "…"),
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue