Skip to content

Commit f94b27f

Browse files
committed
hacking
1 parent 63db9e6 commit f94b27f

File tree

13 files changed

+388
-171
lines changed

13 files changed

+388
-171
lines changed

color.odin

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ BACKGROUND :: "48"
1414
// Color is an interface implemented by all colors that can be converted to an
1515
// ANSI sequence.
1616
Color :: struct {
17-
// Sequence returns the ANSI Sequence for the color.
18-
//Sequence(bg bool) string
19-
type: union {No_Color, ANSI_Color, ANSI256_Color, RGB_Color}
17+
color: string,
18+
type: union {No_Color, ANSI_Color, ANSI256_Color, RGB_Color},
2019
}
2120

2221
// No_Color is a nop for terminals that don't support colors.
2322
No_Color :: struct {
24-
using color: ^Color,
23+
using clr: ^Color,
2524
}
2625

2726
new_no_color :: proc() -> ^Color {
2827
color := new(Color)
28+
color.color = ""
2929
color.type = No_Color{color}
3030
return color
3131
}
@@ -36,12 +36,13 @@ no_color_string :: proc(c: No_Color) -> string {
3636

3737
// ANSI is a color (0-15) as defined by the ANSI Standard.
3838
ANSI_Color :: struct {
39-
using color: ^Color,
39+
using clr: ^Color,
4040
c: int,
4141
}
4242

43-
new_ansi_color :: proc(c: int) -> ^Color {
43+
new_ansi_color :: proc(c: int, s: string) -> ^Color {
4444
color := new(Color)
45+
color.color = s
4546
color.type = ANSI_Color{color, c}
4647
return color
4748
}
@@ -52,12 +53,13 @@ ansi_string :: proc(c: ANSI_Color) -> (str: string) {
5253

5354
// ANSI256_Color is a color (16-255) as defined by the ANSI Standard.
5455
ANSI256_Color :: struct {
55-
using color: ^Color,
56+
using clr: ^Color,
5657
c: int,
5758
}
5859

59-
new_ansi256_color :: proc(c: int) -> ^Color {
60+
new_ansi256_color :: proc(c: int, s: string) -> ^Color {
6061
color := new(Color)
62+
color.color = s
6163
color.type = ANSI256_Color{color, c}
6264
return color
6365
}
@@ -68,12 +70,13 @@ ansi256_string :: proc(c: ANSI256_Color) -> (str: string) {
6870

6971
// RGB is a hex-encoded color, e.g. "#abcdef".
7072
RGB_Color :: struct {
71-
using color: ^Color,
73+
using clr: ^Color,
7274
c: string,
7375
}
7476

7577
new_rgb_color :: proc(c: string) -> ^Color {
7678
color := new(Color)
79+
color.color = c
7780
color.type = RGB_Color{color, c}
7881
return color
7982
}
@@ -205,7 +208,7 @@ ansi256_to_ansi :: proc(c: ANSI256_Color) -> ^Color {
205208
}
206209
}
207210

208-
return new_ansi_color(r)
211+
return new_ansi_color(r, c.color)
209212
}
210213

211214
hex_to_ansi256 :: proc(c: colorful.Color) -> ^Color {
@@ -247,6 +250,7 @@ hex_to_ansi256 :: proc(c: colorful.Color) -> ^Color {
247250
color_dist := colorful.distance_hsluv(c, c2)
248251
gray_dist := colorful.distance_hsluv(c, g2)
249252

250-
if color_dist <= gray_dist do return new_ansi256_color(16 + ci)
251-
return new_ansi256_color(232 + gray_idx)
253+
color_string := colorful.color_hex(c)
254+
if color_dist <= gray_dist do return new_ansi256_color(16 + ci, color_string)
255+
return new_ansi256_color(232 + gray_idx, color_string)
252256
}

colorful/colors.odin

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,10 @@ hex :: proc(scol: string) -> (Color, int) {
7070
factor = 1.0 / 15.0
7171
}
7272

73-
r, g, b: u8
73+
r, g, b: i32
7474
s := strings.clone_to_cstring(scol)
7575
f := strings.clone_to_cstring(format)
7676
n := libc.sscanf(s, f, &r, &g, &b)
77-
//n, err := fmt.Sscanf(scol, format, &r, &g, &b)
7877
if n != 3 {
7978
return Color{}, 1
8079
}

constants_linux.odin

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

constants_unix.odin

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

examples/hello-world/main.odin

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package main
2+
3+
import "core:fmt"
4+
import "core:c"
5+
import "core:os"
6+
7+
import kv "../../"
8+
9+
main :: proc() {
10+
11+
err := kv.init()
12+
defer kv.close()
13+
14+
p := kv.color_profile()
15+
16+
fmt.printf("\n\t%s %s %s %s %s",
17+
kv.set_bold("bold"),
18+
kv.set_faint("faint"),
19+
kv.set_italic("italic"),
20+
kv.set_underline("underline"),
21+
kv.set_crossout("crossout"),
22+
)
23+
24+
fmt.printf("\n\t%s %s %s %s %s %s %s",
25+
kv.set_foreground("red", kv.color(p, "#E88388")),
26+
kv.set_foreground("green", kv.color(p, "#A8CC8C")),
27+
kv.set_foreground("yellow", kv.color(p, "#DBAB79")),
28+
kv.set_foreground("blue", kv.color(p, "#71BEF2")),
29+
kv.set_foreground("magenta", kv.color(p, "#D290E4")),
30+
kv.set_foreground("cyan", kv.color(p, "#66C2CD")),
31+
kv.set_foreground("gray", kv.color(p, "#B9BFCA")),
32+
)
33+
34+
fmt.printf("\n\t%s %s %s %s %s %s %s\n\n",
35+
kv.set_foreground_background("red", kv.color(p, "0"), kv.color(p, "#E88388")),
36+
kv.set_foreground_background("green", kv.color(p, "0"), kv.color(p, "#A8CC8C")),
37+
kv.set_foreground_background("yellow", kv.color(p, "0"), kv.color(p, "#DBAB79")),
38+
kv.set_foreground_background("blue", kv.color(p, "0"), kv.color(p, "#71BEF2")),
39+
kv.set_foreground_background("magenta", kv.color(p, "0"), kv.color(p, "#D290E4")),
40+
kv.set_foreground_background("cyan", kv.color(p, "0"), kv.color(p, "#66C2CD")),
41+
kv.set_foreground_background("gray", kv.color(p, "0"), kv.color(p, "#B9BFCA")),
42+
)
43+
44+
fmt.printf("\n\t%s %s\n", kv.set_bold("Has foreground color"), kv.foreground_color())
45+
fmt.printf("\t%s %s\n", kv.set_bold("Has background color"), kv.background_color())
46+
fmt.printf("\t%s %t\n", kv.set_bold("Has dark background?"), kv.has_dark_background())
47+
fmt.println("done")
48+
49+
}
50+
51+
/*
52+
func main() {
53+
restoreConsole, err := termenv.EnableVirtualTerminalProcessing(termenv.DefaultOutput())
54+
if err != nil {
55+
panic(err)
56+
}
57+
defer restoreConsole()
58+
59+
p := termenv.ColorProfile()
60+
61+
fmt.Printf("\n\t%s %s %s %s %s",
62+
termenv.String("bold").Bold(),
63+
termenv.String("faint").Faint(),
64+
termenv.String("italic").Italic(),
65+
termenv.String("underline").Underline(),
66+
termenv.String("crossout").CrossOut(),
67+
)
68+
69+
fmt.Printf("\n\t%s %s %s %s %s %s %s",
70+
termenv.String("red").Foreground(p.Color("#E88388")),
71+
termenv.String("green").Foreground(p.Color("#A8CC8C")),
72+
termenv.String("yellow").Foreground(p.Color("#DBAB79")),
73+
termenv.String("blue").Foreground(p.Color("#71BEF2")),
74+
termenv.String("magenta").Foreground(p.Color("#D290E4")),
75+
termenv.String("cyan").Foreground(p.Color("#66C2CD")),
76+
termenv.String("gray").Foreground(p.Color("#B9BFCA")),
77+
)
78+
79+
fmt.Printf("\n\t%s %s %s %s %s %s %s\n\n",
80+
termenv.String("red").Foreground(p.Color("0")).Background(p.Color("#E88388")),
81+
termenv.String("green").Foreground(p.Color("0")).Background(p.Color("#A8CC8C")),
82+
termenv.String("yellow").Foreground(p.Color("0")).Background(p.Color("#DBAB79")),
83+
termenv.String("blue").Foreground(p.Color("0")).Background(p.Color("#71BEF2")),
84+
termenv.String("magenta").Foreground(p.Color("0")).Background(p.Color("#D290E4")),
85+
termenv.String("cyan").Foreground(p.Color("0")).Background(p.Color("#66C2CD")),
86+
termenv.String("gray").Foreground(p.Color("0")).Background(p.Color("#B9BFCA")),
87+
)
88+
89+
fmt.Printf("\n\t%s %s\n", termenv.String("Has foreground color").Bold(), termenv.ForegroundColor())
90+
fmt.Printf("\t%s %s\n", termenv.String("Has background color").Bold(), termenv.BackgroundColor())
91+
fmt.Printf("\t%s %t\n", termenv.String("Has dark background?").Bold(), termenv.HasDarkBackground())
92+
fmt.Println()
93+
94+
hw := "Hello, world!"
95+
termenv.Copy(hw)
96+
fmt.Printf("\t%q copied to clipboard\n", hw)
97+
fmt.Println()
98+
99+
termenv.Notify("Termenv", hw)
100+
fmt.Print("\tTriggered a notification")
101+
fmt.Println()
102+
103+
fmt.Printf("\t%s", termenv.Hyperlink("http://example.com", "This is a link"))
104+
fmt.Println()
105+
}
106+
*/

karvi.odin

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
11
package karvi
22

33
import "core:os"
4-
import "core:c"
54
import "core:unicode/utf8"
65
import "core:strings"
76

8-
foreign import libc "system:c"
9-
10-
foreign libc {
11-
isatty :: proc(fd: c.int) -> c.int ---
12-
}
7+
import sys "syscalls"
138

149
// TODO: Unify Error and Errno
1510

@@ -21,46 +16,43 @@ Error :: enum {
2116

2217
Errno :: distinct i32
2318

24-
// ANSI Escape Sequences:
25-
// https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
19+
// Terminal escape codes:
20+
// https://medium.com/israeli-tech-radar/terminal-escape-codes-are-awesome-heres-why-c8eb938b1a1c
2621

2722
// Escape character
28-
// https://www.compart.com/en/unicode/U+001B
29-
ESC :: '\x1b'
23+
ESC :: '\e'
3024
// Bell
3125
BEL :: '\a'
3226
BELL := utf8.runes_to_string([]rune{BEL})
3327
// Control Sequence Introducer
34-
CSI := utf8.runes_to_string([]rune{ESC, '['})
28+
CSI := "\e["
3529
// Operating System Command
36-
// https://www.compart.com/en/unicode/U+009D
37-
OSC := utf8.runes_to_string([]rune{ESC, ']'})
30+
OSC := "\e]"
3831
// String Terminator:
39-
// https://www.compart.com/en/unicode/U+009C
40-
ST := utf8.runes_to_string([]rune{'\x9c'})
32+
ST := "\e\\"
4133

4234
is_tty :: proc(o: ^Output) -> bool {
4335
if o.assume_tty || o.unsafe do return true
4436
if len(o.environ.get_env("CI")) > 0 do return false
4537
fd := writer(o)
46-
if isatty(c.int(fd)) == 1 do return true
38+
if sys.is_atty(fd) == 1 do return true
4739
return false
4840
}
4941

50-
// ColorProfile returns the supported color profile:
42+
// color_profile returns the supported color profile:
5143
// Ascii, ANSI, ANSI256, or TrueColor.
5244
color_profile :: proc() -> Profile {
53-
return output_color_profile(output)
45+
return output.profile
5446
}
5547

5648
// ForegroundColor returns the terminal's default foreground color.
57-
foreground_color :: proc() -> ^Color {
58-
return output_fg_color(output)
49+
foreground_color :: proc() -> string {
50+
return output_fg_color(output).color
5951
}
6052

6153
// BackgroundColor returns the terminal's default background color.
62-
background_color :: proc() -> ^Color {
63-
return output_bg_color(output)
54+
background_color :: proc() -> string {
55+
return output_bg_color(output).color
6456
}
6557

6658
// has_dark_background returns whether terminal uses a dark-ish background.

0 commit comments

Comments
 (0)