Skip to content

Commit 798741f

Browse files
committed
Adding support for hexadecimal
1 parent 7c2abfe commit 798741f

File tree

6 files changed

+136
-52
lines changed

6 files changed

+136
-52
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ sttr -i "your string"
9292
- [x] JSON To YAML
9393
- [x] YAML To JSON
9494
- [x] Hex To RGB
95+
- [x] Hexadecimal To String
96+
- [x] String to Hexadecimal
9597
- [x] Sort Lines
9698
- [x] **and adding more....**
9799

main.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"github.com/abhimanyu003/sttr/processors"
88
"os"
99
"strings"
10-
10+
1111
"github.com/charmbracelet/bubbles/list"
1212
tea "github.com/charmbracelet/bubbletea"
1313
"github.com/charmbracelet/lipgloss"
@@ -51,6 +51,8 @@ var items = []list.Item{
5151
item{title: "YAML To JSON", desc: "Convert YAML to JSON text", processor: processors.YAMLToJSON},
5252

5353
item{title: "Hex To RGB", desc: "Convert a #Hex code to RGB", processor: processors.HexToRGB},
54+
item{title: "Hexadecimal To String", desc: "Convert Hexadecimal to String", processor: processors.HexToString},
55+
item{title: "String To Hexadecimal", desc: "Convert your text to Hexadecimal", processor: processors.StringToHex},
5456

5557
item{title: "Sort Lines", desc: "Sort lines alphabetically", processor: processors.SortLines},
5658
}

processors/hex.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package processors
2+
3+
import (
4+
"encoding/hex"
5+
"fmt"
6+
7+
"github.com/lucasb-eyer/go-colorful"
8+
)
9+
10+
// HexToRGB convert hex color code to R, G, B codes
11+
// here we are using input library helper.
12+
func HexToRGB(input string) string {
13+
c, _ := colorful.Hex(input)
14+
15+
return fmt.Sprintf("%d, %d, %d", int(c.R*255), int(c.G*255), int(c.B*255))
16+
}
17+
18+
// HexToString convert hex color code to R, G, B codes
19+
// here we are using input library helper.
20+
func HexToString(input string) string {
21+
output, _ := hex.DecodeString(input)
22+
23+
return string(output)
24+
}
25+
26+
// StringToHex convert hex color code to R, G, B codes
27+
// here we are using input library helper.
28+
func StringToHex(input string) string {
29+
return hex.EncodeToString([]byte(input))
30+
}

processors/hex_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package processors
2+
3+
import "testing"
4+
5+
func TestHexToRGB(t *testing.T) {
6+
type args struct {
7+
input string
8+
}
9+
tests := []struct {
10+
name string
11+
args args
12+
want string
13+
}{
14+
{
15+
name: "Hex with # string",
16+
args: args{input: "#FF5733"},
17+
want: "255, 87, 51",
18+
},
19+
{
20+
name: "HEX string with wrong string",
21+
args: args{input: "#PPPPP"},
22+
want: "0, 0, 0",
23+
},
24+
{
25+
name: "HEX string with wrong string",
26+
args: args{input: "FF5733"},
27+
want: "0, 0, 0",
28+
},
29+
}
30+
for _, tt := range tests {
31+
t.Run(tt.name, func(t *testing.T) {
32+
if got := HexToRGB(tt.args.input); got != tt.want {
33+
t.Errorf("HexToRGB() = %v, want %v", got, tt.want)
34+
}
35+
})
36+
}
37+
}
38+
39+
func TestHexToString(t *testing.T) {
40+
type args struct {
41+
input string
42+
}
43+
tests := []struct {
44+
name string
45+
args args
46+
want string
47+
}{
48+
{
49+
name: "String",
50+
args: args{input: "7468697320697320737472696e67"},
51+
want: "this is string",
52+
}, {
53+
name: "Emoji",
54+
args: args{input: "f09f9883f09f9887f09f9983f09f9982f09f9889f09f988cf09f9899f09f9897f09f87aef09f87b3"},
55+
want: "😃😇🙃🙂😉😌😙😗🇮🇳",
56+
}, {
57+
name: "Multi line string",
58+
args: args{input: "48656c6c6f0a666f6f0a6261720a666f6f0a6630300a252a265e2a265e260a2a2a2a"},
59+
want: "Hello\nfoo\nbar\nfoo\nf00\n%*&^*&^&\n***",
60+
},
61+
}
62+
for _, tt := range tests {
63+
t.Run(tt.name, func(t *testing.T) {
64+
if got := HexToString(tt.args.input); got != tt.want {
65+
t.Errorf("HexToString() = %v, want %v", got, tt.want)
66+
}
67+
})
68+
}
69+
}
70+
71+
func TestStringToHex(t *testing.T) {
72+
type args struct {
73+
input string
74+
}
75+
tests := []struct {
76+
name string
77+
args args
78+
want string
79+
}{
80+
{
81+
name: "String",
82+
args: args{input: "this is string"},
83+
want: "7468697320697320737472696e67",
84+
}, {
85+
name: "Emoji",
86+
args: args{input: "😃😇🙃🙂😉😌😙😗🇮🇳"},
87+
want: "f09f9883f09f9887f09f9983f09f9982f09f9889f09f988cf09f9899f09f9897f09f87aef09f87b3",
88+
}, {
89+
name: "Multi line string",
90+
args: args{input: "Hello\nfoo\nbar\nfoo\nf00\n%*&^*&^&\n***"},
91+
want: "48656c6c6f0a666f6f0a6261720a666f6f0a6630300a252a265e2a265e260a2a2a2a",
92+
},
93+
}
94+
for _, tt := range tests {
95+
t.Run(tt.name, func(t *testing.T) {
96+
if got := StringToHex(tt.args.input); got != tt.want {
97+
t.Errorf("StringToHex() = %v, want %v", got, tt.want)
98+
}
99+
})
100+
}
101+
}

processors/rgb.go

Lines changed: 0 additions & 14 deletions
This file was deleted.

processors/rgb_test.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)