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

feat: add melon logo style #1

Merged
merged 1 commit into from
Jul 14, 2024
Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
capybara
output
bin
input
67 changes: 67 additions & 0 deletions cmd/border/logo_melon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package border

import (
"github.com/sincerefly/capybara/base/log"
"github.com/sincerefly/capybara/cmd/border_common"
"github.com/sincerefly/capybara/cmd/cmdutils"
"github.com/sincerefly/capybara/service/border"
"github.com/sincerefly/capybara/service/border/styles"
"github.com/sincerefly/capybara/utils/colorizer"
"github.com/spf13/cobra"
)

var LogoMelonCmd = &cobra.Command{
Use: "melon",
Short: "Style: logo left, no padding",
Run: func(cmd *cobra.Command, args []string) {

parameter := &styles.LogoMelonParameter{}

input := cmdutils.GetParam(cmd.Flags(), "input")
parameter.SetInput(input)

output := cmdutils.GetParam(cmd.Flags(), "output")
parameter.SetOutput(output)

// width param
width := cmdutils.GetIntParam(cmd.Flags(), "width")
if fixedWidth, fixed := border_common.FixedBorderWidth(width); fixed {
log.Warn("border width fixed with %d", fixedWidth)
width = fixedWidth
}
parameter.SetBorderWidth(width)

// color param
colorStr := cmdutils.GetParam(cmd.Flags(), "color")
col, err := colorizer.ToColor(colorStr)
if err != nil {
log.Fatal(err)
}
parameter.SetBorderColor(col)

// bottom container height
containerHeight, set := cmdutils.GetIntParamB(cmd.Flags(), "container-height")
parameter.SetBottomContainerHeight(containerHeight)
parameter.SetIsContainerHeightSet(set)

// bottom container height
containerHeightRatio := cmdutils.GetFloat64Param(cmd.Flags(), "container-height-ratio")
parameter.SetBottomContainerHeightRatio(containerHeightRatio)

// run
log.Debugf("parameter: %s", parameter.JSONString())
border.NewStyleProcessor(border.StyleLogoMelon, parameter).Run()
},
}

func init() {

flags := LogoMelonCmd.Flags()
flags.StringP("input", "i", "input", "specify input folder")
flags.StringP("output", "o", "output", "specify output folder")
flags.IntP("width", "w", 100, "specify border width")
flags.StringP("color", "c", "white", "specify border color")
flags.IntP("container-height", "", 300, "bottom logo container height")
flags.Float64P("container-height-ratio", "", 0.12, "bottom logo container height, image based height")
flags.BoolP("without-subtitle", "", false, "without subtitle")
}
1 change: 1 addition & 0 deletions cmd/cmds_style.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ func init() {

borderCmd.AddCommand(border.SimpleCmd)
borderCmd.AddCommand(border.TextBottomCmd)
borderCmd.AddCommand(border.LogoMelonCmd)
}

var borderCmd = &cobra.Command{
Expand Down
23 changes: 23 additions & 0 deletions cmd/cmdutils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,26 @@ func GetBoolParam(flags *pflag.FlagSet, key string) bool {
val, _ := GetBoolParamB(flags, key)
return val
}

// GetFloat64ParamB returns a parameter as float64 and a boolean to tell if it is different from the default
func GetFloat64ParamB(flags *pflag.FlagSet, key string) (float64, bool) {
value, _ := flags.GetFloat64(key)

// If set on Flags, use it.
if flags.Changed(key) {
return value, true
}

// If set through viper (env, config), return it.
if v.IsSet(key) {
return v.GetFloat64(key), true
}

// Otherwise use default value on flags.
return value, false
}

func GetFloat64Param(flags *pflag.FlagSet, key string) float64 {
val, _ := GetFloat64ParamB(flags, key)
return val
}
73 changes: 72 additions & 1 deletion resources/embed.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package resources

import "embed"
import (
"embed"
"errors"
"github.com/sincerefly/capybara/utils/exif"
"github.com/sincerefly/capybara/utils/fsutil"
"os"
"path/filepath"
"strings"
)

//go:embed font/* font/*
var F embed.FS
Expand All @@ -13,3 +21,66 @@ const (
RobotMediumTTF = "font/Robot/Robot-Medium.ttf"
RobotRegularTTF = "font/Robot/Robot-Regular.ttf"
)

//go:embed logo/*
var LOGO embed.FS

const (
NikonLogo = "logo/nikon.png"
SonyLogo = "logo/sony.png"
FujifilmLogo = "logo/fujifilm.png"
AppleLogo = "logo/apple.png"
EmptyLogo = "logo/empty.png"
)

// CreateTemporaryLogoFile TODO: refactor
func CreateTemporaryLogoFile(makeStr string) (string, error) {

if makeStr == "" { // check
return "", errors.New("param 'makeStr' is empty")
}

logo := MakeToLogo(makeStr)
filename := filepath.Join(fsutil.GetTempDir(""), logo)

has, err := fsutil.Exists(filename) // exist
if err != nil {
return "", err
}
if has {
return filename, nil
}

b, err := LOGO.ReadFile(logo) // read from embed
if err != nil {
return "", err
}

exist, err := fsutil.Exists(filepath.Dir(filename))
if err != nil {
return "", err
}
if !exist {
fsutil.MkdirAll(filepath.Dir(filename))
}

if err = fsutil.WriteFile(filename, b, os.FileMode(0644)); err != nil { // to temp file
return "", err
}
return filename, nil
}

func MakeToLogo(makeStr string) string {

upMakeStr := strings.ToUpper(makeStr)
if strings.Contains(upMakeStr, string(exif.SonyCorporation)) {
return SonyLogo
} else if strings.Contains(upMakeStr, string(exif.NikonCorporation)) {
return NikonLogo
} else if strings.Contains(upMakeStr, string(exif.FujifilmCorporation)) {
return FujifilmLogo
} else if strings.Contains(upMakeStr, string(exif.AppleCorporation)) {
return AppleLogo
}
return EmptyLogo
}
13 changes: 13 additions & 0 deletions resources/embed_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package resources

import (
"github.com/sincerefly/capybara/base/log"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetNikonLogoTmpFsPath(t *testing.T) {
path, err := CreateTemporaryLogoFile("NIKON")
assert.Nil(t, err)
log.Info(path)
}
Binary file added resources/logo/apple.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/logo/empty.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/logo/fujifilm.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/logo/nikon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added resources/logo/sony.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions service/border/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Style string
const (
StyleSimple Style = "simple"
StyleTextBottom Style = "text_bottom"
StyleLogoMelon Style = "logo_melon"
)

type Parameterizable interface {
Expand Down Expand Up @@ -58,6 +59,9 @@ func (s *StyleProcessor) Run() {
case StyleTextBottom:
params := s.params.(*styles.TextBottomParameter)
err = styles.NewTextBottomProcessor(params, fiStore).Run()
case StyleLogoMelon:
params := s.params.(*styles.LogoMelonParameter)
err = styles.NewLogoMelonProcessor(params, fiStore).Run()
}
if err != nil {
log.Fatal(err)
Expand Down
Loading
Loading