Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown.lua - Steam version update #901

Merged
merged 18 commits into from
Apr 30, 2024
Merged

Conversation

glotov4
Copy link
Contributor

@glotov4 glotov4 commented Nov 21, 2023

Updated script to work with the Steam version

Features:

  • Added support of screens:
    • Unit/Health/Description
    • Unit/Personality/Traits
    • Item/Description
  • Added name, age & profession of units to description
  • Improved markdown formatting (headers, line dividers)
  • By default, the file name is now “markdown_/Your_World_Name/_export.md”

Docs:

  • Moved the helpstr to markdown.rst
  • Added tags
  • Improved docs
  • Added error messages with instructions

Refactoring:

  • Refactored the code
  • Removed code for processing of unsupported screens (it had time & month processing, as well as logic for other screens - might be useful in the future)
  • Argument '-n' is now '-o' for "overwrite" and it can be placed before or after the filename
  • Replacing whitespaces from user input with underscores
  • Changed success message based on write mode
  • Bug fixes (of the new script):
    • Clicks on tabs, used for data loading, now use relative coordinates to support different screen resolutions
    • Removed redundant check "if unit or item"

Manual testing done before opening the PR:

  • Units:
    • Dwarves
    • Dwarven children
    • Units of other races (elves, humans, goblins, bear-people, etc.)
    • Tame animals
    • Named tame animals
    • Stray animals
  • Items:
    • Items in stockpiles (incl. refuse)
    • Items in world (on the ground)
    • Items installed as furniture
    • Items in storage
    • Items equipped/carried by units
  • Saving:
    • Append default file (markdown)
    • Append custom file (markdown descriptions)
    • Overwrite default file (markdown -o)
    • Overwrite custom file (markdown descriptions -o / markdown -o descriptions)

Features:
- Added support of screens:
    - Unit/Health/Description
    - Unit/Personality/Traits
    - Item/Description
- Added name, age & profession of units to description
- Improved markdown formatting (headers, line dividers)
- By default, the file name is now “markdown_/Your_World_Name/_export.md”

Docs:
- Moved the helpstr to markdown.rst
- Added tags
- Improved docs
- Added error messages with instructions

Refactoring:
- Refactored the code
- Removed code for processing of unsupported screens (it had time & month processing, as well as logic for other screens - might be useful in the future)
- Argument '-n' is now '-o' for "overwrite" and it can be placed before or after the filename
- Replacing whitespaces from user input with underscores
- Changed success message based on write mode
- Bug fixes (of the new script):
    - Clicks on tabs, used for data loading, now use relative coordinates to support different screen resolutions
    - Removed redundant check "if unit or item"
@glotov4
Copy link
Contributor Author

glotov4 commented Nov 22, 2023

Hi @myk002 ,
I force pushed the commit that should have no trailing whitespaces (I've used Lua extension for VSC) and should have an empty line at the end of the file, but pre-commit.ci still says "checks completed with failures". Can you please take a look, and help me understand what I'm doing wrong?

@glotov4
Copy link
Contributor Author

glotov4 commented Nov 23, 2023

pre-commit.ci autofix

docs/markdown.rst Outdated Show resolved Hide resolved
docs/markdown.rst Outdated Show resolved Hide resolved
docs/markdown.rst Show resolved Hide resolved
docs/markdown.rst Outdated Show resolved Hide resolved
docs/markdown.rst Outdated Show resolved Hide resolved
docs/markdown.rst Outdated Show resolved Hide resolved
docs/markdown.rst Outdated Show resolved Hide resolved
docs/markdown.rst Outdated Show resolved Hide resolved
docs/markdown.rst Outdated Show resolved Hide resolved

Options
-------
markdown
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this is a tool that could benefit from a global hotkey - (Ctrl-Shift-s?) so people can click on things with the mouse and export them quickly with their left hand. This would be added to the keybindings file in the other repo: https://github.com/DFHack/dfhack/blob/develop/data/init/dfhack.keybindings.init

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll add it later, thank you

Copy link
Contributor Author

@glotov4 glotov4 Nov 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct way to add keybinding? I'm not sure what @dawrfmode means, and maybe I should be using @textviewer instead

save description of selected unit or item in markdown format
keybinding add Ctrl-Shift-S@dwarfmode markdown

Copy link
Member

@myk002 myk002 Dec 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that refers to the "context" where the hotkey should be considered active. dwarfmode is fort mode, but you can make the hotkey local to more specific contexts (the strings returned from dfhack.gui.getCurFocus(true)). that way, the hotkey will only show up in the DFHack context menu where it is applicable.

e.g.

keybinding add Ctrl-Shift-S@dwarfmode/ViewSheets/UNIT|dwarfmode/ViewSheets/ITEM|...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same, let's add it later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I call the command with the hotkey, it doesn't show success message (or any message). Is it expected behavior?

Copy link
Contributor Author

@glotov4 glotov4 Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using Shift+R.

keybinding list Shift-R gives an empty list.

The potential conflicts I see are:
Ctrl+R - recording
(easy to misclick)

and

materials: q->workshop; b->select items:
#keybinding add Shift-R "job-material RHYOLITE"
Which is fine, because we will only use the command in our context window

Pull request to add the hotkey:
DFHack/dfhack#4470

markdown.lua Outdated Show resolved Hide resolved
markdown.lua Outdated
local unit = dfhack.gui.getSelectedUnit(true)

if not item and not unit then
print([[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use dfhack.printerr for a nice red color

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code returns white text. Is it expected behavior?
Screenshot 2023-11-23 204134

if not item and not unit then
    dfhack.printerr([[
Error: No unit or item is currently selected.
- To select a unit, click on it.
- For items that are installed as buildings (like statues or beds),
open the building's interface and click the magnifying glass icon.
Please select a valid target and try running the script again.]])
    -- Early return to avoid proceeding further if no unit or item is selected
    return
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, the red color only shows up on the DFHack console. the launcher output won't ever show color since color information isn't available via the API that gui/launcher uses

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I resolve this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the best way to resolve this would be to create a proper UI for the tool -- add an export link to the screens where export is supported. In fact, this might be a better solution than the global keybinding since the feature will be more discoverable.

Copy link
Member

@myk002 myk002 Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example overlay code (which you'd integrate into markdown.lua):

--@ module=true

local overlay = require('plugins.overlay')
local widgets = require('gui.widgets')

MarkdownOverlay = defclass(MarkdownOverlay, overlay.OverlayWidget)
MarkdownOverlay.ATTRS{
    desc='Adds links to screen where text can be exported to a file.',
    default_pos={x=-30, y=21},
    default_enabled=true,
    viewscreens={
        'dwarfmode/ViewSheets/ITEM',
        'dwarfmode/ViewSheets/UNIT',
    },
    frame={w=21, h=1},
}

function MarkdownOverlay:init()
    self:addviews{
        widgets.TextButton{
            view_id='button',
            label='Export text',
            key='CUSTOM_CTRL_T',
            on_activate=self:callback('do_export'),
        },
        widgets.TextButton{
            view_id='button_good',
            label='Export done',
            text_pen=COLOR_GREEN,
            key='CUSTOM_CTRL_T',
            visible=false,
        },
    }
end

function MarkdownOverlay:do_export()
    dfhack.run_script('markdown')
    self.subviews.button.visible = false
    self.subviews.button_good.visible = true
    local end_ms = dfhack.getTickCount() + 5000
    local function label_reset()
        if dfhack.getTickCount() < end_ms then
            dfhack.timeout(10, 'frames', label_reset)
        else
            self.subviews.button_good.visible = false
            self.subviews.button.visible = true
        end
    end
    label_reset()
end

OVERLAY_WIDGETS = {overlay=MarkdownOverlay}

default_pos might need some tweaking -- I just made those numbers up without testing

I also used Ctrl-T since that's what many screen-specific DFHack overlays use

markdown.lua Outdated Show resolved Hide resolved
@myk002
Copy link
Member

myk002 commented Nov 23, 2023

I don't think it changes anything for this PR, but be aware that we're correcting some of the definitions for the internal markup structures:

https://github.com/DFHack/df-structures/pull/679/files

Changes:
- Replaced manual argument parsing with DFHack's argparse.processArgsGetopt
- Now support "--overwrite" argument.
- Switched to dfhack.printerr
- Default filename is now ``markdown_{worldname}.md`` instead of ``markdown_{worldname}_export.md``
- Removed unused variables
- Mention UTF-8 and argument option in docs
- Minor docs improvements
- Other minor changes to the markdown.rst
- Added (hard-coded) root folder name to the export success message (Exported to ``Dwarf Fortress/markdown_{worldname}.md``)
@myk002
Copy link
Member

myk002 commented Dec 20, 2023

but pre-commit.ci still says "checks completed with failures". Can you please take a look, and help me understand what I'm doing wrong?

It looks like you figured this out. Sorry for the long delay!

docs/markdown.rst Outdated Show resolved Hide resolved
docs/markdown.rst Outdated Show resolved Hide resolved
docs/markdown.rst Outdated Show resolved Hide resolved
docs/markdown.rst Show resolved Hide resolved
docs/markdown.rst Outdated Show resolved Hide resolved
docs/markdown.rst Outdated Show resolved Hide resolved
markdown.lua Outdated Show resolved Hide resolved
markdown.lua Outdated Show resolved Hide resolved
docs/markdown.rst Outdated Show resolved Hide resolved
Co-authored-by: Myk <myk.taylor@gmail.com>
@glotov4
Copy link
Contributor Author

glotov4 commented Dec 27, 2023

I will be away from computer for another 3 weeks, will get back to it end of Jan probably. Thank you for the review!

glotov4 and others added 3 commits April 12, 2024 19:26
- Changed FileHandle error handling
- Removed empty else statement

Docs:
- Removed "|" characters
- Tweaked directory description
- Minor improvements
@glotov4
Copy link
Contributor Author

glotov4 commented Apr 15, 2024

I want to rename the script to "Record keeper" (to reference the hotkey) or "Text extractor"

with ``gui/quickcmd`` and ``keybinding``.
@myk002
Copy link
Member

myk002 commented Apr 15, 2024

I want to rename the script to "Record keeper" (to reference the hotkey) or "Text extractor"

This script is already known as markdown, and I'm not sure we should change it. Anyway, when done right, people won't know or care what the script is named.

@myk002 myk002 merged commit f56ec77 into DFHack:master Apr 30, 2024
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
2 participants