|
7 | 7 | "fmt" |
8 | 8 | "math" |
9 | 9 | "os" |
| 10 | + "sort" |
10 | 11 | "strconv" |
11 | 12 | "strings" |
12 | 13 |
|
@@ -318,11 +319,24 @@ func title_lines(left_path, right_path string, columns, margin_size int, ans []* |
318 | 319 |
|
319 | 320 | type LogicalLines struct { |
320 | 321 | lines []*LogicalLine |
| 322 | + title_line_indices []int // sorted indices of TITLE_LINEs in lines -- used for sticky_header |
321 | 323 | margin_size, columns int |
322 | 324 | } |
323 | 325 |
|
324 | 326 | func (self *LogicalLines) At(i int) *LogicalLine { return self.lines[i] } |
325 | 327 |
|
| 328 | +func (self *LogicalLines) TitleLineIdxFor(line_idx int) int { |
| 329 | + if len(self.title_line_indices) == 0 || line_idx < 0 { |
| 330 | + return -1 |
| 331 | + } |
| 332 | + // first title index > line_idx, so the one before it is our answer |
| 333 | + i := sort.SearchInts(self.title_line_indices, line_idx+1) |
| 334 | + if i == 0 { |
| 335 | + return -1 |
| 336 | + } |
| 337 | + return self.title_line_indices[i-1] |
| 338 | +} |
| 339 | + |
326 | 340 | func (self *LogicalLines) ScreenLineAt(pos ScrollPos) *ScreenLine { |
327 | 341 | if pos.logical_line < len(self.lines) && pos.logical_line >= 0 { |
328 | 342 | line := self.lines[pos.logical_line] |
@@ -793,8 +807,13 @@ func rename_lines(path, other_path string, columns, margin_size int, ans []*Logi |
793 | 807 | func render(collection *Collection, diff_map map[string]*Patch, screen_size screen_size, largest_line_number int, image_size graphics.Size) (result *LogicalLines, err error) { |
794 | 808 | margin_size := utils.Max(3, len(strconv.Itoa(largest_line_number))+1) |
795 | 809 | ans := make([]*LogicalLine, 0, 1024) |
| 810 | + var title_line_indices []int |
| 811 | + track_titles := conf != nil && conf.Sticky_header |
796 | 812 | columns := screen_size.columns |
797 | 813 | err = collection.Apply(func(path, item_type, changed_path string) error { |
| 814 | + if track_titles { |
| 815 | + title_line_indices = append(title_line_indices, len(ans)) |
| 816 | + } |
798 | 817 | ans = title_lines(path, changed_path, columns, margin_size, ans) |
799 | 818 | defer func() { |
800 | 819 | ans = append(ans, &LogicalLine{line_type: EMPTY_LINE, screen_lines: []*ScreenLine{{}}}) |
@@ -863,5 +882,5 @@ func render(collection *Collection, diff_map map[string]*Patch, screen_size scre |
863 | 882 | // Having am empty list of lines causes panics later on |
864 | 883 | ll = []*LogicalLine{{line_type: EMPTY_LINE, screen_lines: []*ScreenLine{{}}}} |
865 | 884 | } |
866 | | - return &LogicalLines{lines: ll, margin_size: margin_size, columns: columns}, err |
| 885 | + return &LogicalLines{lines: ll, title_line_indices: title_line_indices, margin_size: margin_size, columns: columns}, err |
867 | 886 | } |
0 commit comments