Skip to content

Commit 911d9f3

Browse files
committed
truncate hash in scan/rescan commands
1 parent 856ed1c commit 911d9f3

1 file changed

Lines changed: 28 additions & 7 deletions

File tree

cmd/scanui.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ func (m scanModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
320320
s := spinner.New()
321321
s.Spinner = spinner.Dot
322322
m.files = append(m.files, fileRow{
323-
filename: archiveName + "/" + truncSha(childHash),
323+
filename: archiveName + "/" + childHash,
324324
sha256: childHash,
325325
state: stateScanning,
326326
spinner: s,
@@ -470,10 +470,9 @@ func (m scanModel) View() string {
470470
if m.isRescan {
471471
label = " Rescanning "
472472
}
473-
s += f.spinner.View() + styleLabel.Render(label) + name + " ...\n"
473+
s += f.spinner.View() + styleLabel.Render(label) + displayName(name) + " ...\n"
474474
case stateScanning:
475-
sha := truncSha(f.sha256)
476-
s += f.spinner.View() + styleLabel.Render(" Scanning ") + name + " " + styleDim.Render(sha) + "\n"
475+
s += f.spinner.View() + styleLabel.Render(" Scanning ") + displayName(name) + " " + styleDim.Render(f.sha256) + "\n"
477476
}
478477
}
479478

@@ -486,8 +485,7 @@ func (m scanModel) View() string {
486485
name := filepath.Base(f.filename)
487486
switch f.state {
488487
case stateDone:
489-
sha := truncSha(f.sha256)
490-
line := styleSuccess.Render("✓") + " " + name + " " + styleDim.Render(sha)
488+
line := styleSuccess.Render("✓") + " " + displayName(name) + " " + styleDim.Render(f.sha256)
491489
if f.isArchive {
492490
line += " " + styleDim.Render(formatSize(f.size))
493491
line += " " + styleLabel.Render(fmt.Sprintf("archive (%d files)", f.childCount))
@@ -509,7 +507,7 @@ func (m scanModel) View() string {
509507
}
510508
doneRows = append(doneRows, doneRow{line})
511509
case stateError:
512-
line := styleError.Render("✗") + " " + name + " " + styleError.Render(f.err.Error())
510+
line := styleError.Render("✗") + " " + displayName(name) + " " + styleError.Render(f.err.Error())
513511
doneRows = append(doneRows, doneRow{line})
514512
}
515513
}
@@ -546,6 +544,29 @@ func truncSha(sha string) string {
546544
return sha
547545
}
548546

547+
// looksLikeHash returns true if s is a hex string of a common hash length
548+
// (MD5=32, SHA1=40, SHA256=64).
549+
func looksLikeHash(s string) bool {
550+
if len(s) != 32 && len(s) != 40 && len(s) != 64 {
551+
return false
552+
}
553+
for _, c := range s {
554+
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) {
555+
return false
556+
}
557+
}
558+
return true
559+
}
560+
561+
// displayName returns the name as-is, unless it looks like a hash, in which
562+
// case it is truncated to the first 12 characters.
563+
func displayName(name string) string {
564+
if looksLikeHash(name) {
565+
return truncSha(name)
566+
}
567+
return name
568+
}
569+
549570
func derivedHashes(files []entity.DerivedFile) []string {
550571
hashes := make([]string, len(files))
551572
for i, f := range files {

0 commit comments

Comments
 (0)