mirror of
https://github.com/doublecmd/doublecmd.git
synced 2026-06-21 09:58:13 +00:00
UPD: Speed up file type coloring by template
This commit is contained in:
parent
2b1de2cd3f
commit
dc4ac9d60a
2 changed files with 93 additions and 100 deletions
|
|
@ -4,7 +4,7 @@
|
|||
Load colors of files in file panels
|
||||
|
||||
Copyright (C) 2003-2004 Radek Cervinka (radek.cervinka@centrum.cz)
|
||||
Copyright (C) 2006-2017 Alexander Koblov (alexx2000@mail.ru)
|
||||
Copyright (C) 2006-2020 Alexander Koblov (alexx2000@mail.ru)
|
||||
Copyright (C) 2008 Dmitry Kolomiets (B4rr4cuda@rambler.ru)
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
|
|
@ -30,7 +30,7 @@ unit uColorExt;
|
|||
interface
|
||||
|
||||
uses
|
||||
Classes, Graphics, uFile, uMasks, DCXmlConfig;
|
||||
Classes, Graphics, uFile, uMasks, uSearchTemplate, DCXmlConfig;
|
||||
|
||||
type
|
||||
|
||||
|
|
@ -39,11 +39,15 @@ type
|
|||
TMaskItem = class
|
||||
private
|
||||
FExt: String;
|
||||
FModeStr: String;
|
||||
FMaskList: TMaskList;
|
||||
FAttrList: TMaskList;
|
||||
FTemplate: TSearchTemplate;
|
||||
procedure SetExt(const AValue: String);
|
||||
procedure SetModeStr(const AValue: String);
|
||||
public
|
||||
sName: String;
|
||||
sModeStr: String;
|
||||
|
||||
cColor: TColor;
|
||||
|
||||
constructor Create;
|
||||
|
|
@ -51,13 +55,14 @@ type
|
|||
|
||||
procedure Assign(ASource: TMaskItem);
|
||||
property sExt: String read FExt write SetExt;
|
||||
property sModeStr: String read FModeStr write SetModeStr;
|
||||
end;
|
||||
|
||||
{ TColorExt }
|
||||
|
||||
TColorExt = class
|
||||
private
|
||||
lslist: TList;
|
||||
FMaskItems: TList;
|
||||
|
||||
function GetCount: Integer;
|
||||
function GetItems(const Index: Integer): TMaskItem;
|
||||
|
|
@ -86,10 +91,41 @@ uses
|
|||
{ TMaskItem }
|
||||
|
||||
procedure TMaskItem.SetExt(const AValue: String);
|
||||
var
|
||||
ATemplate: TSearchTemplate;
|
||||
begin
|
||||
FExt:= AValue;
|
||||
FreeAndNil(FMaskList);
|
||||
FMaskList:= TMaskList.Create(FExt);
|
||||
// Plain mask
|
||||
if not IsMaskSearchTemplate(FExt) then
|
||||
begin
|
||||
FreeAndNil(FTemplate);
|
||||
if (Length(FExt) > 0) then
|
||||
FMaskList:= TMaskList.Create(FExt);
|
||||
end
|
||||
// Search template
|
||||
else begin
|
||||
ATemplate:= gSearchTemplateList.TemplateByName[PAnsiChar(FExt) + 1];
|
||||
if (ATemplate = nil) then
|
||||
FreeAndNil(FTemplate)
|
||||
else begin
|
||||
if (FTemplate = nil) then begin
|
||||
FTemplate:= TSearchTemplate.Create;
|
||||
end;
|
||||
FTemplate.SearchRecord:= ATemplate.SearchRecord;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TMaskItem.SetModeStr(const AValue: String);
|
||||
begin
|
||||
if FModeStr <> AValue then
|
||||
begin
|
||||
FModeStr:= AValue;
|
||||
FreeAndNil(FAttrList);
|
||||
if (Length(FModeStr) > 0) then
|
||||
FAttrList:= TMaskList.Create(FModeStr);
|
||||
end;
|
||||
end;
|
||||
|
||||
constructor TMaskItem.Create;
|
||||
|
|
@ -99,7 +135,9 @@ end;
|
|||
|
||||
destructor TMaskItem.Destroy;
|
||||
begin
|
||||
FreeAndNil(FMaskList);
|
||||
FAttrList.Free;
|
||||
FTemplate.Free;
|
||||
FMaskList.Free;
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
|
|
@ -114,39 +152,38 @@ end;
|
|||
|
||||
function TColorExt.GetCount: Integer;
|
||||
begin
|
||||
Result := lslist.Count;
|
||||
Result := FMaskItems.Count;
|
||||
end;
|
||||
|
||||
function TColorExt.GetItems(const Index: Integer): TMaskItem;
|
||||
begin
|
||||
Result := TMaskItem(lslist[Index]);
|
||||
Result := TMaskItem(FMaskItems[Index]);
|
||||
end;
|
||||
|
||||
constructor TColorExt.Create;
|
||||
begin
|
||||
inherited;
|
||||
lslist:= TList.Create;
|
||||
FMaskItems:= TList.Create;
|
||||
end;
|
||||
|
||||
destructor TColorExt.Destroy;
|
||||
begin
|
||||
Clear;
|
||||
FreeAndNil(lsList);
|
||||
inherited;
|
||||
FreeAndNil(FMaskItems);
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
procedure TColorExt.Clear;
|
||||
begin
|
||||
while lslist.Count > 0 do
|
||||
begin
|
||||
TMaskItem(lslist[0]).Free;
|
||||
lslist.Delete(0);
|
||||
end;
|
||||
while FMaskItems.Count > 0 do
|
||||
begin
|
||||
TMaskItem(FMaskItems[0]).Free;
|
||||
FMaskItems.Delete(0);
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TColorExt.Add(AItem: TMaskItem);
|
||||
begin
|
||||
lslist.Add(AItem);
|
||||
FMaskItems.Add(AItem);
|
||||
end;
|
||||
|
||||
function TColorExt.GetColorByExt(const sExt: String): TColor;
|
||||
|
|
@ -154,11 +191,11 @@ var
|
|||
I: Integer;
|
||||
begin
|
||||
Result:= clDefault;
|
||||
for I:=0 to lslist.Count-1 do
|
||||
for I:=0 to FMaskItems.Count-1 do
|
||||
begin
|
||||
if MatchesMaskList(sExt, TMaskItem(lslist[I]).sExt,';') then
|
||||
if MatchesMaskList(sExt, TMaskItem(FMaskItems[I]).sExt,';') then
|
||||
begin
|
||||
Result:= TMaskItem(lslist[I]).cColor;
|
||||
Result:= TMaskItem(FMaskItems[I]).cColor;
|
||||
Exit;
|
||||
end;
|
||||
end;
|
||||
|
|
@ -169,11 +206,11 @@ var
|
|||
I: Integer;
|
||||
begin
|
||||
Result:= clDefault;
|
||||
for I:=0 to lslist.Count-1 do
|
||||
for I:=0 to FMaskItems.Count-1 do
|
||||
begin
|
||||
if MatchesMaskList(sModeStr,TMAskItem(lslist[I]).sModeStr,';') then
|
||||
if MatchesMaskList(sModeStr,TMAskItem(FMaskItems[I]).sModeStr,';') then
|
||||
begin
|
||||
Result:=TMAskItem(lslist[I]).cColor;
|
||||
Result:=TMAskItem(FMaskItems[I]).cColor;
|
||||
Exit;
|
||||
end;
|
||||
end;
|
||||
|
|
@ -182,44 +219,40 @@ end;
|
|||
function TColorExt.GetColorBy(const AFile: TFile): TColor;
|
||||
var
|
||||
Attr: String;
|
||||
I, J: Integer;
|
||||
Index: Integer;
|
||||
MaskItem: TMaskItem;
|
||||
begin
|
||||
Result:= clDefault;
|
||||
|
||||
if not (fpAttributes in AFile.SupportedProperties) then
|
||||
Attr:= EmptyStr
|
||||
else begin
|
||||
Attr:= AFile.Properties[fpAttributes].AsString;
|
||||
end;
|
||||
for I:= 0 to lslist.Count-1 do
|
||||
|
||||
for Index:= 0 to FMaskItems.Count - 1 do
|
||||
begin
|
||||
MaskItem:= TMaskItem(lslist[I]);
|
||||
MaskItem:= TMaskItem(FMaskItems[Index]);
|
||||
|
||||
// Get color by search template
|
||||
if MaskItem.sExt[1] = '>' then
|
||||
if IsMaskSearchTemplate(MaskItem.FExt) then
|
||||
begin
|
||||
for J:= 0 to gSearchTemplateList.Count - 1 do
|
||||
with gSearchTemplateList do
|
||||
begin
|
||||
if (Templates[J].TemplateName = PChar(MaskItem.sExt)+1) and
|
||||
Templates[J].CheckFile(AFile) then
|
||||
begin
|
||||
Result:= MaskItem.cColor;
|
||||
Exit;
|
||||
end;
|
||||
end;
|
||||
if Assigned(MaskItem.FTemplate) and MaskItem.FTemplate.CheckFile(AFile) then
|
||||
begin
|
||||
Result:= MaskItem.cColor;
|
||||
Exit;
|
||||
end;
|
||||
Continue;
|
||||
end;
|
||||
|
||||
// Get color by extension and attribute.
|
||||
// If attributes field is empty then don't match directories.
|
||||
if ((MaskItem.sExt = '') or
|
||||
(((MaskItem.sModeStr <> '') or
|
||||
if ((MaskItem.FMaskList = nil) or (((MaskItem.FAttrList <> nil) or
|
||||
not (AFile.IsDirectory or AFile.IsLinkToDirectory)) and
|
||||
MaskItem.FMaskList.Matches(AFile.Name)))
|
||||
and
|
||||
((MaskItem.sModeStr = '') or (Length(Attr) = 0) or
|
||||
MatchesMaskList(Attr, MaskItem.sModeStr, ';')) then
|
||||
((MaskItem.FAttrList = nil) or (Length(Attr) = 0) or
|
||||
MaskItem.FAttrList.Matches(Attr)) then
|
||||
begin
|
||||
Result:= MaskItem.cColor;
|
||||
Exit;
|
||||
|
|
@ -227,51 +260,6 @@ begin
|
|||
end;
|
||||
end;
|
||||
|
||||
(* Load colors of files from doublecmd.ini *)
|
||||
|
||||
{ format of colors storage as in Total Commander:
|
||||
doublecmd.ini
|
||||
[Colors]
|
||||
ColorFilter1=*.o;*.ppu;*.rst;*.bak;*.dcu
|
||||
ColorFilter1Color=16711680
|
||||
ColorFilter2=*.pas
|
||||
ColorFilter2Color=16711000
|
||||
etc...
|
||||
|
||||
Added Attributes:
|
||||
ColorFilter1Attributes=-r*xr*xr*x //all read/executable file
|
||||
ColorFilter2Attributes=-*x* //all executable
|
||||
ColorFilter3Attributes=d* //all directories
|
||||
ColorFilter4Attributes=l* //all links
|
||||
|
||||
Be careful with * expression. Functions return just first found value.
|
||||
|
||||
This is right demo of [Colors] section:
|
||||
ColorFilter3=*
|
||||
ColorFilter3Color=55758
|
||||
ColorFilter3Attributes=-rwxrwxr*x
|
||||
ColorFilter3Name=SomeName3
|
||||
ColorFilter4=*
|
||||
ColorFilter4Color=32768
|
||||
ColorFilter4Attributes=-*x*
|
||||
ColorFilter4Name=SomeName4
|
||||
|
||||
This IS WRONG because ColorFilter3Attributes=-*x* will be
|
||||
found and ColorFilter3Color=32768 will be returned first:
|
||||
ColorFilter3=*
|
||||
ColorFilter3Color=32768
|
||||
ColorFilter3Attributes=-*x*
|
||||
ColorFilter3Name=SomeName3
|
||||
ColorFilter4=*
|
||||
ColorFilter4Color=55758
|
||||
ColorFilter4Attributes=-rwxrwxr*x
|
||||
ColorFilter4Name=SomeName4
|
||||
|
||||
|
||||
!!! The "?" and other regular expressions DOES NOT SUPPORTED
|
||||
|
||||
}
|
||||
|
||||
procedure TColorExt.Load(AConfig: TXmlConfig; ANode: TXmlNode);
|
||||
var
|
||||
sExtMask,
|
||||
|
|
@ -294,11 +282,11 @@ begin
|
|||
AConfig.TryGetValue(ANode, 'Color', iColor) and
|
||||
AConfig.TryGetValue(ANode, 'Attributes', sAttr) then
|
||||
begin
|
||||
lsList.Add(TMaskItem.Create);
|
||||
TMaskItem(lsList.Last).sName := sName;
|
||||
TMaskItem(lsList.Last).cColor := iColor;
|
||||
TMaskItem(lsList.Last).sExt := sExtMask;
|
||||
TMaskItem(lsList.Last).sModeStr := sAttr;
|
||||
FMaskItems.Add(TMaskItem.Create);
|
||||
TMaskItem(FMaskItems.Last).sName := sName;
|
||||
TMaskItem(FMaskItems.Last).cColor := iColor;
|
||||
TMaskItem(FMaskItems.Last).sExt := sExtMask;
|
||||
TMaskItem(FMaskItems.Last).sModeStr := sAttr;
|
||||
end
|
||||
else
|
||||
begin
|
||||
|
|
@ -315,19 +303,19 @@ var
|
|||
I : Integer;
|
||||
SubNode: TXmlNode;
|
||||
begin
|
||||
if not Assigned(lslist) then
|
||||
if not Assigned(FMaskItems) then
|
||||
Exit;
|
||||
|
||||
ANode := AConfig.FindNode(ANode, 'FileFilters', True);
|
||||
AConfig.ClearNode(ANode);
|
||||
|
||||
for I:=0 to lslist.Count - 1 do
|
||||
for I:=0 to FMaskItems.Count - 1 do
|
||||
begin
|
||||
SubNode := AConfig.AddNode(ANode, 'Filter');
|
||||
AConfig.AddValue(SubNode, 'Name', TMaskItem(lsList[I]).sName);
|
||||
AConfig.AddValue(SubNode, 'FileMasks', TMaskItem(lsList[I]).sExt);
|
||||
AConfig.AddValue(SubNode, 'Color', TMaskItem(lsList[I]).cColor);
|
||||
AConfig.AddValue(SubNode, 'Attributes', TMaskItem(lsList[I]).sModeStr);
|
||||
AConfig.AddValue(SubNode, 'Name', TMaskItem(FMaskItems[I]).sName);
|
||||
AConfig.AddValue(SubNode, 'FileMasks', TMaskItem(FMaskItems[I]).sExt);
|
||||
AConfig.AddValue(SubNode, 'Color', TMaskItem(FMaskItems[I]).cColor);
|
||||
AConfig.AddValue(SubNode, 'Attributes', TMaskItem(FMaskItems[I]).sModeStr);
|
||||
end;
|
||||
end;
|
||||
|
||||
|
|
|
|||
|
|
@ -2576,8 +2576,6 @@ begin
|
|||
gLogInfoColor:= GetValue(Node, 'LogWindow/Info', gLogInfoColor);
|
||||
gLogErrorColor:= GetValue(Node, 'LogWindow/Error', gLogErrorColor);
|
||||
gLogSuccessColor:= GetValue(Node, 'LogWindow/Success', gLogSuccessColor);
|
||||
|
||||
gColorExt.Load(gConfig, Node);
|
||||
end;
|
||||
|
||||
{ ToolTips page }
|
||||
|
|
@ -3092,6 +3090,13 @@ begin
|
|||
{ Search template list }
|
||||
gSearchTemplateList.LoadFromXml(gConfig, Root);
|
||||
|
||||
{ File type colors, load after search templates }
|
||||
Node := Root.FindNode('Colors');
|
||||
if Assigned(Node) then
|
||||
begin
|
||||
gColorExt.Load(gConfig, Node);
|
||||
end;
|
||||
|
||||
{ Columns sets }
|
||||
ColSet.Load(gConfig, Root);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue