-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 上揃え追加 * 下揃え追加 * 中央揃え追加 * vertical * update * vert top * vertical bottom * makefile削除 * ver up
- Loading branch information
Showing
7 changed files
with
464 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
}, | ||
} |
Oops, something went wrong.