当前位置:  开发笔记 > 编程语言 > 正文

用什么工具绘制文件树图

如何解决《用什么工具绘制文件树图》经验,为你挑选了3个好方法。

给定一个文件树 - 一个包含目录的目录等,你如何编写一个脚本来创建一个文件树图表作为一个图形文件,我可以嵌入一个文字处理器文档.我更喜欢矢量(SVG,EPS,EMF ...)文件.该工具必须在Windows上运行,但最好是跨平台的.该工具可以是商业的,但最好是免费的.

更新2012-02-20.这个问题与文档子项目有关.我不得不解释文件(特别是资源和配置文件)所在的位置.我最终使用了dos tree命令.我两个屏幕抓取结果(对于短文件夹)和更长的文件夹我重定向到文本文件,然后我编辑.例如,如果一个子文件夹包含20个类似类型的文件,这些文件分别对我正在制作的点不重要,我只剩下两个并用一个......替换其余的文件.然后我再次将文件打印到控制台,屏幕抓住了它.在屏幕抓取之前,我必须将前景颜色修改为黑色,将背景颜色修改为白色,以便更好地查看并在文档中保存墨水.

令人惊讶的是,它没有更好的工具.如果我有时间,我会写一个Visio扩展,或者可能是一些产生SVG的命令行.SVG是HTML5不合格的,甚至可以轻松地包含在在线文档中.

2017-10-17更新.对不起,这个问题因为不属于SO而被删除了.所以我已经重新措辞了.我需要一个脚本 - 而不是WYSIWYG.所以任何脚本语言或库都可以.所以这是一个代码编写问题,我相信属于SO.



1> svinto..:

从MS-DOS tree命令复制和粘贴也可能适合您.例子:

C:\Foobar>tree
C:.
????FooScripts
????barconfig
????Baz
?   ????BadBaz
?   ????Drop
...

树/ F.

C:\Foobar>tree
C:.
????FooScripts
?    foo.sh
????barconfig
?    bar.xml
????Baz
?   ????BadBaz
?   ?    badbaz.xml
?   ????Drop
...

树/ A.

C:\Foobar>tree /A
C:.
+---FooScripts
+---barconfig
+---Baz
¦   +---BadBaz
¦   \---Drop
...

树/ F/A.

C:\Foobar>tree /A
C:.
+---FooScripts
¦    foo.sh
+---barconfig
¦    bar.xml
+---Baz
¦   +---BadBaz
¦   ¦    badbaz.xml
¦   \---Drop
...

语法[ 来源 ]

tree[ drive:] [ path] [ /F] [ /A]

drive:\path - 包含磁盘的驱动器和目录,用于显示目录结构,不包含列表文件.

/F - 包括生活在每个目录中的所有文件.

/A - 将用于链接行的图形字符替换为ext字符,而不是图形字符./a用于不支持图形字符的代码页,并将输出发送到不能正确解释图形字符的打印机.


Linux也有这样的"树"命令,我刚刚在查看了Stack Overflow问题后发现了.谢谢你指出我应该寻找的名字!"tree -A"是如何使用漂亮的绘图字符创建树; 普通的"树"只限于ASCII.

2> joel.neely..:

Graphviz - 来自网页:

Graphviz布局程序以简单的文本语言描述图形,并以几种有用的格式制作图表,例如网页的图像和SVG,包含在PDF或其他文档中的Postscript; 或在交互式图形浏览器中显示.(Graphviz还支持XML方言GXL.)

这是我发现的最简单,最高效的工具,可以创建各种盒子和线条图.我有和使用Visio和OmniGraffle,但总是有诱惑"只需再调整一次".

编写代码以生成Graphiz消耗的"点文件"格式也非常容易,因此自动化图表生成也很容易实现.



3> PhiLho..:

正如所承诺的,这是我的开罗版本.我用Lua编写脚本,使用lfs来遍历目录.我喜欢这些小挑战,因为它们允许我探索我想要挖掘的API很长一段时间......
lfs和LuaCairo都是跨平台的,所以它应该适用于其他系统(在法语WinXP Pro SP3上测试).

当我走过树时,我制作了第一个绘制文件名的版本.优点:没有内存开销.不便之处:我必须事先指定图像大小,因此列表可能会被切断.

所以我创建了这个版本,首先遍历目录树,将其存储在Lua表中.然后,知道文件的数量,创建适合(至少垂直)的画布并绘制名称.
您可以轻松地在PNG渲染和SVG渲染之间切换.后者的问题:开罗在低级别生成它,绘制字母而不是使用SVG的文本功能.好吧,至少,即使在没有字体的系统上,它也能保证准确的结果.但文件更大...如果你压缩它后,并没有真正的问题,有一个.svgz文件.
或者直接生成SVG不应该太难,我过去使用Lua生成SVG.

-- LuaFileSystem 
require"lfs"
-- LuaCairo 
require"lcairo"
local CAIRO = cairo


local PI = math.pi
local TWO_PI = 2 * PI

--~ local dirToList = arg[1] or "C:/PrgCmdLine/Graphviz"
--~ local dirToList = arg[1] or "C:/PrgCmdLine/Tecgraf"
local dirToList = arg[1] or "C:/PrgCmdLine/tcc"
-- Ensure path ends with /
dirToList = string.gsub(dirToList, "([^/])$", "%1/")
print("Listing: " .. dirToList)
local fileNb = 0

--~ outputType = 'svg'
outputType = 'png'

-- dirToList must have a trailing slash
function ListDirectory(dirToList)
  local dirListing = {}
  for file in lfs.dir(dirToList) do
    if file ~= ".." and file ~= "." then
      local fileAttr = lfs.attributes(dirToList .. file)
      if fileAttr.mode == "directory" then
        dirListing[file] = ListDirectory(dirToList .. file .. '/')
      else
        dirListing[file] = ""
      end
      fileNb = fileNb + 1
    end
  end
  return dirListing
end

--dofile[[../Lua/DumpObject.lua]] -- My own dump routine
local dirListing = ListDirectory(dirToList)
--~ print("\n" .. DumpObject(dirListing))
print("Found " .. fileNb .. " files")

--~ os.exit()

-- Constants to change to adjust aspect
local initialOffsetX = 20
local offsetY = 50
local offsetIncrementX = 20
local offsetIncrementY = 12
local iconOffset = 10

local width = 800 -- Still arbitrary
local titleHeight = width/50
local height = offsetIncrementY * (fileNb + 1) + titleHeight
local outfile = "CairoDirTree." .. outputType

local ctxSurface
if outputType == 'svg' then
  ctxSurface = cairo.SvgSurface(outfile, width, height)
else
  ctxSurface = cairo.ImageSurface(CAIRO.FORMAT_RGB24, width, height)
end
local ctx = cairo.Context(ctxSurface)

-- Display a file name
-- file is the file name to display
-- offsetX is the indentation
function DisplayFile(file, bIsDir, offsetX)
  if bIsDir then
    ctx:save()
    ctx:select_font_face("Sans", CAIRO.FONT_SLANT_NORMAL, CAIRO.FONT_WEIGHT_BOLD)
    ctx:set_source_rgb(0.5, 0.0, 0.7)
  end

  -- Display file name
  ctx:move_to(offsetX, offsetY)
  ctx:show_text(file)

  if bIsDir then
    ctx:new_sub_path() -- Position independent of latest move_to
    -- Draw arc with absolute coordinates
    ctx:arc(offsetX - iconOffset, offsetY - offsetIncrementY/3, offsetIncrementY/3, 0, TWO_PI)
    -- Violet disk
    ctx:set_source_rgb(0.7, 0.0, 0.7)
    ctx:fill()
    ctx:restore() -- Restore original settings
  end

  -- Increment line offset
  offsetY = offsetY + offsetIncrementY
end

-- Erase background (white)
ctx:set_source_rgb(1.0, 1.0, 1.0)
ctx:paint()

--~ ctx:set_line_width(0.01)

-- Draw in dark blue
ctx:set_source_rgb(0.0, 0.0, 0.3)
ctx:select_font_face("Sans", CAIRO.FONT_SLANT_NORMAL, CAIRO.FONT_WEIGHT_BOLD)
ctx:set_font_size(titleHeight)
ctx:move_to(5, titleHeight)
-- Display title
ctx:show_text("Directory tree of " .. dirToList)

-- Select font for file names
ctx:select_font_face("Sans", CAIRO.FONT_SLANT_NORMAL, CAIRO.FONT_WEIGHT_NORMAL)
ctx:set_font_size(10)
offsetY = titleHeight * 2

-- Do the job
function DisplayDirectory(dirToList, offsetX)
  for k, v in pairs(dirToList) do
--~ print(k, v)
    if type(v) == "table" then
      -- Sub-directory
      DisplayFile(k, true, offsetX)
      DisplayDirectory(v, offsetX + offsetIncrementX)
    else
      DisplayFile(k, false, offsetX)
    end
  end
end

DisplayDirectory(dirListing, initialOffsetX)

if outputType == 'svg' then
    cairo.show_page(ctx)
else
  --cairo.surface_write_to_png(ctxSurface, outfile)
  ctxSurface:write_to_png(outfile)
end

ctx:destroy()
ctxSurface:destroy()

print("Found " .. fileNb .. " files")

当然,您可以更改样式.我没有绘制连接线,我没有看到它的必要.我可以稍后选择添加它们.

推荐阅读
U友50081205_653
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有