1
1
-- module represents a lua module for the plugin
2
- local types = require (" types" )
2
+ --- @module ' carbon-now'
3
+ --- @class Carbon
4
+ --- @field config ConfigSchema
5
+ --- @field setup SetupFunc
3
6
local Carbon = {}
4
7
8
+ --- Types
9
+ --- @alias Themes ' 3024-night' | ' a11y-dark' | ' blackboard' | ' base-16-dark' | ' base-16-light' | ' cobalt' | ' dracula-pro' | ' duotone' | ' hopscotch' | ' lucario' | ' material' | ' monokai' | ' night-owl' | ' nord' | ' oceanic-next' | ' one-light' | ' one-dark' | ' panda' | ' paraiso' | ' seti' | ' shades-of-purple' | ' solarized-dark' | ' solarized-light' | ' synthwave-84' | ' twilight' | ' terminal' | ' vscode' | ' yeti' | ' zenburn'
10
+
11
+ --- @class WindowOptions
12
+ --- @field public bg string
13
+ --- @field public drop_shadow_blur string
14
+ --- @field public drop_shadow_offset_y string
15
+ --- @field public font_family string
16
+ --- @field public font_size string
17
+ --- @field public line_height string
18
+ --- @field public line_numbers boolean
19
+ --- @field public drop_shadow boolean
20
+ --- @field public theme Themes
21
+ --- @field public titlebar string
22
+ --- @field public watermark boolean
23
+ --- @field public width string
24
+ --- @field public window_theme ' none' | ' sharp' | ' bw' | ' boxy'
25
+ --- @field public padding_vertical string
26
+ --- @field public padding_horizontal string
27
+
28
+ --- @class ConfigSchema
29
+ --- @field public base_url string
30
+ --- @field public options WindowOptions
31
+
32
+ --- @alias SetupFunc fun ( params : ConfigSchema )
33
+
5
34
-- map some known filetypes to carbon.now.sh supported languages
6
35
--- list of supported languages: https://github.com/carbon-app/carbon/blob/2cbdcd0cc23d2d2f23736dd3cfbe94134b141191/lib/constants.js#L624-L1048
36
+ --- TODO: check possible removal of this in favor of vim.filetype
7
37
local language_map = {
8
38
typescriptreact = " typescript" ,
9
39
javascriptreact = " javascript" ,
10
40
}
11
41
12
42
-- default config
13
- --- @type cn.ConfigSchema
14
43
Carbon .config = {
15
44
base_url = " https://carbon.now.sh/" ,
16
- open_cmd = " xdg-open" ,
17
45
options = {
18
46
bg = " gray" ,
19
47
drop_shadow_blur = " 68px" ,
@@ -23,11 +51,11 @@ Carbon.config = {
23
51
font_size = " 18px" ,
24
52
line_height = " 133%" ,
25
53
line_numbers = true ,
26
- theme = types . Themes . monokai ,
54
+ theme = " monokai" ,
27
55
titlebar = " Made with carbon-now.nvim" ,
28
56
watermark = false ,
29
57
width = " 680" ,
30
- window_theme = types . WindowThemes . sharp ,
58
+ window_theme = " sharp" ,
31
59
padding_horizontal = " 0px" ,
32
60
padding_vertical = " 0px" ,
33
61
},
@@ -48,9 +76,9 @@ local function query_param_encode(str)
48
76
end
49
77
50
78
--- helper function to encode and concatenate a [k,v] table
51
- --- as query parameters. iEx : {a = b, c = d} --> a=b&c=d
79
+ --- as query parameters. Ex : {a = b, c = d} --> a=b&c=d
52
80
--- @param values table
53
- --- @return string # Concatanation of the enconded query parameters
81
+ --- @return string # Concatenation of the encoded query parameters
54
82
--- @nodiscard
55
83
local function encode_params (values )
56
84
--- @type table<string , any>
@@ -96,35 +124,8 @@ local function get_carbon_now_snapshot_uri(code)
96
124
return encode_params (params )
97
125
end
98
126
99
- --- @nodiscard
100
- --- @return string
101
- --- Returns the launch command. If no launch command is
102
- --- available an exception will be raised.
103
- local function get_open_command ()
104
- -- default launcher
105
- if vim .fn .executable (Carbon .config .open_cmd ) then
106
- return Carbon .config .open_cmd
107
- end
108
-
109
- -- alternative launcher
110
- if vim .fn .executable (" open" ) then
111
- return " open"
112
- end
113
-
114
- -- windows fallback
115
- if vim .fn .has (" win32" ) then
116
- return " start"
117
- end
118
-
119
- vim .api .nvim_err_writeln (" [carbon-now.nvim] Couldn't find a launch command" )
120
- return " echo"
121
- end
122
-
123
127
--- @param opts { args : string , line1 : integer , line2 : integer }
124
128
local function create_snapshot (opts )
125
- -- get launch command
126
- local open_cmd = get_open_command ()
127
-
128
129
--- @type string , string
129
130
local url , query_params
130
131
@@ -140,19 +141,16 @@ local function create_snapshot(opts)
140
141
end
141
142
142
143
-- launch the Uri
143
- local cmd = open_cmd .. " " .. " '" .. url .. " '"
144
- vim .fn .system (cmd )
144
+ vim .ui .open (url )
145
145
end
146
146
147
147
local function create_commands ()
148
- --- @param opts table<string , any>
149
148
vim .api .nvim_create_user_command (" CarbonNow" , function (opts )
150
149
create_snapshot (opts )
151
150
end , { range = " %" , nargs = " ?" })
152
151
end
153
152
154
153
--- initialization function for the carbon plugin commands
155
- --- @param params cn.ConfigSchema ?
156
154
Carbon .setup = function (params )
157
155
Carbon .config = vim .tbl_deep_extend (" force" , {}, Carbon .config , params or {})
158
156
create_commands ()
0 commit comments