Skip to content

Commit

Permalink
feat: 垂直方向揃えできるように #7 (#12)
Browse files Browse the repository at this point in the history
* 上揃え追加

* 下揃え追加

* 中央揃え追加

* vertical

* update

* vert top

* vertical bottom

* makefile削除

* ver up
  • Loading branch information
jiro4989 committed Nov 12, 2020
1 parent bbb78b6 commit b2f4e24
Show file tree
Hide file tree
Showing 7 changed files with 464 additions and 54 deletions.
53 changes: 0 additions & 53 deletions Makefile

This file was deleted.

62 changes: 62 additions & 0 deletions align.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,3 +167,65 @@ func AlignRight(lines []string, length int, pad string) []string {
}
return ret
}

// AlignVerticalTop は垂直上方向に位置揃えする
func AlignVerticalTop(lines []string, height int) []string {
if height <= len(lines) {
return lines
}

var ret []string
for _, line := range lines {
ret = append(ret, line)
}
for i := 0; i < height-len(lines); i++ {
ret = append(ret, "")
}

return ret
}

// AlignVerticalCenter は垂直中央方向に位置揃えする
func AlignVerticalCenter(lines []string, height int) []string {
if height <= len(lines) {
return lines
}

diffLen := height - len(lines)
halfDiff := diffLen / 2

var ret []string
for i := 0; i < halfDiff; i++ {
ret = append(ret, "")
}
for _, line := range lines {
ret = append(ret, line)
}
for i := 0; i < halfDiff; i++ {
ret = append(ret, "")
}

// 追加の必要な行数が奇数の場合は、下側に余った1行を追加する
if diffLen%2 == 1 {
ret = append(ret, "")
}

return ret
}

// AlignVerticalBottom は垂直下方向に位置揃えする
func AlignVerticalBottom(lines []string, height int) []string {
if height <= len(lines) {
return lines
}

var ret []string
for i := 0; i < height-len(lines); i++ {
ret = append(ret, "")
}
for _, line := range lines {
ret = append(ret, line)
}

return ret
}
182 changes: 182 additions & 0 deletions align_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,185 @@ func TestAlignRight(t *testing.T) {

assert.Equal(t, []string{"123456", " a"}, AlignRight([]string{"123456", "a"}, 4, " "))
}

func TestAlignVerticalTop(t *testing.T) {
tests := []struct {
desc string
lines []string
height int
want []string
}{
{
desc: "正常系: 1行上に揃える",
lines: []string{
"hello",
"world",
},
height: 3,
want: []string{
"hello",
"world",
"",
},
},
{
desc: "正常系: 変更なし",
lines: []string{
"hello",
"world",
},
height: 2,
want: []string{
"hello",
"world",
},
},
{
desc: "正常系: heightのほうがlinesより小さい場合は変更なし",
lines: []string{
"hello",
"world",
},
height: 1,
want: []string{
"hello",
"world",
},
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
assert := assert.New(t)
got := AlignVerticalTop(tt.lines, tt.height)
assert.Equal(tt.want, got)
})
}
}

func TestAlignVerticalCenter(t *testing.T) {
tests := []struct {
desc string
lines []string
height int
want []string
}{
{
desc: "正常系: 1行中央に揃える",
lines: []string{
"hello",
"world",
},
height: 3,
want: []string{
"hello",
"world",
"",
},
},
{
desc: "正常系: 両方に追加される",
lines: []string{
"hello",
"world",
},
height: 4,
want: []string{
"",
"hello",
"world",
"",
},
},
{
desc: "正常系: 加算した結果余分になった行は、下側に追加される",
lines: []string{
"hello",
"world",
},
height: 5,
want: []string{
"",
"hello",
"world",
"",
"",
},
},
{
desc: "正常系: heightのほうがlinesより小さい場合は変更なし",
lines: []string{
"hello",
"world",
},
height: 1,
want: []string{
"hello",
"world",
},
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
assert := assert.New(t)
got := AlignVerticalCenter(tt.lines, tt.height)
assert.Equal(tt.want, got)
})
}
}

func TestAlignVerticalBottom(t *testing.T) {
tests := []struct {
desc string
lines []string
height int
want []string
}{
{
desc: "正常系: 1行下に揃える",
lines: []string{
"hello",
"world",
},
height: 3,
want: []string{
"",
"hello",
"world",
},
},
{
desc: "正常系: 変更なし",
lines: []string{
"hello",
"world",
},
height: 2,
want: []string{
"hello",
"world",
},
},
{
desc: "正常系: heightのほうがlinesより小さい場合は変更なし",
lines: []string{
"hello",
"world",
},
height: 1,
want: []string{
"hello",
"world",
},
},
}

for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
assert := assert.New(t)
got := AlignVerticalBottom(tt.lines, tt.height)
assert.Equal(tt.want, got)
})
}
}
73 changes: 73 additions & 0 deletions subcmd_vertical_bottom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package main

import (
"fmt"
"io/ioutil"
"os"
"strings"

"github.com/spf13/cobra"
)

func init() {
RootCommand.AddCommand(verticalBottomCommand)
verticalBottomCommand.Flags().IntP("length", "n", -1, "Padding length")
verticalBottomCommand.Flags().BoolP("write", "w", false, "Overwrite file")
verticalBottomCommand.Flags().StringP("linefeed", "l", "\n", "Line feed")
}

var verticalBottomCommand = &cobra.Command{
Use: "vertical-bottom",
Aliases: []string{"vb"},
Short: "Align vertical bottom command from file or stdin",
Run: func(cmd *cobra.Command, args []string) {
f := cmd.Flags()

n, err := f.GetInt("length")
if err != nil {
panic(err)
}

writeFile, err := f.GetBool("write")
if err != nil {
panic(err)
}

lf, err := f.GetString("linefeed")
if err != nil {
panic(err)
}

// 引数なしの場合は標準入力を処理
if len(args) < 1 {
args = readStdin()
padded := AlignVerticalBottom(args, n)
for _, v := range padded {
fmt.Println(v)
}
return
}

for _, fn := range args {
b, err := ioutil.ReadFile(fn)
if err != nil {
panic(err)
}
s := string(b)
lines := strings.Split(s, lf)
padded := AlignVerticalBottom(lines, n)

// ファイル上書き指定があれば上書き
if writeFile {
b := []byte(strings.Join(padded, lf))
if err := ioutil.WriteFile(fn, b, os.ModePerm); err != nil {
panic(err)
}
continue
}
for _, v := range padded {
fmt.Println(v)
}
}
},
}
Loading

0 comments on commit b2f4e24

Please sign in to comment.