Mark selected files

This commit is contained in:
Kovid Goyal
2025-07-10 19:39:18 +05:30
parent 3469e1b044
commit 35b2553723
3 changed files with 41 additions and 55 deletions

View File

@@ -166,6 +166,7 @@ func (s State) WindowTitle() string {
}
return s.window_title
}
func (s *State) AddSelection(abspath string) bool {
if !slices.Contains(s.selections, abspath) {
s.selections = append(s.selections, abspath)
@@ -182,6 +183,14 @@ func (s *State) ToggleSelection(abspath string) {
}
}
func (s *State) IsSelected(x *ResultItem) bool {
if len(s.selections) == 0 {
return false
}
q := filepath.Join(s.CurrentDir(), x.text)
return slices.Contains(s.selections, q)
}
type ScreenSize struct {
width, height, cell_width, cell_height, width_px, height_px int
}
@@ -270,14 +279,6 @@ func (h *Handler) current_abspath() string {
}
func (h *Handler) add_selection_if_possible() bool {
m := h.current_abspath()
if m != "" {
return h.state.AddSelection(m)
}
return false
}
func (h *Handler) toggle_selection() bool {
m := h.current_abspath()
if m != "" {
@@ -380,32 +381,25 @@ func (h *Handler) dispatch_action(name, args string) (err error) {
}
case "accept":
m := h.current_abspath()
if m == "" {
h.lp.Beep()
return
}
if h.state.mode.SelectFiles() {
if m != "" {
var s os.FileInfo
if s, err = os.Stat(m); err != nil {
h.lp.Beep()
return nil
}
if s.IsDir() {
return h.change_to_current_dir_if_possible()
}
}
}
if h.add_selection_if_possible() {
if len(h.state.selections) > 0 {
return h.finish_selection()
}
return h.draw_screen()
} else {
if h.state.mode.CanSelectNonExistent() {
t := h.state.SearchText()
h.initialize_save_file_name(utils.IfElse(t == "", h.state.suggested_save_file_name, t))
return h.draw_screen()
} else {
var s os.FileInfo
if s, err = os.Stat(m); err != nil {
h.lp.Beep()
return nil
}
if s.IsDir() {
return h.change_to_current_dir_if_possible()
}
}
h.state.AddSelection(m)
if len(h.state.selections) > 0 {
return h.finish_selection()
}
return h.draw_screen()
case "cd":
switch args {
case "current":

View File

@@ -57,6 +57,7 @@ func (h *Handler) draw_no_matches_message(in_progress bool) {
}
const matching_position_style = "fg=green"
const selected_style = "fg=magenta"
const current_style = "fg=intense-white bold"
func (h *Handler) render_match_with_positions(text string, add_ellipsis bool, positions []int, is_current bool) {
@@ -127,7 +128,13 @@ func (h *Handler) draw_column_of_matches(matches ResultsType, current_idx int, x
for i, m := range matches {
h.lp.QueueWriteString("\r")
h.lp.MoveCursorHorizontally(x)
icon := icon_for(filepath.Join(root_dir, m.text), m.ftype)
is_selected := h.state.IsSelected(m)
var icon string
if is_selected {
icon = "󰗠 "
} else {
icon = icon_for(filepath.Join(root_dir, m.text), m.ftype)
}
text := m.text
add_ellipsis := false
if wcswidth.Stringwidth(text) > available_width-3 {
@@ -138,7 +145,11 @@ func (h *Handler) draw_column_of_matches(matches ResultsType, current_idx int, x
if is_current {
h.lp.QueueWriteString(h.lp.SprintStyled(matching_position_style, icon+" "))
} else {
h.lp.QueueWriteString(icon + " ")
if is_selected {
h.lp.QueueWriteString(h.lp.SprintStyled(selected_style, icon+" "))
} else {
h.lp.QueueWriteString(icon + " ")
}
}
h.render_match_with_positions(text, add_ellipsis, m.sorted_positions(), is_current)
h.lp.MoveCursorVertically(1)
@@ -255,22 +266,3 @@ func (h *Handler) move_sideways(leftwards bool) {
}
}
}
func (h *Handler) handle_result_list_keys(ev *loop.KeyEvent) bool {
switch {
case ev.MatchesPressOrRepeat("down"):
h.next_result(1)
return true
case ev.MatchesPressOrRepeat("up"):
h.next_result(-1)
return true
case ev.MatchesPressOrRepeat("left") || ev.MatchesPressOrRepeat("pgup"):
h.move_sideways(true)
return true
case ev.MatchesPressOrRepeat("right") || ev.MatchesPressOrRepeat("pgdn"):
h.move_sideways(false)
return true
default:
return false
}
}

View File

@@ -58,20 +58,20 @@ func (h *Handler) draw_controls(y int) (max_width int) {
}
lines := make([]entry, 0, SEARCH_BAR_HEIGHT)
add_control := func(icon, text string, callback func()) {
line := icon + " " + text
line := icon + " " + text
width := wcswidth.Stringwidth(line)
max_width = max(max_width, width)
lines = append(lines, entry{line, callback, width})
}
add_control(utils.IfElse(h.state.ShowHidden(), "", ""), utils.IfElse(h.state.ShowHidden(), "hide dotfiles", "show dotfiles"), func() {
add_control(utils.IfElse(h.state.ShowHidden(), " ", " "), utils.IfElse(h.state.ShowHidden(), "hide dotfiles", "show dotfiles"), func() {
h.state.show_hidden = !h.state.show_hidden
h.result_manager.set_show_hidden()
})
add_control("󰑑", utils.IfElse(h.state.RespectIgnores(), "show ignored", "hide ignored"), func() {
add_control("󰑑 ", utils.IfElse(h.state.RespectIgnores(), "show ignored", "hide ignored"), func() {
h.state.respect_ignores = !h.state.respect_ignores
h.result_manager.set_respect_ignores()
})
add_control(utils.IfElse(h.state.SortByLastModified(), "", ""), utils.IfElse(h.state.SortByLastModified(), "sort names", "sort dates"), func() {
add_control(utils.IfElse(h.state.SortByLastModified(), " ", " "), utils.IfElse(h.state.SortByLastModified(), "sort names", "sort dates"), func() {
h.state.sort_by_last_modified = !h.state.sort_by_last_modified
h.result_manager.set_sort_by_last_modified()
})