1- local mod = require ' down.mod '
2- local log = require ' down.util.log'
3- local config = require ' down.config '
1+ local config = require ( " down.config " )
2+ local log = require ( " down.util.log" )
3+ local mod = require ( " down.mod " )
44
55--- @class down.mod.Data : down.Mod
6- local M = mod .new ' data'
6+ local M = mod .new ( " data" )
77
8- --- @class down.mod..Data
8+ --- @class down.mod.data .Data
99M .data = {}
1010--- @type down.Store<down.File>
1111M .file = {
@@ -14,7 +14,7 @@ M.file = {
1414
1515--- @return down.mod.Setup
1616M .setup = function ()
17- vim .api .nvim_create_autocmd (' VimLeavePre' , {
17+ vim .api .nvim_create_autocmd (" VimLeavePre" , {
1818 callback = function ()
1919 M .flush ()
2020 end ,
@@ -31,14 +31,14 @@ M.load = function() end
3131
3232--- @class down.mod..Config
3333M .config = {
34- path = vim .fn .stdpath ' data' .. ' /down.mpack' ,
34+ path = vim .fn .stdpath ( " data" ) .. " /down.mpack" ,
3535 dir = {
36- vim = vim .fs .joinpath (vim .fn .stdpath ' data' , ' down/' ),
37- home = vim .fs .joinpath (os.getenv ' HOME' or ' ~/ ' , ' .down/' ),
36+ vim = vim .fs .joinpath (vim .fn .stdpath ( " data" ), " down/" ),
37+ home = vim .fs .joinpath (os.getenv ( " HOME" ) or " ~/ " , " .down/" ),
3838 },
3939 file = {
40- vim = vim .fs .joinpath (vim .fn .stdpath ' data' , ' down/' , ' down.json' ),
41- home = vim .fs .joinpath (os.getenv ' HOME' or ' ~/ ' , ' .down/' , ' down.json' ),
40+ vim = vim .fs .joinpath (vim .fn .stdpath ( " data" ), " down/" , " down.json" ),
41+ home = vim .fs .joinpath (os.getenv ( " HOME" ) or " ~/ " , " .down/" , " down.json" ),
4242 },
4343}
4444
5151--- @return table<string>
5252M .files = function (path , cond )
5353 local f = {}
54- local dir = path or vim .fs .root (vim .fn .cwd (), ' .down/' )
54+ local dir = path or vim .fs .root (vim .fn .cwd (), " .down/" )
5555 for name , type in vim .fs .dir (dir ) do
56- if type == ' file' and cond or name :endswith ' .md' then
56+ if type == " file" and cond or name :endswith ( " .md" ) then
5757 table.insert (f , name )
58- elseif type == ' directory' and not name :startswith ' . ' then
58+ elseif type == " directory" and not name :startswith ( " . " ) then
5959 local fs = M .get_files (M .concat (path , name ))
6060 for _ , v in ipairs (fs ) do
6161 table.insert (f , v )
6666
6767M .directory_map = function (path , callback )
6868 for name , type in vim .fs .dir (path ) do
69- if type == ' directory' then
69+ if type == " directory" then
7070 M .directory_map (M .concat (path , name ), callback )
7171 else
7272 callback (name , type , path )
8080--- succeed if the directory already exists.
8181--- @return boolean #If true, the directory copying succeeded
8282M .copy_directory = function (old_path , new_path )
83- local file_permissions = tonumber (' 744' , 8 )
83+ local file_permissions = tonumber (" 744" , 8 )
8484 local ok , err = vim .loop .fs_mkdir (new_path , file_permissions )
8585
8686 if not ok then
8787 return ok , err --- @diagnostic disable-line -- TODO : type error workaround <pysan3 >
8888 end
8989
9090 for name , type in vim .fs .dir (old_path ) do
91- if type == ' file' then
92- ok , err = vim .loop .fs_copyfile (M .concat (old_path , name ), M .concat (new_path , name ))
91+ if type == " file" then
92+ ok , err =
93+ vim .loop .fs_copyfile (M .concat (old_path , name ), M .concat (new_path , name ))
9394
9495 if not ok then
9596 return ok , err --- @diagnostic disable-line -- TODO : type error workaround <pysan3 >
9697 end
97- elseif type == ' directory' and not vim .endswith (new_path , name ) then
98- ok , err = M .copy_directory (M .concat (old_path , name ), M .concat (new_path , name ))
98+ elseif type == " directory" and not vim .endswith (new_path , name ) then
99+ ok , err =
100+ M .copy_directory (M .concat (old_path , name ), M .concat (new_path , name ))
99101
100102 if not ok then
101103 return ok , err --- @diagnostic disable-line -- TODO : type error workaround <pysan3 >
@@ -107,11 +109,11 @@ M.copy_directory = function(old_path, new_path)
107109end
108110--- Grabs the data present on disk and overwrites it with the data present in memory
109111M .sync = function ()
110- local file = io.open (M .config .path , ' r ' )
112+ local file = io.open (M .config .path , " r " )
111113 if not file then
112114 return
113115 end
114- local content = file :read ' *a '
116+ local content = file :read ( " *a " )
115117 file :close ()
116118 M .data = vim .mpack .decode and vim .mpack .decode (content )
117119end
121123--- @param data any #The data to store at the specific key
122124M .put = function (key , data )
123125 M .data [key ] = data
126+ M .flush ()
124127end
125128
126129M .set = M .put
@@ -141,18 +144,20 @@ end
141144M .json = function (path )
142145 local dir = M .config .dir .home
143146 local vim = M .config .dir .vim
144- vim .fn .mkdir (dir , ' p ' )
145- vim .fn .mkdir (vim , ' p ' )
146- local f = io.open (path or M .config .file .vim , ' w ' )
147+ vim .fn .mkdir (dir , " p " )
148+ vim .fn .mkdir (vim , " p " )
149+ local f = io.open (path or M .config .file .vim , " w " )
147150 f :write (vim .json .encode (M .data ))
148151end
149152--- Flushes the contents in memory to the location specified
150153M .flush = function (path )
151- local file = io.open (path or M .config .path , ' w ' )
154+ local file = io.open (path or M .config .path , " w " )
152155 if not file then
153156 return
154157 end
155- file :write (vim .mpack .encode and vim .mpack .encode (M .data ) or vim .mpack .pack (M .data ))
158+ file :write (
159+ vim .mpack .encode and vim .mpack .encode (M .data ) or vim .mpack .pack (M .data )
160+ )
156161 file :close ()
157162end
158163
0 commit comments