Skip to content

Commit 493f131

Browse files
committed
Merge pull request #16 from pocke/15-convert-line-ending
[WIP] Convert line ending
2 parents 8c4cb34 + ac1d6c0 commit 493f131

File tree

12 files changed

+197
-128
lines changed

12 files changed

+197
-128
lines changed

Makefile

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
VERSION=$(shell git describe --tags)
22

33
build:
4-
go build -ldflags "-X main.version=$(VERSION)"
4+
go build -ldflags "-X github.com/pocke/lemonade/lemon.Version=$(VERSION)"
55

66
install:
7-
go install -ldflags "-X main.version=$(VERSION)"
7+
go install -ldflags "-X github.com/pocke/lemonade/lemon.Version=$(VERSION)"
88

99
release:
10-
gox --arch 'amd64 386' --os 'windows linux darwin' --output "dist/{{.Dir}}_{{.OS}}_{{.Arch}}/{{.Dir}}" -ldflags "-X main.version=$(VERSION)"
10+
gox --arch 'amd64 386' --os 'windows linux darwin' --output "dist/{{.Dir}}_{{.OS}}_{{.Arch}}/{{.Dir}}" -ldflags "-X github.com/pocke/lemonade/lemon.Version=$(VERSION)"
1111
zip pkg/lemonade_windows_386.zip dist/lemonade_windows_386/lemonade.exe -j
1212
zip pkg/lemonade_windows_amd64.zip dist/lemonade_windows_amd64/lemonade.exe -j
1313
tar zcvf pkg/lemonade_linux_386.tar.gz -C dist/lemonade_linux_386/ lemonade

README.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ Sub Commands:
5050

5151
Options:
5252
--port=2489 TCP port number
53-
--allow="0.0.0.0/0,::0" Allow IP Range [Server only]
53+
--line-ending Convert Line Ending(CR/CRLF)
54+
--allow="0.0.0.0/0,::/0" Allow IP Range [Server only]
5455
--host="localhost" Destination hostname [Client only]
5556
--trans-loopback=true Translate loopback address [open subcommand only]
5657
--trans-localfile=true Translate local file path [open subcommand only]
@@ -91,6 +92,7 @@ There is configuration file at `~/.config/lemonade.toml`.
9192
```toml
9293
port = 1234
9394
allow = '192.168.0.0/24'
95+
line-ending = 'crlf'
9496
```
9597

9698
- `port` is a listening port of TCP.
@@ -104,6 +106,7 @@ port = 1234
104106
host = '192.168.x.x'
105107
trans-loopback = true
106108
trans-localfile = true
109+
line-ending = 'crlf'
107110
```
108111

109112
- `port` is a port of server.
@@ -152,6 +155,14 @@ If this option is true, server receives IP address of client. And client serve t
152155
So, server can open the local file!
153156

154157

158+
### line-ending
159+
160+
Default: "" (NONE)
161+
162+
This options works with `copy` and `paste` command only.
163+
164+
If this option is `lf` or `crlf`, lemonade converts the line ending of text to the specified.
165+
155166

156167
### Alias
157168

cli.go

-89
This file was deleted.

client/client.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,22 @@ import (
99
"net/rpc"
1010
"os"
1111

12+
"github.com/pocke/lemonade/lemon"
1213
"github.com/pocke/lemonade/param"
1314
"github.com/pocke/lemonade/server"
1415
)
1516

1617
type client struct {
17-
host string
18-
port int
18+
host string
19+
port int
20+
lineEnding string
1921
}
2022

21-
func New(host string, port int) *client {
23+
func New(c *lemon.CLI) *client {
2224
return &client{
23-
host: host,
24-
port: port,
25+
host: c.Host,
26+
port: c.Port,
27+
lineEnding: c.LineEnding,
2528
}
2629
}
2730

@@ -94,7 +97,7 @@ func (c *client) Paste() (string, error) {
9497
return "", err
9598
}
9699

97-
return resp, nil
100+
return lemon.ConvertLineEnding(resp, c.lineEnding), nil
98101
}
99102

100103
func (c *client) Copy(text string) error {
@@ -108,7 +111,7 @@ func (c *client) withRPCClient(f func(*rpc.Client) error) error {
108111
if err != nil {
109112
log.Println(err)
110113
log.Println("Fall back to localhost")
111-
rc, err = fallbackLocal()
114+
rc, err = c.fallbackLocal()
112115
if err != nil {
113116
return err
114117
}
@@ -121,8 +124,9 @@ func (c *client) withRPCClient(f func(*rpc.Client) error) error {
121124
return nil
122125
}
123126

124-
func fallbackLocal() (*rpc.Client, error) {
127+
func (c *client) fallbackLocal() (*rpc.Client, error) {
125128
port, err := server.ServeLocal()
129+
server.LineEndingOpt = c.lineEnding
126130
if err != nil {
127131
return nil, err
128132
}

lemon/cli.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package lemon
2+
3+
import "io"
4+
5+
type CommandType int
6+
7+
// Commands
8+
const (
9+
OPEN CommandType = iota + 1
10+
COPY
11+
PASTE
12+
SERVER
13+
)
14+
15+
const (
16+
Success = 0
17+
FlagParseError = iota + 10
18+
RPCError
19+
Help
20+
)
21+
22+
type CommandStyle int
23+
24+
const (
25+
ALIAS CommandStyle = iota + 1
26+
SUBCOMMAND
27+
)
28+
29+
type CLI struct {
30+
In io.Reader
31+
Out, Err io.Writer
32+
33+
Type CommandType
34+
DataSource string
35+
36+
// options
37+
Port int
38+
Allow string
39+
Host string
40+
TransLoopback bool
41+
TransLocalfile bool
42+
LineEnding string
43+
44+
Help bool
45+
}

flag.go renamed to lemon/flag.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package lemon
22

33
import (
44
"flag"
@@ -74,6 +74,7 @@ func (c *CLI) flags() *flag.FlagSet {
7474
flags.BoolVar(&c.Help, "help", false, "Show this message")
7575
flags.BoolVar(&c.TransLoopback, "trans-loopback", true, "Translate loopback address")
7676
flags.BoolVar(&c.TransLocalfile, "trans-localfile", true, "Translate local file")
77+
flags.StringVar(&c.LineEnding, "line-ending", "", "Convert Line Ending(CR/CRLF)")
7778
return flags
7879
}
7980

flag_test.go renamed to lemon/flag_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package lemon
22

33
import (
44
"os"

lemon/main.go

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package lemon
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"strings"
7+
)
8+
9+
var Version string
10+
var Usage = fmt.Sprintf(`Usage: lemonade [options]... SUB_COMMAND [arg]
11+
Sub Commands:
12+
open [URL] Open URL by browser
13+
copy [text] Copy text.
14+
paste Paste text.
15+
server Start lemonade server.
16+
17+
Options:
18+
--port=2489 TCP port number
19+
--line-ending Convert Line Ending(CR/CRLF)
20+
--allow="0.0.0.0/0,::/0" Allow IP Range [Server only]
21+
--host="localhost" Destination hostname [Client only]
22+
--trans-loopback=true Translate loopback address [open subcommand only]
23+
--trans-localfile=true Translate local file path [open subcommand only]
24+
--help Show this message
25+
26+
27+
Version:
28+
%s`, Version)
29+
30+
func ConvertLineEnding(text, option string) string {
31+
switch option {
32+
case "lf", "LF":
33+
text = strings.Replace(text, "\r\n", "\n", -1)
34+
return strings.Replace(text, "\r", "\n", -1)
35+
case "crlf", "CRLF":
36+
text = regexp.MustCompile(`\r(.)|\r$`).ReplaceAllString(text, "\r\n$1")
37+
text = regexp.MustCompile(`([^\r])\n|^\n`).ReplaceAllString(text, "$1\r\n")
38+
return text
39+
default:
40+
return text
41+
}
42+
}

lemon/main_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package lemon
2+
3+
import "testing"
4+
5+
func TestConvertLineEnding(t *testing.T) {
6+
assert := func(text, option, expected string) {
7+
if got := ConvertLineEnding(text, option); got != expected {
8+
t.Errorf("Expected: %+v, got %+v", []byte(expected), []byte(got))
9+
}
10+
}
11+
12+
assert("aaa\r\nbbb", "lf", "aaa\nbbb")
13+
assert("aaa\rbbb", "lf", "aaa\nbbb")
14+
assert("aaa\nbbb", "lf", "aaa\nbbb")
15+
16+
assert("aaa\r\nbbb", "crlf", "aaa\r\nbbb")
17+
assert("aaa\rbbb", "crlf", "aaa\r\nbbb")
18+
assert("aaa\nbbb", "crlf", "aaa\r\nbbb")
19+
20+
assert("a\r", "crlf", "a\r\n")
21+
assert("\na", "crlf", "\r\na")
22+
}

0 commit comments

Comments
 (0)