Skip to content

Commit e094211

Browse files
committed
Fix indentation alignment issue when using custom indent values
1 parent fc1dcd5 commit e094211

File tree

3 files changed

+32
-46
lines changed

3 files changed

+32
-46
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
9+
### Fixed
10+
- Fixed indentation alignment issue when using custom indent values
11+
- Tree drawing characters now properly align with specified indentation
12+
13+
### Changed
14+
- Refactored `Node#pre_tree` method to reduce complexity
15+
- Converted multiple methods to Ruby 3.0+ shorthand notation for cleaner code
916

1017
## [3.2.0] - 2025-06-25
1118

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ git tree
3232
```
3333
-i, --indent INDENT Set indentation (2-10 spaces)
3434
-v, --version Show version
35-
-h Show help message
35+
-h, --help Show help message
3636
```
3737

3838
**Note:** Due to how git handles aliases, when using `git tree --help`, git will show the alias expansion instead of the help message. Use `git tree -h` to see the help message.

lib/node.rb

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,9 @@ def to_primitive
124124
end
125125
end
126126

127-
def file?
128-
children.nil?
129-
end
127+
def file? = children.nil?
130128

131-
def dir?
132-
!file?
133-
end
129+
def dir? = !file?
134130

135131
def valid?
136132
file? ? valid_file? : valid_dir?
@@ -162,37 +158,14 @@ def to_tree_s(depth = 0, open_parents = [0], last: true)
162158
str_tree
163159
end
164160

165-
def modified?
166-
status.include?('M')
167-
end
168-
169-
def added?
170-
status.include?('A')
171-
end
172-
173-
def deleted?
174-
status.include?('D')
175-
end
176-
177-
def renamed?
178-
status.include?('R')
179-
end
180-
181-
def copied?
182-
status.include?('C')
183-
end
184-
185-
def unmerged?
186-
status.include?('U')
187-
end
188-
189-
def new?
190-
status.include?('?')
191-
end
192-
193-
def staged?
194-
status.include?('+')
195-
end
161+
def modified? = status.include?('M')
162+
def added? = status.include?('A')
163+
def deleted? = status.include?('D')
164+
def renamed? = status.include?('R')
165+
def copied? = status.include?('C')
166+
def unmerged? = status.include?('U')
167+
def new? = status.include?('?')
168+
def staged? = status.include?('+')
196169

197170
private
198171

@@ -238,18 +211,24 @@ def valid_file?
238211
end
239212

240213
def pre_tree(depth, open_parents, last)
241-
if depth.zero?
242-
''
243-
elsif depth.positive?
244-
pre_ary = Array.new(depth).fill(' ')
245-
indent = self.class.indent - 2
214+
return '' if depth.zero?
215+
return '' unless depth.positive?
246216

247-
open_parents.each { |idx| pre_ary[idx] = "│#{' ' * indent} " if pre_ary[idx] == ' ' }
217+
pre_ary = build_pre_array(depth, open_parents)
218+
sibling(depth, self.class.indent - 2, last, open_parents, pre_ary)
219+
pre_ary * ''
220+
end
248221

249-
sibling(depth, indent, last, open_parents, pre_ary)
222+
def build_pre_array(depth, open_parents)
223+
spaces = ' ' * self.class.indent
224+
pre_ary = Array.new(depth).fill(spaces)
225+
indent = self.class.indent - 2
250226

251-
pre_ary * ''
227+
open_parents.each do |idx|
228+
pre_ary[idx] = "│#{' ' * indent} " if pre_ary[idx] == spaces
252229
end
230+
231+
pre_ary
253232
end
254233

255234
def sibling(depth, indent, last, open_parents, pre_ary)

0 commit comments

Comments
 (0)