11local utils = require (" core.utils" )
22local Path = require (" core.path" )
3+ local ansi = require (" core.ansi" )
34
4- --- @alias CREATE_ACTION { type : " CREATE" , path : Path , lines : string[] , tostring : fun ( self : Action ): string }
5- --- @alias OVERWRITE_ACTION { type : " OVERWRITE" , path : Path , lines : string[] , tostring : fun ( self : Action ): string }
6- --- @alias MKDIR_ACTION { type : " MKDIR" , path : Path , tostring : fun ( self : Action ): string }
7- --- @alias IGNORE_ACTION { type : " IGNORE" , path : Path , lines : string[] , tostring : fun ( self : Action ): string }
8- --- @alias RENAME_ACTION { type : " RENAME" , path : Path , new_path : Path }
5+ --- @alias CREATE_ACTION { type : " CREATE" , path : Path , lines : string[] , tostring : fun ( self : Action , enable_ansi : boolean ? ): string }
6+ --- @alias OVERWRITE_ACTION { type : " OVERWRITE" , path : Path , lines : string[] , tostring : fun ( self : Action , enable_ansi : boolean ? ): string }
7+ --- @alias MKDIR_ACTION { type : " MKDIR" , path : Path , tostring : fun ( self : Action , enable_ansi : boolean ? ): string }
8+ --- @alias IGNORE_ACTION { type : " IGNORE" , path : Path , lines : string[] , tostring : fun ( self : Action , enable_ansi : boolean ? ): string }
9+ --- @alias RENAME_ACTION { type : " RENAME" , path : Path , new_path : Path , tostring : fun ( self : Action , enable_ansi : boolean ?): string }
910
1011--- @alias Action CREATE_ACTION | OVERWRITE_ACTION | MKDIR_ACTION | IGNORE_ACTION | RENAME_ACTION
1112
@@ -17,21 +18,26 @@ M.MKDIR = "MKDIR"
1718M .IGNORE = " IGNORE"
1819M .RENAME = " RENAME"
1920
20- --- TODO(gitpushjoe): add option for no colors
2121--- @param self Action
22+ --- @param enable_ansi boolean
2223--- @return string
23- local tostring = function (self )
24- local text = self .type == M .CREATE and " \27 [32m"
25- or self .type == M .MKDIR and " \27 [33m"
26- or self .type == M .OVERWRITE and " \27 [35m"
27- or self .type == M .IGNORE and " \27 [31m"
28- or self .type == M .RENAME and " \27 [96m"
29- or error (" Unknown action type: " .. self .type )
30- text = text
31- .. " [ "
32- .. string.rep (" " , # " OVERWRITE" - # self .type )
33- .. self .type
34- .. " ] "
24+ local tostring = function (self , enable_ansi )
25+ local color = enable_ansi
26+ and (({
27+ [M .CREATE ] = ansi .green ,
28+ [M .MKDIR ] = ansi .yellow ,
29+ [M .OVERWRITE ] = ansi .magenta ,
30+ [M .IGNORE ] = ansi .red ,
31+ [M .RENAME ] = ansi .cyan ,
32+ })[self .type ] or error (" Unknown action type: " .. self .type ))
33+ or ansi .none
34+ local bold = enable_ansi and ansi .bold or ansi .none
35+ local text = bold (
36+ " [ "
37+ .. string.rep (" " , # " OVERWRITE" - # self .type )
38+ .. self .type
39+ .. " ] "
40+ )
3541 local line_count_str = " -"
3642 local char_count_str = " -"
3743 if self .lines then
@@ -42,19 +48,21 @@ local tostring = function(self)
4248 end
4349 char_count_str = tostring (char_count )
4450 end
45- return text
46- .. string.rep (" " , # " lines " - # line_count_str )
47- .. line_count_str
48- .. " "
49- .. string.rep (" " , # " chars " - # char_count_str )
50- .. char_count_str
51- .. " "
52- .. tostring (self .path )
53- .. (self .type == M .RENAME and " \n " .. string.rep (
54- " " ,
55- # " action lines chars " - # " -> "
56- ) .. " -> " .. tostring (self .new_path ) or " " )
57- .. " \27 [0m"
51+ return color (
52+ text
53+ .. string.rep (" " , # " lines " - # line_count_str )
54+ .. line_count_str
55+ .. " "
56+ .. string.rep (" " , # " chars " - # char_count_str )
57+ .. char_count_str
58+ .. " "
59+ .. tostring (self .path )
60+ .. (self .type == M .RENAME and " \n " .. string.rep (
61+ " " ,
62+ # " action lines chars " - # " -> "
63+ ) .. " -> " .. tostring (self .new_path ) or " " )
64+ .. " \27 [0m"
65+ )
5866end
5967
6068--- @param path Path
0 commit comments