mirror of
https://github.com/charmbracelet/glow
synced 2024-12-14 14:12:27 +00:00
Use base name when stashing from the CLI if no memo is set
This commit is contained in:
parent
a5b67ba721
commit
b3909354aa
1 changed files with 14 additions and 2 deletions
16
stash.go
16
stash.go
|
@ -5,6 +5,8 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/charmbracelet/charm"
|
"github.com/charmbracelet/charm"
|
||||||
"github.com/charmbracelet/charm/ui/common"
|
"github.com/charmbracelet/charm/ui/common"
|
||||||
|
@ -18,7 +20,8 @@ var (
|
||||||
forceKey bool
|
forceKey bool
|
||||||
*/
|
*/
|
||||||
|
|
||||||
memo string
|
memo string
|
||||||
|
|
||||||
stashCmd = &cobra.Command{
|
stashCmd = &cobra.Command{
|
||||||
Use: "stash SOURCE",
|
Use: "stash SOURCE",
|
||||||
Hidden: false,
|
Hidden: false,
|
||||||
|
@ -26,20 +29,29 @@ var (
|
||||||
Long: "",
|
Long: "",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
filePath := args[0]
|
||||||
|
|
||||||
|
if memo == "" {
|
||||||
|
memo = strings.Replace(path.Base(filePath), path.Ext(filePath), "", 1)
|
||||||
|
}
|
||||||
|
|
||||||
cc := initCharmClient()
|
cc := initCharmClient()
|
||||||
f, err := os.Open(args[0])
|
f, err := os.Open(filePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("bad filename")
|
return fmt.Errorf("bad filename")
|
||||||
}
|
}
|
||||||
|
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
b, err := ioutil.ReadAll(f)
|
b, err := ioutil.ReadAll(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error reading file")
|
return fmt.Errorf("error reading file")
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = cc.StashMarkdown(memo, string(b))
|
_, err = cc.StashMarkdown(memo, string(b))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error stashing markdown")
|
return fmt.Errorf("error stashing markdown")
|
||||||
}
|
}
|
||||||
|
|
||||||
dot := termenv.String("•").Foreground(common.Green.Color()).String()
|
dot := termenv.String("•").Foreground(common.Green.Color()).String()
|
||||||
fmt.Println(dot + " Stashed!")
|
fmt.Println(dot + " Stashed!")
|
||||||
return nil
|
return nil
|
||||||
|
|
Loading…
Reference in a new issue