Skip to content

Commit 3646b07

Browse files
authored
Merge branch 'main' into dev
2 parents 48062d8 + e55d6a7 commit 3646b07

File tree

15 files changed

+340
-67
lines changed

15 files changed

+340
-67
lines changed

.chglog/CHANGELOG.tpl.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{{ range .Versions }}
2+
<a name="{{ .Tag.Name }}"></a>
3+
## {{ if .Tag.Previous }}[{{ .Tag.Name }}]({{ $.Info.RepositoryURL }}/compare/{{ .Tag.Previous.Name }}...{{ .Tag.Name }}){{ else }}{{ .Tag.Name }}{{ end }}
4+
5+
> {{ datetime "2006-01-02" .Tag.Date }}
6+
7+
{{ range .CommitGroups -}}
8+
### {{ .Title }}
9+
10+
{{ range .Commits -}}
11+
* {{ .Subject }}
12+
{{ end }}
13+
{{ end -}}
14+
15+
{{- if .NoteGroups -}}
16+
{{ range .NoteGroups -}}
17+
### {{ .Title }}
18+
19+
{{ range .Notes }}
20+
{{ .Body }}
21+
{{ end }}
22+
{{ end -}}
23+
{{ end -}}
24+
{{ end -}}

.chglog/config.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
style: github
2+
template: CHANGELOG.tpl.md
3+
info:
4+
title: CHANGELOG
5+
repository_url: https://github.com/cjodo/convert.nvim
6+
options:
7+
commits:
8+
# filters:
9+
# Type:
10+
# - feat
11+
# - fix
12+
# - perf
13+
# - refactor
14+
commit_groups:
15+
# title_maps:
16+
# feat: Features
17+
# fix: Bug Fixes
18+
# perf: Performance Improvements
19+
# refactor: Code Refactoring
20+
header:
21+
pattern: "^(\\w*)\\:\\s(.*)$"
22+
pattern_maps:
23+
- Type
24+
- Subject
25+
notes:
26+
keywords:
27+
- BREAKING CHANGE

README.md

Lines changed: 72 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,106 @@
11
# convert.nvim
22

3-
https://github.com/user-attachments/assets/46129dd1-35b0-41ce-a1d8-ead1922d8af4
3+
https://github.com/user-attachments/assets/46320296-58c1-408c-9fd5-e3ee757d9288
44

55
## Dependencies
66
- [nui.nvim](https://github.com/MunifTanjim/nui.nvim): UI Components
77

88
## Features
99
- Convert css units with one simple command
10-
- track base font size on save to convert px to rems accurately (Single file support only)
10+
- Base font supported for accurate rem conversion
11+
- Convert all in a selection or entire buffer
1112

1213
## Installation:
1314
Use your favourite plugin manager
1415

1516
- Lazy:
1617
```lua
1718
return {
18-
'cjodo/convert.nvim',
19-
dependencies = {
20-
'MunifTanjim/nui.nvim'
21-
},
22-
keys = {
23-
{ "<leader>cn", "<cmd>ConvertFindNext<CR>", desc = "Find next convertable unit" },
24-
{ "<leader>cc", "<cmd>ConvertFindCurrent<CR>", desc = "Find convertable unit in current line" },
25-
{ "<leader>ca", "<cmd>ConvertAll<CR>", desc = "Convert all of a specified unit" },
26-
},
19+
'cjodo/convert.nvim',
20+
dependencies = {
21+
'MunifTanjim/nui.nvim'
22+
},
23+
keys = {
24+
{ "<leader>cn", "<cmd>ConvertFindNext<CR>", desc = "Find next convertable unit" },
25+
{ "<leader>cc", "<cmd>ConvertFindCurrent<CR>", desc = "Find convertable unit in current line" },
26+
-- Add "v" to enable converting a selected region
27+
{ "<leader>ca", "<cmd>ConvertAll<CR>", mode = {"n", "v"}, desc = "Convert all of a specified unit" },
28+
},
2729
}
30+
```
31+
- Packer:
32+
```lua
33+
use {
34+
'cjodo/convert.nvim',
35+
requires = { 'MunifTanjim/nui.nvim' },
36+
config = function()
37+
require('convert').setup()
38+
vim.keymap.set('n', '<leader>cn', '<cmd>ConvertFindNext<CR>', { desc = 'Find next convertible unit' })
39+
vim.keymap.set('n', '<leader>cc', '<cmd>ConvertFindCurrent<CR>', { desc = 'Find convertible unit in current line' })
40+
vim.keymap.set({ 'n', 'v' }, '<leader>ca', '<cmd>ConvertAll<CR>', { desc = 'Convert all of a specified unit' })
41+
end
42+
}
43+
2844
```
2945
## Commands:
3046

3147
| Command | Description |
3248
|----------------------|---------------------------------------------------------------------------|
3349
| :ConvertFindNext | Finds the next convertible unit |
3450
| :ConvertFindCurrent | Finds the convertible unit in the current line (starting from cursor) |
35-
| :ConvertAll | Converts all instances of a given unit to another |
51+
| :ConvertAll | Converts all instances in a buffer or visual mode selection of one unit to another of the same type (size, color) |
3652

3753
## Usage
3854
You can choose you're own custom keys for the ui menu
3955

4056
```lua
41-
config = function()
57+
config = function()
4258
local convert = require('convert')
4359
-- defaults
4460
convert.setup({
45-
keymaps = {
46-
focus_next = { "j", "<Down>", "<Tab>" },
47-
focus_prev = { "k", "<Up>", "<S-Tab>" },
48-
close = { "<Esc>", "<C-c>", 'qq' },
49-
submit = { "<CR>", "<Space>" },
50-
}
61+
keymaps = {
62+
focus_next = { "j", "<Down>", "<Tab>" },
63+
focus_prev = { "k", "<Up>", "<S-Tab>" },
64+
close = { "<Esc>", "<C-c>", 'qq' },
65+
submit = { "<CR>", "<Space>" },
66+
},
67+
modes = { "color", "size", "numbers" } -- available conversion modes
5168
})
52-
end
69+
end
5370

5471
```
72+
## Supported Conversions
73+
74+
### Size Units 📏
75+
76+
| Unit | Description |
77+
|------|------------|
78+
| `px` | Pixels |
79+
| `rem` | Relative to root element |
80+
| `cm` | Centimeters |
81+
| `in` | Inches |
82+
| `mm` | Millimeters |
83+
| `pt` | Points |
84+
| `pc` | Picas |
85+
86+
---
87+
88+
### Color Formats 🎨
89+
90+
| Format | Description |
91+
|--------|------------|
92+
| `rgb` | Red-Green-Blue |
93+
| `hex` | Hexadecimal color code |
94+
| `hsl` | Hue-Saturation-Lightness |
95+
96+
---
97+
98+
### Number Systems 🔢 (Experimental)
99+
- Defined with respective prefix: 0b, 0x, 0o
100+
101+
| Format | Description |
102+
|-------------|------------|
103+
| `bin` | Binary |
104+
| `hexadecimal` | Hexadecimal |
105+
| `octal` | Octal |
106+

lazy.lua

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

lua/convert/calculator.lua

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ local utils = require("convert.utils")
33

44
local M = {}
55

6-
76
---@param from string
87
---@param to string
9-
---@param val number
8+
---@param val number|string
109
---@return string
1110
M.convert = function(from, to, val)
1211
if from == to then
@@ -18,12 +17,22 @@ M.convert = function(from, to, val)
1817
return tostring(val)
1918
end
2019

20+
if (from == "bin" or "hexadecimal" or "octal") then
21+
local cases = {
22+
["bin"] = function () return "0b" .. val end,
23+
["hexadecimal"] = function () return "0x" .. val end,
24+
["octal"] = function () return "0o" .. val end,
25+
}
26+
27+
return tostring((cases[from])())
28+
end
29+
2130
return val .. from
2231
end
2332

2433
local round = 0
2534

26-
if (to ~= 'px') then
35+
if to ~= 'px' then
2736
round = 2
2837
end
2938

@@ -37,6 +46,18 @@ M.convert = function(from, to, val)
3746
return "#" .. result
3847
end
3948

49+
if to == 'bin' then
50+
return "0b".. result
51+
end
52+
53+
if to == 'hexadecimal' then
54+
return "0x".. result
55+
end
56+
57+
if to == 'octal' then
58+
return "0o".. result
59+
end
60+
4061
return tostring(utils.round(result, round)) .. to
4162
end
4263

lua/convert/config.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ config.keymaps = {
88
submit = { "<CR>", "<Space>" },
99
}
1010

11+
config.modes = { "color", "size", "number" }
12+
1113
return config

lua/convert/convert_all.lua

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,6 @@ local convert_all = function(bufnr, from, to)
2222
end
2323

2424
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
25-
for row = 1, #lines, 1 do
26-
local line = lines[row]
27-
2825
local all_units = utils.find_all_units_in_line(line, row)
2926
if all_units ~= nil and #all_units > 0 and all_units[1].unit == from then
3027
for i = #all_units, 1, -1 do

lua/convert/converters.lua

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,54 @@ converters.hsl = {
275275
end
276276
}
277277

278+
converters.bin = {
279+
hexadecimal = function(val)
280+
local decimal = tonumber(val, 2)
281+
if not decimal then
282+
error("invalid bin string: " .. val, 1)
283+
end
284+
285+
return string.format("%X", decimal)
286+
end,
287+
288+
octal = function (val)
289+
local decimal = tonumber(val, 2)
290+
if not decimal then
291+
error("invalid bin string: " .. val, 1)
292+
return
293+
end
294+
295+
local octal = ""
296+
while decimal > 0 do
297+
local remainder = decimal % 8
298+
octal = remainder .. octal
299+
decimal = math.floor(decimal / 8)
300+
end
301+
302+
return octal == "" and "0" or octal
303+
end
304+
}
305+
306+
converters.hexadecimal = {
307+
bin = function (val)
308+
return utils.num_convert(val, 16, 2)
309+
end,
310+
octal = function (val)
311+
return utils.num_convert(val, 16, 8)
312+
end
313+
}
314+
315+
converters.octal = {
316+
bin = function (val)
317+
return utils.num_convert(val, 8, 2)
318+
end,
319+
hexadecimal = function (val)
320+
local decimal = tonumber(val, 8)
321+
return string.format("%X", decimal)
322+
end,
323+
}
324+
325+
278326
local function convert(from, to, val)
279327
if converters[from] and converters[from][to] then
280328
return converters[from][to](val)

lua/convert/init.lua

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@ local ui = require("convert.ui.open_popup")
33
local config = require("convert.config")
44
local split = require("convert.ui.open_split")
55

6+
local Menu = require("nui.menu")
67

78
local M = {}
89

910
M.setup = function(opts)
1011
if opts.keymaps then
1112
config.keymaps = opts.keymaps
1213
end
14+
if opts.modes then
15+
config.modes = opts.modes
16+
end
1317
end
1418

1519
M.find_next = function()
@@ -51,7 +55,34 @@ M.find_current = function()
5155
end
5256

5357
M.convert_all = function()
54-
split.open_split(config)
58+
local units_menu = {}
59+
-- TODO: refactor this
60+
if utils.contains(config.modes, "color") then
61+
table.insert(units_menu, Menu.separator('Colors', { char = '-', text_align = 'left' }))
62+
table.insert(units_menu, Menu.item('rgb'))
63+
table.insert(units_menu, Menu.item('hex'))
64+
table.insert(units_menu, Menu.item('hsl'))
65+
end
66+
67+
if utils.contains(config.modes, "size") then
68+
table.insert(units_menu, Menu.separator('Size', { char = '-', text_align = 'left' }))
69+
table.insert(units_menu, Menu.item('px'))
70+
table.insert(units_menu, Menu.item('rem'))
71+
table.insert(units_menu, Menu.item('cm'))
72+
table.insert(units_menu, Menu.item('in'))
73+
table.insert(units_menu, Menu.item('mm'))
74+
table.insert(units_menu, Menu.item('pt'))
75+
table.insert(units_menu, Menu.item('pc'))
76+
end
77+
78+
if utils.contains(config.modes, "number") then
79+
table.insert(units_menu, Menu.separator('Numbers', { char = '-', text_align = 'left' }))
80+
table.insert(units_menu, Menu.item('bin'))
81+
table.insert(units_menu, Menu.item('hexadecimal'))
82+
table.insert(units_menu, Menu.item('octal'))
83+
end
84+
85+
split.open_split(config, units_menu)
5586
end
5687

5788
return M

lua/convert/patterns.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ M.matchers = {
44
px = "(%d+%.?%d*)px",
55
rem = "(%d+%.?%d*)rem",
66
em = "(%d+%.?%d*)em",
7-
['in'] = "(%d+%.?%d*)in", -- in is a reserved lua keyword
7+
['in'] = "(%d+%.?%d*)in", -- "in" is a reserved lua keyword
88
ch = "(%d+%.?%d*)ch",
99
mm = "(%d+%.?%d*)mm",
1010
cm = "(%d+%.?%d*)cm",
@@ -13,6 +13,9 @@ M.matchers = {
1313
rgb = 'rgb%(%d+, %d+, %d+%)',
1414
hex = "#(%x%x%x%x?%x?%x?%x?%x?)",
1515
hsl = 'hsl%(%d+, %d+%%, %d+%%%)',
16+
bin = '0b([01]+)',
17+
octal = '0o([0-7]+)',
18+
hexadecimal = '0x([%da-fA-F]+)',
1619
}
1720

1821
M.extract = {

0 commit comments

Comments
 (0)