Fix stash view when no items are stashed

This commit is contained in:
Christian Rocha 2020-06-02 18:58:15 -04:00 committed by Christian Muehlhaeuser
parent e50aff65c2
commit 2688a8c4a6
3 changed files with 28 additions and 10 deletions

4
go.mod
View file

@ -3,7 +3,7 @@ module github.com/charmbracelet/glow
go 1.13 go 1.13
require ( require (
github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f80 github.com/charmbracelet/bubbles v0.0.0-20200602224800-12b2758d50f2
github.com/charmbracelet/bubbletea v0.7.1-0.20200528144715-b10f6d679158 github.com/charmbracelet/bubbletea v0.7.1-0.20200528144715-b10f6d679158
github.com/charmbracelet/charm v0.5.1 github.com/charmbracelet/charm v0.5.1
github.com/charmbracelet/glamour v0.2.0 github.com/charmbracelet/glamour v0.2.0
@ -13,5 +13,5 @@ require (
github.com/spf13/cobra v0.0.7 github.com/spf13/cobra v0.0.7
golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37 golang.org/x/crypto v0.0.0-20200510223506-06a226fb4e37
golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2 // indirect
golang.org/x/sys v0.0.0-20200523222454-059865788121 golang.org/x/sys v0.0.0-20200602100848-8d3cce7afc34
) )

4
go.sum
View file

@ -17,6 +17,8 @@ github.com/calmh/randomart v1.1.0/go.mod h1:DQUbPVyP+7PAs21w/AnfMKG5NioxS3TbZ2F9
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f80 h1:cfaoL1+tHPABTLEAg831PIFG96teW69Wamz9M025r5M= github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f80 h1:cfaoL1+tHPABTLEAg831PIFG96teW69Wamz9M025r5M=
github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f80/go.mod h1:/AeLRFlL2Uf4X7U5LjnswTII6u4maPzMm1+vZfeUJKc= github.com/charmbracelet/bubbles v0.0.0-20200526000837-87c7cd778f80/go.mod h1:/AeLRFlL2Uf4X7U5LjnswTII6u4maPzMm1+vZfeUJKc=
github.com/charmbracelet/bubbles v0.0.0-20200602224800-12b2758d50f2 h1:JDEhuhIiBwpnd/3f0o/4pjYobUTnTU+sxvDMCDC/D78=
github.com/charmbracelet/bubbles v0.0.0-20200602224800-12b2758d50f2/go.mod h1:/AeLRFlL2Uf4X7U5LjnswTII6u4maPzMm1+vZfeUJKc=
github.com/charmbracelet/bubbletea v0.6.4-0.20200525234836-3b8b011b5a26/go.mod h1:BTzHOUvUlKecQz7ZB8NgPRWi2Z8NRCV04qwyFOfO1Kk= github.com/charmbracelet/bubbletea v0.6.4-0.20200525234836-3b8b011b5a26/go.mod h1:BTzHOUvUlKecQz7ZB8NgPRWi2Z8NRCV04qwyFOfO1Kk=
github.com/charmbracelet/bubbletea v0.7.0 h1:nf0FEQCCc1e0qRGgpnz08QFsy25eDitzVwxV+Z/p4UI= github.com/charmbracelet/bubbletea v0.7.0 h1:nf0FEQCCc1e0qRGgpnz08QFsy25eDitzVwxV+Z/p4UI=
github.com/charmbracelet/bubbletea v0.7.0/go.mod h1:BTzHOUvUlKecQz7ZB8NgPRWi2Z8NRCV04qwyFOfO1Kk= github.com/charmbracelet/bubbletea v0.7.0/go.mod h1:BTzHOUvUlKecQz7ZB8NgPRWi2Z8NRCV04qwyFOfO1Kk=
@ -180,6 +182,8 @@ golang.org/x/sys v0.0.0-20200413165638-669c56c373c4/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200519105757-fe76b779f299/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zrpzXdb/voyeOuVKS46o= golang.org/x/sys v0.0.0-20200523222454-059865788121 h1:rITEj+UZHYC927n8GT97eC3zrpzXdb/voyeOuVKS46o=
golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200602100848-8d3cce7afc34 h1:u6CI7A++8r4SItZHYe2cWeAEndN4p1p+3Oum/Ft2EzM=
golang.org/x/sys v0.0.0-20200602100848-8d3cce7afc34/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20180221164845-07fd8470d635/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

View file

@ -19,7 +19,7 @@ import (
const ( const (
stashViewItemHeight = 3 stashViewItemHeight = 3
stashViewTopPadding = 5 stashViewTopPadding = 7
stashViewBottomPadding = 4 stashViewBottomPadding = 4
stashViewHorizontalPadding = 6 stashViewHorizontalPadding = 6
setNotePromptText = "Memo: " setNotePromptText = "Memo: "
@ -132,6 +132,9 @@ func (m stashModel) markdownIndex() int {
// return the current selected markdown in the stash // return the current selected markdown in the stash
func (m stashModel) selectedMarkdown() *markdown { func (m stashModel) selectedMarkdown() *markdown {
if len(m.markdowns) == 0 {
return nil
}
return m.markdowns[m.markdownIndex()] return m.markdowns[m.markdownIndex()]
} }
@ -395,20 +398,30 @@ func stashView(m stashModel) string {
case stashStateSettingNote: case stashStateSettingNote:
fallthrough fallthrough
case stashStatePromptDelete: case stashStatePromptDelete:
if len(m.markdowns) == 0 {
s += stashEmtpyView(m) var stashView string
break var stashViewHeight int
if len(m.markdowns) > 0 {
stashView = stashPopulatedView(m)
stashViewHeight = strings.Count(stashView, "\n") + 1
} }
// We need to fill any empty height with newlines so the footer reaches // We need to fill any empty height with newlines so the footer reaches
// the bottom. // the bottom.
numBlankLines := max(0, (m.terminalHeight-stashViewTopPadding-stashViewBottomPadding)%stashViewItemHeight) //numBlankLines := max(0, (m.terminalHeight-stashViewTopPadding-stashViewBottomPadding)%stashViewItemHeight)
numBlankLines := max(0, (m.terminalHeight-stashViewTopPadding-stashViewBottomPadding)-stashViewHeight)
blankLines := "" blankLines := ""
if numBlankLines > 0 { if numBlankLines > 0 {
blankLines = strings.Repeat("\n", numBlankLines) blankLines = strings.Repeat("\n", numBlankLines)
} }
header := "Heres your markdown stash:" var header string
if len(m.markdowns) > 0 {
header = "Heres your markdown stash:"
} else {
header = "Nothing stashed yet. To stash you can " + common.Code("glow stash path/to/file.md") + "."
}
switch m.state { switch m.state {
case stashStatePromptDelete: case stashStatePromptDelete:
header = te.String("Delete this item? ").Foreground(common.Red.Color()).String() + header = te.String("Delete this item? ").Foreground(common.Red.Color()).String() +
@ -430,7 +443,7 @@ func stashView(m stashModel) string {
" %s\n\n %s\n\n%s\n\n%s %s\n\n %s", " %s\n\n %s\n\n%s\n\n%s %s\n\n %s",
glowLogoView(" Glow "), glowLogoView(" Glow "),
header, header,
stashPopulatedView(m), stashView,
blankLines, blankLines,
pagination, pagination,
stashHelpView(m), stashHelpView(m),
@ -481,7 +494,8 @@ func stashPopulatedView(m stashModel) string {
func stashHelpView(m stashModel) string { func stashHelpView(m stashModel) string {
var ( var (
h []string h []string
isNews bool = m.selectedMarkdown().markdownType == newsMarkdown md = m.selectedMarkdown()
isNews = md != nil && md.markdownType == newsMarkdown
) )
if m.state == stashStateSettingNote { if m.state == stashStateSettingNote {