6969
7070local header_overrides = {[' ^' ] = style .header }
7171
72- -- refreshes the ass text using the contents of the list
73- local function update_ass ()
74- if state .hidden then state .flag_update = true ; return end
75-
76- append (style .global )
77-
78- append (style .header )
79- append (fb_utils .substitute_codes (o .format_string_header , header_overrides , nil , nil , function (str , code )
80- if code == ' ^' then return str end
81- return ass_escape (str , style .header )
82- end ))
83- newline ()
84-
85- if # state .list < 1 then
86- append (state .empty_text )
87- flush_buffer ()
88- draw ()
89- return
90- end
91-
72+ --- @return number start
73+ --- @return number finish
74+ --- @return boolean is_overflowing
75+ local function calculate_view_window ()
9276 --- @type number
9377 local start = 1
9478 --- @type number
@@ -115,48 +99,101 @@ local function update_ass()
11599 -- this is necessary when the number of items in the dir is less than the max
116100 if not overflow then finish = # state .list end
117101
118- -- these are the number values to place into the wrappers
119- local wrapper_overrides = {[' <' ] = tostring (start - 1 ), [' >' ] = tostring (# state .list - finish )}
102+ return start , finish , overflow
103+ end
104+
105+ --- @param i number index
106+ --- @return string
107+ local function calculate_item_style (i )
108+ local is_playing_file = highlight_entry (state .list [i ])
109+
110+ -- sets the selection colour scheme
111+ local multiselected = state .selection [i ]
112+
113+ -- sets the colour for the item
114+ local item_style = style .body
115+
116+ if multiselected then item_style = item_style .. style .multiselect
117+ elseif i == state .selected then item_style = item_style .. style .selected end
118+
119+ if is_playing_file then item_style = item_style .. (multiselected and style .playing_selected or style .playing ) end
120+
121+ return item_style
122+ end
123+
124+ local function draw_header ()
125+ append (style .header )
126+ append (fb_utils .substitute_codes (o .format_string_header , header_overrides , nil , nil , function (str , code )
127+ if code == ' ^' then return str end
128+ return ass_escape (str , style .header )
129+ end ))
130+ newline ()
131+ end
120132
133+ --- @param wrapper_overrides ReplacerTable
134+ local function draw_top_wrapper (wrapper_overrides )
121135 -- adding a header to show there are items above in the list
122- if o . format_string_topwrapper ~= ' ' and start > 1 then
123- append (style . footer_header , fb_utils .substitute_codes (o .format_string_topwrapper , wrapper_overrides , nil , nil , function (str )
124- return ass_escape (str )
125- end ))
126- newline ()
127- end
136+ append ( style . footer_header )
137+ append (fb_utils .substitute_codes (o .format_string_topwrapper , wrapper_overrides , nil , nil , function (str )
138+ return ass_escape (str )
139+ end ))
140+ newline ()
141+ end
128142
129- for i = start , finish do
130- local v = state .list [i ]
131- local playing_file = highlight_entry (v )
132- append (style .body )
143+ --- @param wrapper_overrides ReplacerTable
144+ local function draw_bottom_wrapper (wrapper_overrides )
145+ append (style .footer_header )
146+ append (fb_utils .substitute_codes (o .format_string_bottomwrapper , wrapper_overrides , nil , nil , function (str )
147+ return ass_escape (str )
148+ end ))
149+ end
133150
134- -- handles custom styles for different entries
135- if i == state .selected or i == state .multiselect_start then
136- if not (i == state .selected ) then append (style .selection_marker ) end
151+ --- @param i number index
152+ --- @param cursor string
153+ local function draw_cursor (i , cursor )
154+ -- handles custom styles for different entries
155+ if i == state .selected or i == state .multiselect_start then
156+ if not (i == state .selected ) then append (style .selection_marker ) end
137157
138- if not state .multiselect_start then append (style .cursor )
139- else
140- if state .selection [state .multiselect_start ] then append (style .cursor_select )
141- else append (style .cursor_deselect ) end
142- end
143- append (o .cursor_icon , " \\ h" , style .body )
158+ if not state .multiselect_start then append (style .cursor )
144159 else
145- append (g .style .indent , o .cursor_icon , " \\ h" , style .body )
160+ if state .selection [state .multiselect_start ] then append (style .cursor_select )
161+ else append (style .cursor_deselect ) end
146162 end
163+ else
164+ append (g .style .indent )
165+ end
166+ append (cursor , ' \\ h' , style .body )
167+ end
147168
148- -- sets the selection colour scheme
149- local multiselected = state .selection [i ]
169+ -- refreshes the ass text using the contents of the list
170+ local function update_ass ()
171+ if state .hidden then state .flag_update = true ; return end
150172
151- -- sets the colour for the item
152- local item_style = style .body
153- -- local function set_colour()
154- if multiselected then item_style = item_style .. style .multiselect
155- elseif i == state .selected then item_style = item_style .. style .selected end
173+ append (style .global )
174+ draw_header ()
175+
176+ if # state .list < 1 then
177+ append (state .empty_text )
178+ flush_buffer ()
179+ draw ()
180+ return
181+ end
182+
183+ local start , finish , overflow = calculate_view_window ()
184+
185+ -- these are the number values to place into the wrappers
186+ local wrapper_overrides = {[' <' ] = tostring (start - 1 ), [' >' ] = tostring (# state .list - finish )}
187+ if o .format_string_topwrapper ~= ' ' and start > 1 then
188+ draw_top_wrapper (wrapper_overrides )
189+ end
190+
191+ for i = start , finish do
192+ local v = state .list [i ]
193+ append (style .body )
194+ if g .ALIGN_X ~= ' right' then draw_cursor (i , o .cursor_icon ) end
156195
157- if playing_file then item_style = item_style .. (multiselected and style .playing_selected or style .playing ) end
158- -- end
159- -- set_colour()
196+ local item_style = calculate_item_style (i )
160197 append (item_style )
161198
162199 -- sets the folder icon
@@ -166,13 +203,13 @@ local function update_ass()
166203 end
167204
168205 -- adds the actual name of the item
169- append (v .ass or ass_escape (v .label or v .name , item_style ))
206+ append (v .ass or ass_escape (v .label or v .name , item_style ), ' \\ h' )
207+ if g .ALIGN_X == ' right' then draw_cursor (i , o .cursor_icon_flipped ) end
170208 newline ()
171209 end
172210
173211 if o .format_string_bottomwrapper ~= ' ' and overflow then
174- append (style .footer_header )
175- append (fb_utils .substitute_codes (o .format_string_bottomwrapper , wrapper_overrides , nil , nil , ass_escape ))
212+ draw_bottom_wrapper (wrapper_overrides )
176213 end
177214
178215 flush_buffer ()
0 commit comments