Skip to content

Commit e580495

Browse files
committed
refactor: use sub() everywhere that we should use it
1 parent e75745c commit e580495

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

lua/wincent/commandt/init.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local is_integer = require('wincent.commandt.private.is_integer')
77
local is_table = require('wincent.commandt.private.is_table')
88
local keys = require('wincent.commandt.private.keys')
99
local merge = require('wincent.commandt.private.merge')
10+
local sub = require('wincent.commandt.private.sub')
1011

1112
local commandt = {}
1213

@@ -313,7 +314,7 @@ local default_options = {
313314
fd = {
314315
command = function(directory, options)
315316
if vim.startswith(directory, './') then
316-
directory = directory:sub(3, -1)
317+
directory = sub(directory, 3)
317318
end
318319
if directory ~= '' and directory ~= '.' then
319320
directory = vim.fn.shellescape(directory)
@@ -337,7 +338,7 @@ local default_options = {
337338
find = {
338339
command = function(directory, options)
339340
if vim.startswith(directory, './') then
340-
directory = directory:sub(3, -1)
341+
directory = sub(directory, 3)
341342
end
342343
if directory ~= '' and directory ~= '.' then
343344
directory = vim.fn.shellescape(directory)
@@ -464,7 +465,7 @@ local default_options = {
464465
rg = {
465466
command = function(directory, options)
466467
if vim.startswith(directory, './') then
467-
directory = directory:sub(3, -1)
468+
directory = sub(directory, 3)
468469
end
469470
if directory ~= '' and directory ~= '.' then
470471
directory = vim.fn.shellescape(directory)

lua/wincent/commandt/private/match_listing.lua

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local match_listing = {}
55

66
local Window = require('wincent.commandt.private.window').Window
77
local merge = require('wincent.commandt.private.merge')
8+
local sub = require('wincent.commandt.private.sub')
89

910
local border_height = 2
1011
local prompt_height = 1 + border_height
@@ -64,13 +65,6 @@ local function len(str)
6465
return vim.str_utfindex(str, 'utf-32')
6566
end
6667

67-
-- Unicode-aware `sub` implementation.
68-
local function sub(str, start_char, end_char)
69-
local start_byte = vim.str_byteindex(str, 'utf-32', start_char, false)
70-
local end_byte = end_char and vim.str_byteindex(str, 'utf-32', end_char, false)
71-
return str:sub(start_byte, end_byte)
72-
end
73-
7468
local format_line = function(line, width, selected, truncate, get_icon)
7569
local prefix = selected and '> ' or ' '
7670

lua/wincent/commandt/private/sub.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- SPDX-FileCopyrightText: Copyright 2025-present Greg Hurrell and contributors.
2+
-- SPDX-License-Identifier: BSD-2-Clause
3+
4+
-- Unicode-aware `sub` implementation.
5+
local function sub(str, start_char, end_char)
6+
local start_byte = vim.str_byteindex(str, 'utf-32', start_char, false)
7+
local end_byte = end_char and vim.str_byteindex(str, 'utf-32', end_char, false)
8+
return str:sub(start_byte, end_byte)
9+
end
10+
11+
return sub

0 commit comments

Comments
 (0)